如何将List <t>转换为BsonArray以保存MongoDB文档

时间:2016-06-13 10:07:20

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

我有一个Model Class,我需要将它保存在MongoDB Collection中。

我的模特课程:

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; }
}

值是

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);

我希望在MongoDB中保存 EmpInfo 类型 Employee 。但我无法创建BsonDocument。请帮助我,上面的代码有什么问题。如果是,请帮助我。

2 个答案:

答案 0 :(得分:2)

无需序列化为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; }
}

screenshot from LinqPad

答案 1 :(得分:0)

除了上面的回答,如果你想出于某种原因直接与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 }
        })) }
};

您遇到错误的原因是BsonArray.Create创建了一个值数组,而不是一个对象数组。有关详细信息,请参阅this question