Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can i use pocos with mongodb c# driver

Tags:

c#

mongodb

I was wondering if I can read and write raw pocos to mongodb

the driver tutorial shows adding each field to a bsondocument one at a time. Does bsonserialzer do it?

I can write something myself to reflect on an object but I wonder it it already exists.

Working with dynamic expandos would be nice

like image 368
pm100 Avatar asked Dec 11 '25 18:12

pm100


2 Answers

Yes, the 10gen official C# MongoDB driver supports POCO serialisation and deserialisation, e.g.

MongoCollection<Thing> thingCollection = _db.GetCollection<Thing>("things");
Thing thing = col.FindAllAs<Thing>();
col.Insert(new Thing { Name = "Foo" });
like image 108
Chris Fulstow Avatar answered Dec 14 '25 06:12

Chris Fulstow


I think you can and you should, 10gen driver POCO objects. You can design your POCO model in a completely sepparate assembly without any reference to Mongo.Driver o Mongo.BSon and configure the entry point of your app to use that assembly, setting indexes, ingnore fields, discriminators, ids columns, and a large etc.

 BsonClassMap.RegisterClassMap<Post>(cm =>
        {
            cm.AutoMap();
            cm.SetIdMember(cm.GetMemberMap(c => c.IdPost));
            cm.UnmapProperty(c => c.TimeStamp);
            cm.UnmapProperty(c => c.DatePostedFormat);
            cm.UnmapProperty(c => c.IdPostString);
            cm.UnmapProperty(c => c.ForumAvatar);
            cm.UnmapProperty(c => c.ForumAvatarAlt);
        });
like image 21
javierlinked Avatar answered Dec 14 '25 07:12

javierlinked