1
ConventionRegistry.Register("IgnoreIfDefault", 
                            new ConventionPack { new IgnoreIfDefaultConvention(true) }, 
                            _ => true);

var bsonDocument = anon.ToBsonDocument();

using IgnoreIfDefaultConvention can cause an unintended behavior due to the fact it affects all default values. For instance, values, as listed below, will not be saved:

int = 0
decimal = 0
bool = false

If I want to ignore only null values and Empty Arrays what is the best approach?

2 Answers 2

2

I didn't use it my own, but I think the answer is this convention: IgnoreIfNullConvention. Also you may configure a particular convention for a particular field:

BsonClassMap.RegisterClassMap<test>(c => c.MapField(e => e.A).SetIgnoreIfNull(ignoreIfNull: true));

or use this attribute BsonIgnoreIfNullAttribute

UPDATE: If you need more complicated convention, you can always implement a custom one: https://mongodb.github.io/mongo-csharp-driver/2.12/reference/bson/mapping/conventions/

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the reply. I don't have a separate class and that's why I can not use attributes and RegisterClassMap. the goal is to convert dynamic or anonymous objects to BsonDocument also exclude null and empty lists.
IgnoreIfNullConvention doesn't ignore empty lists
doesn't ignore empty lists because it's not null? If you need more complicated convention, you can always implement a custom one: mongodb.github.io/mongo-csharp-driver/2.12/reference/bson/…
looks like this is what I need Custom Conventions. I'll try to write my own custom convention. just update your answer like your last comment (if you want) I will accept that. if not I will post the custom convention later. thanks
0

This is the custom attribute we use to ignore empty collections:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class BsonIgnoreIfEmpty : Attribute, IBsonMemberMapAttribute
{
    public void Apply(BsonMemberMap memberMap)
    {
        // Get the member where the attribute is applied
        MemberInfo memberInfo = memberMap.MemberInfo;

        memberMap.SetShouldSerializeMethod(entity =>
        {
            // Get value of memberInfo
            var member = memberInfo switch
            {
                FieldInfo field => field.GetValue(entity),
                PropertyInfo property => property.GetValue(entity),
                _ => null
            };

            if (member is IEnumerable collection)
            {
                // Check if collection is empty
                return collection.GetEnumerator().MoveNext();
            }

            return false;
        });
    }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.