开发者

Convert string into MongoDB BsonDocument

开发者 https://www.devze.com 2023-02-24 09:48 出处:网络
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 con开发者_高级运维version?I\'m using the official C# driver.The

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 con开发者_高级运维version? I'm using the official C# driver.


The answer is:

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


string json = "{ 'foo' : 'bar' }";  
BsonDocument document = BsonDocument.Parse(json);


Using Version 2.1 of MongoDB's .NET library

string json = "{'foo' : 'bar' }";
var document = new BsonDocument();
document.Add(BsonDocument.Parse(json));
0

精彩评论

暂无评论...
验证码 换一张
取 消