6

I'm having an Model Class, I need to Save it in a MongoDB Collection.

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. But I can't able to create a BsonDocument. Kindly assist me is there is anything wrong in the above code. If yes kindly assist me.

2 Answers2

3

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

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

profesor79
  • 9,213
  • 3
  • 31
  • 52
  • Let me know one more information please. How could I add DateTime in MongoDB Document. Right now I'm using new DateTime() but in MongoDB it shows -62135596800000. –  Jun 13 '16 at 11:04
  • @IRPunch I added CreatedAt field in Employee, with initialisation CreatedAt = DateTime.Now, and it looks good – profesor79 Jun 13 '16 at 11:09
  • I'm having two more questions in mongodb - can you please assist me please 1. [Where Clause in Embedded Document](http://stackoverflow.com/questions/37801269/step-by-step-single-where-clause-query-in-a-embedded-document-of-mongodb-collect) 2. [Insert Many Document](http://stackoverflow.com/questions/37802690/insertmany-document-in-a-mongodb-collection-using-c-sharp-bsonarray) –  Jun 14 '16 at 03:58
3

In addition to answer above, I can suggest following code if you want to deal directly with Bson for some reason:

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. See this question for details.

Community
  • 1
  • 1
stop-cran
  • 4,229
  • 2
  • 30
  • 47