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

标签 c# mongodb mongodb-.net-driver bson

如何在 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 方法需要一个集合(IEnumerableListArray..) 的 BsonDocument 而不是 BsonArray

尝试:

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

关于c# - 使用 C# BsonArray 在 MongoDB 集合中插入多个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802690/

相关文章:

javascript - Ruby MongoDB 驱动程序客户端查询匹配器

java - 如何在mongo查询java驱动程序中执行$or和 "and"?

c# - 使用 C# api 创建一个 mongodb 上限集合

时间:2019-03-08 标签:c#mongodb drivergroupby

c# - 当我使用 ReplaceOneAsync 和 IsUpsert = true 时,mongodb 添加了一个空 ID。我该如何阻止呢?

c# - MVC分层项目结构

mongodb - 如何在具有正确角色的现有数据库上创建 mongo 用户?

c# - 如何在 Compass Windows Phone 8 中获取 Qibla 方向?

c# - 从 SQL 中导出数据并写入文本文件(不能使用 CP 或 SP)

c# - 我怎样才能使应用程序保持响应,同时线程在后台工作?