简体   繁体   English

使用C#BsonArray将MongoDB集合中的InsertMany文档

[英]InsertMany Document in a MongoDB Collection using C# BsonArray

How to Insert more than one document in a Single statement using InsertMany() MongoDB Method in C# 如何在C#中使用InsertMany()MongoDB方法在单个语句中插入多个文档

My MongoDB Database and Connections 我的MongoDB数据库和连接

IMongoClient _client;
IMongoDatabase _database;

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

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

I'm having a Collection - BsonArray 我正在收藏-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}
    }

}

The Insert Statement: 插入语句:

collection.InsertMany(EmpInfoArray);

In the above InsertMany() Extended method has a build error . 在上面的InsertMany() Extended方法具有构建错误 Kindly assist me how to insert multiple records in a single statement execution using C#. 请协助我如何使用C#在单个语句执行中插入多个记录。

Off the top of my head, the build error is most probably because the InsertMany method is expecting a collection ( IEnumerable , List or Array ..) of BsonDocument instead of a BsonArray . 让我烦恼的是,生成错误最有可能是因为InsertMany方法期望BsonDocument而不是BsonArray的集合( IEnumerableListArray ..)。

try : 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM