MongoDB C# Driver - Ignore fields on binding

后端 未结 3 1931
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 21:10

When using a FindOne() using MongoDB and C#, is there a way to ignore fields not found in the object?

EG, example model.

public class UserModel
{
           


        
3条回答
  •  无人及你
    2020-12-04 21:46

    Yes. Another way (instead of editing you model class) is to use RegisterClassMap with SetIgnoreExtraElements.

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

    BsonClassMap.RegisterClassMap(cm =>
    {
         cm.AutoMap();
         cm.SetIgnoreExtraElements(true);
    });
    

    You can read more about ignoring extra elements using class mapping here - Ignoring Extra Elements.

提交回复
热议问题