Csharp/C#教程:使用C#BsonArray在MongoDB集合中插入TextMany文档分享


使用C#BsonArray在MongoDB集合中插入TextMany文档

如何使用C#中的InsertMany()MongoDB方法在Single语句中插入多个文档

我的MongoDB数据库和连接

IMongoClient _client; IMongoDatabase _database; _client = new MongoClient(); _database = _client.GetDatabase("test"); var collection = _database.GetCollection("EmpInfo"); 

我有一个collections–BsonArray

 var EmpInfoArray = new BsonArray { new BsonDocument { {"EmpID", "100"}, {"EmpName", "John"}, {"EmpMobile", new BsonArray { new BsonDocument { {"MobNumber", "55566610"}, {"IsPreferred", true}, {"IsLive", false} }, new BsonDocument { {"MobNumber", "55566611"}, {"IsPreferred", true}, {"IsLive", true} }, } }, {"IsLive", true} }, new BsonDocument { {"EmpID", "101"}, {"EmpName", "Peter"}, {"EmpMobile", new BsonArray { new BsonDocument { {"MobNumber", "55566610"}, {"IsPreferred", true}, {"IsLive", false} }, new BsonDocument { {"MobNumber", "55566611"}, {"IsPreferred", true}, {"IsLive", false} }, } }, {"IsLive", true} }, new BsonDocument { {"EmpID", "102"}, {"EmpName", "Jack"}, {"EmpMobile", new BsonArray { new BsonDocument { {"MobNumber", "55566610"}, {"IsPreferred", true}, {"IsLive", true} }, new BsonDocument { {"MobNumber", "55566611"}, {"IsPreferred", true}, {"IsLive", true} }, } }, {"IsLive", false} } } 

插入声明:

 collection.InsertMany(EmpInfoArray); 

在上面的InsertMany() Extended方法有一个构建错误 。 请帮助我如何使用C#在单个语句执行中插入多个记录。

在我的脑海中,构建错误很可能是因为InsertMany方法期望BsonDocument的集合( IEnumerableListArray ..)而不是BsonArray

尝试:

上述就是C#学习教程:使用C#BsonArray在MongoDB集合中插入TextMany文档分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 var EmpInfoArray = new List() { //Changed BsonArray to List new BsonDocument { {"EmpID", "100"}, {"EmpName", "John"}, {"EmpMobile", new BsonArray . . . 

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

ctvol管理联系方式QQ:251552304

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

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

精彩推荐