I have a mongo document that contains an array of embedded documents. The embedded documents have a property named “Id”.
{ Name: "Outer object", Embedded: [ {Name: "Embedded A", Id: "5f1c591a71dc237199eeaeda"} ] }
My C# mapping objects look something like this (a simplification, obviously)
public class Outer
{
public string Name { get; set; }
public IEnumerable<Inner> Inners { get; set; }
}
public class Inner
{
public string Name { get; set; }
public string Id { get; set; }
}
When I write an outer to the database, the C# driver changes the name of the Inner.Id property to _id. How do I circumvent this automatic rename? I’ve tried using the [BsonElement(“Id”)] attribute on the Id property, but it didn’t help.
MongoDB documentation explicitly states:
On the other hand, C# properties are usually pascal-case and don’t use prefixes so driver designers apparently decided to force mapping
Idproperty to_iddatabase attribute.If you want to bind a non-
_idattribute that just happens to be calledIdin MongoDB, you could declare another C# property with a name other thanIdso the driver doesn’t interfere with it: