The actual ObjectId string type has a 12-byte hexadecimal string, such as '546c776b3e23f5f2ebdd3b03'
.
You put [BsonRepresentation(BsonType.ObjectId)]
for your Name
property. this means that the C # driver will convert the string to ObjectId and vice versa automatically before any serialization.
Remove [BsonRepresentation(BsonType.ObjectId)]
and
if you register BsonSerializer.RegisterIdGenerator(typeof(string), new StringObjectIdGenerator())
when you launch your application, and if you have a property named Id
for your entity, mongo puts a string instead of ObjectId fields for Id, and you can use any string as a key for Id
fields.
source share