开发者

MongoDB C# Driver: Ignore Property on Insert

开发者 https://www.devze.com 2023-02-09 00:30 出处:网络
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.

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!


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.


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/


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

精彩评论

暂无评论...
验证码 换一张
取 消