Skip to main content
 首页 » 编程设计

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

2024年02月05日27sxdcgaq8080

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

请您参考如下方法:

在我脑海中,构建错误很可能是因为 InsertMany 方法需要一个集合(IEnumerable、List 或Array..) 的 BsonDocument 而不是 BsonArray。

尝试:

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