BsonElement on arbitrary attribute Id?

I want to map an attribute from MongoDb to Id in the POCO class. Note that it is not the real _id in MongoDb, I wanted to use the name Id over something else.

The Mongo Object looks like

       "_id": "string",
       "OtherField": "string",

The Class looks like

    class MyClass
       public string Id { get; set;}

However, even though I used the [BsonElement] decorator, the Id still reflects the _id in the MongoDb. Is it not possible to do that?

dotnet core 3.1
MongoDb.Driver 2.10.4

Hi @sssppp and welcome in the MongoDB community :muscle:,

I’m not a C# dev but I found 2 possible solutions in the doc which could solve your issue.

  1. Mapping Classes

Looks like you can use the decorator [BsonId]. If it’s set explicitly on another field, maybe it won’t be mapped to your “id” field anymore.

  1. Make a ClassMap: Mapping Classes

This will give you a chance to set your own serializer I guess.

@sssppp Try the suggestions from @MaBeuLux88_xxx and let us know how it goes.