I'm working with latest C# driver for MongoDB. I know it's beta now, but I think I'm doing some basic things.
What's my problem: I'm trying to set representation for my Id field to ObjectId
instead of string
like it is described in documentation:
BsonClassMap.RegisterClassMap<Entity>(cm =>
{
cm.AutoMap();
cm.IdMemberMap.SetRepresentation(BsonType.ObjectId);
});
But I can not do that because method SetRepresentation()
does not exist. And I can not find anything similar.
So I wonder, was this method removed? Is there any other way to set representation besides attributes? I can not use attributes because I don't have access to Entity class, I'm working with derived class.
Thanks in advance!
I've spoken with the developer of the driver and he clarified this situation:
We've brought all those options into the serializers themselves, so, in this case, you'll want to set the serializer. IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId)); //It's a string which will be represented as an ObjectId in the database.
this works for me (v2.2)
cm.MapIdMember(c => c.Id)
.SetSerializer(new StringSerializer(BsonType.ObjectId))
.SetIdGenerator(StringObjectIdGenerator.Instance);
it's represent objectId in database (not string)
"_id" : ObjectId("56715ebddb6986202816e566"),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With