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

3

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

我的MongoDB数据库和连接

IMongoClient _client;
IMongoDatabase _database;

_client = new MongoClient();
_database = _client.GetDatabase("test");

var collection = _database.GetCollection<BsonDocument>("EmpInfo");

我有一个集合--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() 扩展方法出现了一个构建错误。请帮我解决如何使用C#在单个语句执行中插入多个记录的问题。
1个回答

9
从我的角度来看,构建错误很可能是因为InsertMany方法期望收到一个BsonDocument的集合(IEnumerable, ListArray..),而不是一个BsonArray。请尝试使用以下代码:
var EmpInfoArray = new List<BsonDocument>() { //Changed BsonArray to List<BsonDocument>
    new BsonDocument
    {
        {"EmpID", "100"},
        {"EmpName", "John"},
        {"EmpMobile", new BsonArray
        .
        .
        .

如果这个答案是正确的,请点击勾号接受答案 :) - sm_
你能否回答我的新问题:http://stackoverflow.com/questions/37804551/multiple-logical-conditions-in-mongodb-select - user6060080

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接