If you're fine using the MongoDB C # or Mapper attributes, you can do something like this:
public class Order { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } }
So you can usually refer to a type as a string (including serialization), but when MongoDB serializes it, etc., it is internally treated as an ObjectId. Here, using the class map method:
BsonClassMap.RegisterClassMap<Order>(cm => { cm.AutoMap(); cm.SetIdMember(cm.GetMemberMap(c => c.Id); cm.GetMemberMap(c => c.Id) .SetRepresentation(BsonType.ObjectId); });
source share