I have a long string in JSON format, and I want to convert it into a BSONDocument for insertion into a MongoDB database. How do I do the conversion? I'm using the official C# driver.
3 Answers
string json = "{ 'foo' : 'bar' }";
BsonDocument document = BsonDocument.Parse(json);
2 Comments
Vikash Pandey
Cool.. only one error-- 'BsonDocument' does not contain a definition for 'parse -- any idea why?
Rafa
@VikashPandey: BsonDocument.Parse is included in the new .net driver for Mongo
Using Version 2.1 of MongoDB's .NET library
string json = "{'foo' : 'bar' }";
var document = new BsonDocument();
document.Add(BsonDocument.Parse(json));
1 Comment
Diogo Rodrigues
document.Add is obsolete now. Use document.addRange insted.