.NET driver and raw bson data

24 views
Skip to first unread message

Andreas Vilinski

unread,
May 3, 2016, 4:36:43 PM5/3/16
to mongodb-user
Hi,

i would like insert data structures already serialized to bson byte array (with FsPickler)
Can't figure out myself whether it is possible with current .NET driver.
There is RawBsonDocument class, however i was not able to make insert with it.

Can you help?


Craig Wilson

unread,
May 3, 2016, 4:48:38 PM5/3/16
to mongodb-user
It should be as simple as using 

var collection = db.GetCollection<RawBsonDocument>("collection_name");
var doc = new RawBsonDocument(bytes);

collection.InsertOne(doc);

Perhaps you could let us know what you tried and the error you received.
Craig

Andreas Vilinski

unread,
May 4, 2016, 6:43:05 AM5/4/16
to mongodb-user
Oh, thanks!

my try was:

var collection = db.GetCollection<BsonDocument>("collection_name");

collection of BsonDocument, but tried to insert RawBsonDocument, which compiled but didn't work

Robert Stam

unread,
May 5, 2016, 3:12:15 PM5/5/16
to mongod...@googlegroups.com
A collection of <BsonDocument> should work as well... I tried it on my machine and it did work:

    var client = new MongoClient("mongodb://localhost");
    var database = client.GetDatabase("test");
    var collection = database.GetCollection<BsonDocument>("test");
    var document = new BsonDocument { { "_id", 1 }, { "x", 0 } };
    var bytes = document.ToBson();
    var rawDocument = new RawBsonDocument(bytes);

    database.DropCollection("test");
    collection.InsertOne(rawDocument);


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8c73dbc6-9049-4d95-99f3-744bfef6f842%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages