MongoDB C# Driver: Ignore Property on Insert MongoDB C# Driver: Ignore Property on Insert mongodb mongodb

MongoDB C# Driver: Ignore Property on Insert


It looks like the [BsonIgnore] attribute did the job.

public class GroceryList : MongoEntity<ObjectId>{    public FacebookList Owner { get; set; }    [BsonIgnore]    public bool IsOwner { get; set; }}


Alternatively, if you don't want to use the attribute for some reason (e. g. in case you don't want to bring an extra dependency to MongoDB.Bson to your DTO), you can do the following:

BsonClassMap.RegisterClassMap<GroceryList>(cm =>{  cm.AutoMap();  cm.UnmapMember(m => m.IsOwner);});


Also you can make IsOwner Nullable and add [BsonIgnoreExtraElements] to the whole class:

[BsonIgnoreExtraElements]public class GroceryList : MongoEntity<ObjectId>{    public FacebookList Owner { get; set; }    public bool? IsOwner { get; set; }}

A property with null value will be ignored during serialization.But I think [BsonIgnore] will be better for your case.