Convert string into MongoDB BsonDocument

丶灬走出姿态 提交于 2019-11-26 17:36:06

问题


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));


来源:https://stackoverflow.com/questions/5624934/convert-string-into-mongodb-bsondocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!