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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The answer is:
string json = "{ 'foo' : 'bar' }";
MongoDB.Bson.BsonDocument document
= MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);
回答2:
string json = "{ 'foo' : 'bar' }";
BsonDocument document = BsonDocument.Parse(json);
回答3:
Using Version 2.1 of MongoDB's .NET library
string json = "{'foo' : 'bar' }";
var document = new BsonDocument();
document.Add(BsonDocument.Parse(json));