MongoDB C# Driver: Ignore Property on Insert

前端 未结 5 1650
余生分开走
余生分开走 2021-02-03 18:48

I am using the Official MongoDB C# Drive v0.9.1.26831, but I was wondering given a POCO class, is there anyway to ignore certain properties from getting inserted.

For ex

相关标签:
5条回答
  • 2021-02-03 19:13

    You should probably want to combine the two attributes BsonIgnoreExtraElements and BsonIgnore. The reason for that is although BsonIgnore will not insert the "IsOwner" property to you DB but if you have "old" instances in your DB that contained this field and you will remove this fields from your model in the feature or extend your "GroceryList" class and use your new class in the DB will get an exception:

    "Element 'IsOwner' does not match any field or property of class."

    Another way (instead of editing you model class) is to use "Register Class Map" with "SetIgnoreExtraElements" and "UnmapMember" together.

    In your case just add this code when you initialize your driver:

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

    You can read more about Mongo Class Mapping in:

    http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/

    0 讨论(0)
  • 2021-02-03 19:15

    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);
    });
    
    0 讨论(0)
  • 2021-02-03 19:20

    Just in case somebody might be interested in another way of doing it. Via conventions:

    public class IgnoreSomePropertyConvention : ConventionBase, IMemberMapConvention
    {
        public void Apply(BsonMemberMap memberMap)
        { // more checks will go here for the case above, e.g. type check
            if (memberMap.MemberName != "DoNotWantToSaveThis")
                memberMap.SetShouldSerializeMethod(o => false);
        }
    }
    

    And then you need to register this convention once during you app startup:

    ConventionRegistry.Register("MyConventions", new ConventionPack { new IgnoreBaseIdConvention()  }, t => true);
    
    0 讨论(0)
  • 2021-02-03 19:35

    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; }
    }
    
    0 讨论(0)
  • 2021-02-03 19:36

    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.

    0 讨论(0)
提交回复
热议问题