Convert string into MongoDB BsonDocument

Journeyman picture Journeyman · Apr 11, 2011 · Viewed 42.1k times · Source

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.

Answer

Journeyman picture Journeyman · Apr 12, 2011

The answer is:

string json = "{ 'foo' : 'bar' }";
MongoDB.Bson.BsonDocument document
    = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);