如何使用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#在单个语句执行中插入多个记录的问题。