Csharp/C#教程:将BSON数组添加到MongoDB中的BsonDocument分享


将BSON数组添加到MongoDB中的BsonDocument

如何使用C#驱动程序将BsonArray添加到MongoDB中的BsonDocument? 我想要一个像这样的结果

{ author: 'joe', title : 'Yet another blog post', text : 'Here is the text...', tags : [ 'example', 'joe' ], comments : [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ] } 

您可以使用以下语句在C#中创建上述文档:

 var document = new BsonDocument { { "author", "joe" }, { "title", "yet another blog post" }, { "text", "here is the text..." }, { "tags", new BsonArray { "example", "joe" } }, { "comments", new BsonArray { new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } }, new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } } }} }; 

您可以测试是否获得了写入结果:

 var json = document.ToJson(); 

你也可以在BsonDocument已经存在之后添加数组,如下所示:

上述就是C#学习教程:将BSON数组添加到MongoDB中的BsonDocument分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 BsonDocument doc = new BsonDocument { { "author", "joe" }, { "title", "yet another blog post" }, { "text", "here is the text..." } }; BsonArray array1 = new BsonArray { "example", "joe" }; BsonArray array2 = new BsonArray { new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } }, new BsonDocument { { "author", "nancy" }, { "comment", "Good post" } } }; doc.Add("tags", array1); doc.Add("comments", array2); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1007817.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐