简体   繁体   English

如何转换列表 <T> 到BsonArray保存MongoDB文档

[英]How to Convert List<T> to BsonArray to save a MongoDB Document

I'm having an Model Class, I need to Save it in a MongoDB Collection. 我有一个模型类,我需要将它保存在MongoDB集合中。

My Model Class: 我的模型类:

public Class Employee
{
    public string EmpID { get; set; }
    public string EmpName { get; set; }
    public List<Mobile> EmpMobile { get; set; }
}

public Class Mobile
{
    public string MobID { get; set; }
    public string MobNumber { get; set; }
    public bool IsPreferred { get; set; }
}

The Values are 价值观是

Employee EmpInfo = new Employee()
{
    EmpID = "100",
    EmpName = "John",
    EmpMobile = new List<Mobile>()
    {
        { MobNumber = "55566610", IsPreferred = true },
        { MobNumber = "55566611", IsPreferred = false },
    }
}

BsonDocument _employee = new BsonDocument()
                             {
                                 { "Emp_ID", EmpInfo.EmpID },
                                 { "Emp_Name", EmpInfo.EmpName },
                                 { "Emp_Mobile", new BsonArray (EmpInfo.EmpMobile.Select(m => new 
                                     {
                                         MobID = new ObjectId(),
                                         MobNumber = m.MobNumber,
                                         IsPreferred = m.IsPreferred
                                     })) }
                             };

var collection = _database.GetCollection<BsonDocument>("EmployeeInfo");
collection.InsertOne(_employee);

I wish to save the above EmpInfo of type Employee in a MongoDB. 我希望在MongoDB中保存Employee类型的上述EmpInfo But I can't able to create a BsonDocument. 但我无法创建BsonDocument。 Kindly assist me is there is anything wrong in the above code. 请帮助我,上面的代码有什么问题。 If yes kindly assist me. 如果是,请帮助我。

there is no need to serialize to bson document You can use TYPED collection and just insert data Please see attached code snipet with updated class structure 没有必要序列化到bson文档你可以使用TYPED集合,只需插入数据请参阅附加的代码snipet与更新的类结构

void Main()
{
    // To directly connect to a single MongoDB server
    // or use a connection string
    var client = new MongoClient("mongodb://localhost:27017");
    var database = client.GetDatabase("test");      

var collectionEmpInfo = database.GetCollection<Employee>("Employee");

Employee EmpInfo = new Employee
{       
    EmpID = "100",
    EmpName = "John",
    EmpMobile = new List<Mobile>
    {
        new Mobile{ MobNumber = "55566610", IsPreferred = true, MobID = ObjectId.GenerateNewId() },
        new Mobile{ MobNumber = "55566611", IsPreferred = false, MobID = ObjectId.GenerateNewId() },
    }
};

collectionEmpInfo.InsertOne(EmpInfo);
var empList = collectionEmpInfo.Find(new BsonDocument()).ToList();
empList.Dump(); //dump is used in linqPad

}

public class Employee
{
   public ObjectId Id  { get; set; }
    public string EmpID { get; set; }
    public string EmpName { get; set; }
    public List<Mobile> EmpMobile { get; set; }
}

public class Mobile
{
    public ObjectId MobID { get; set; }
    public string MobNumber { get; set; }
    public bool IsPreferred { get; set; }
}

来自LinqPad的截图

In addition to answer above, I can suggest following code if you want to deal directly with Bson for some reason: 除了上面的回答,如果你想出于某种原因直接与Bson打交道,我可以建议以下代码:

BsonDocument _employee = new BsonDocument()
{
    { "Emp_ID", EmpInfo.EmpID },
    { "Emp_Name", EmpInfo.EmpName },
    { "Emp_Mobile", BsonArray.Create(EmpInfo.EmpMobile.Select(m => new BsonDocument()
        {
            { "MobID" , new ObjectId() },
            { "MobNumber", m.MobNumber },
            { "IsPreferred", m.IsPreferred }
        })) }
};

The reason of the error you've got is that BsonArray.Create creates an array of values, not an array of objects. 你得到错误的原因是BsonArray.Create创建了一个值数组,而不是一个对象数组。 See this question for details. 有关详情,请参阅此问题

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

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