Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting the POCO Model into MongoDB's Bson format

Here is the information about my development environment:

Microsoft Visual Studio Community 2015

.NET Framework 4.6

ASP.NET MVC assembly System.Web.Mvc Version=5.2.3.0

MongoDB.Driver 2.0.1.27

Mongodb 3.0.6

Within my C# application, I have the following code that retrieves a MongoDB database reference:

 public class MongoDBConnectionManager
 {

     public IMongoDatabase getMongoDB() {

            var client = new MongoClient("mongodb://localhost:27017");
            IMongoDatabase iMgDb = client.GetDatabase("foo");
            IMongoCollection<BsonDocument> UserDetails = 
            iMgDb.GetCollection<BsonDocument>("Users");
            return iMgDb;
     } // end of public IMongoDatabase getMongoDB()

  } // end of public class MongoDBConnectionManager

Here is the POCO class that represent a User Business Entity:

 using MongoDB.Bson.Serialization.Attributes;

 public class UserModel
 {

    public object _id { get; set; } //MongoDb uses this field as identity.

   [BsonId]
   public int ID { get; set; }

   [Required]
   [BsonRepresentation(MongoDB.Bson.BsonType.String)]
   public string UserName { get; set; }

   [Required]
   //[DataType(DataType.Password)]
   [BsonRepresentation(MongoDB.Bson.BsonType.String)]
   public string Password { get; set; }

   [Required]
   // [DataType(DataType.EmailAddress)]
   [BsonRepresentation(MongoDB.Bson.BsonType.String)]
   public string Email { get; set; }


   [BsonRepresentation(MongoDB.Bson.BsonType.String)]
   public string PhoneNo { get; set; }

   [BsonRepresentation(MongoDB.Bson.BsonType.String)]
   public string Address { get; set; }

 }

Here is the DAO C# class that uses the Mongo DB Connection Manager Class:

   public class DAO
   {

       public int insertNewUser(UserModel um)
       {
         MongoDBConnectionManager mgoDBCntMng = new MongoDBConnectionManager();
         IMongoDatabase database = mgoDBCntMng.getMongoDB();
         IMongoCollection <BsonDocument> UserDetails = database.GetCollection<BsonDocument>("Users");
         UserDetails.InsertOneAsync(um);

         return 0;
      }
  }

The problem that I'm facing is with the following line of code in the DAO class:

  UserDetails.InsertOneAsync(um);

Visual Studio Community 2015 displays the following error:

  cannot convert from 'UserModel' to MongoDB.Bson.BsonDocument

I believe the error has something to do with somehow translating the UserModel POCO into MongoDB's Bson format. Therefore, in my UserModel POCO, I used the use space called MongoDB.Bson.Serialization.Attributes because I thought we needed to use MongoDB Annotations. However, it still failed to solve the problem.

Could someone please tell me how to correct the problem?

like image 928
user1338998 Avatar asked Mar 15 '23 17:03

user1338998


1 Answers

You should remove the _id property from your model as it is generated by mongo and you don't need to add it yourself. Also you can mark all your properties that should be included in the model with [BsonElement] attribute except the [BsonId].

Also you should serialize and deserialize to your own model not BsonDocument :

MongoCollection<UserModel> userDetails = database.GetCollection<UserModel>("Users");
userDetails.InsertOneAsync(um);

You are creating your custom class and trying to fetch and insert BsonDocument without any conversion. If you want to work directly with BsonDocument you can call something like this:

var umToBson = um.ToBsonDocument();

and then insert it as you are trying to.

like image 102
dlght Avatar answered Mar 17 '23 05:03

dlght