MongoDB C# Driver: Ignore Property on Insert

╄→尐↘猪︶ㄣ 提交于 2019-12-20 10:26:32

问题


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 example, I have the following class:

public class GroceryList
{
    public string Name { get; set; }
    public FacebookList Owner { get; set; }
    public bool IsOwner { get; set; }
}

Is there a way, for the IsOwner to not get inserted when I insert a GroceryList object? Basically, I fetch the object from the database then set the IsOwner property in the app layer and then return it back to the controller, which than maps the object to a view model.

Hope my question makes sense. thanks!


回答1:


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; }
}



回答2:


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

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

Property with null value will be ignored during searilization. But i think [BsonIgnore] will be better for your case.




回答3:


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);
});



回答4:


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/




回答5:


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);


来源:https://stackoverflow.com/questions/4892816/mongodb-c-sharp-driver-ignore-property-on-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!