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

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

我有一个模型 Class,我需要将它保存在 MongoDB 集合中。

我的模型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; }
}

值为

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

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

无需序列化为bson文档 您可以使用 TYPED 集合并只插入数据 请参阅随附的代码片段,其中更新了 class 结构

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

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