Embedded id Field Converts to _id

I have a Microsoft Teams document that looks like this:

    {
       "_id":"ObjectId(""619bbe113cb12f40795fbeca"")",
       "TeamsCallDetails":{
          "_id":"063a0cc8-7c56-420e-a116-386fc63a8d76",
          "Modalities":[
             "audio",
             "videoBasedScreenSharing",
             "screenSharing"
          ]
       }
    }

There is an id field nested in the TeamsCallDetails node. I would like to keep it as “id”, but the .Net API converts it to “_id”, even though I already have a document “_id”. Is there a way to override this?

Here is the poco class for the embedded node:

public class TeamsCallDetails
{
    [JsonPropertyName("modalities")]
    public IEnumerable<string> Modalities { get; set; }

    [JsonPropertyName("id")]
    public string Id { get; set; }
}

Apologies for the formatting:

    {
        “_id”:“ObjectId(”“619bbe113cb12f40795fbeca”")",
        “TeamsCallDetails”:{
            “_id”:“063a0cc8-7c56-420e-a116-386fc63a8d76”,
            “Modalities”:[
                    “audio”,
                    “videoBasedScreenSharing”,
                    “screenSharing”
            ]
        }
    }

Hey @John_Boldt ,

try this:

        BsonClassMap.RegisterClassMap<TeamsCallDetails>(m =>
        {
            m.AutoMap();
            m.UnmapMember(m => m.Id);
            m.MapMember(m => m.Id).SetElementName("Id");
        });
        var client = new MongoClient();
        var db = client.GetDatabase("db");
        var coll = db.GetCollection<TeamsCallDetails>("coll");
        coll.InsertOne(new TeamsCallDetails());
1 Like

@Dmitry_Lukyanov thank you sir - worked like a champ!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.