Unable to use BsonIgnoreIfDefault for property of

2019-08-06 21:21发布

[BsonDefaultValue(0)]
[BsonIgnoreIfDefault]
public long TotalItems { get; set; }

The attribute [BsonDefaultValue(0)] is preventing the complete document being inserted into the mongo whereas I just want to prevent storing TotalItems if its value is zero. If I don't use the attributes [BsonDefaultValue(0)], [BsonIgnoreIfDefault] the the document is inserted properly in the db with TotalItems is inserted in the document as "TotalItems" : NumberLong(0)" which I actually don't want to strore into db if its zero. My question why [BsonDefaultValue(0)], [BsonIgnoreIfDefault] attributes preventing the insertion of the complete document.

Note: I am able to use the above two attributes with properties of type int.

1条回答
神经病院院长
2楼-- · 2019-08-06 22:02

Minor error there, you need to cast your default value to a long:

[BsonDefaultValue((long)0)]
[BsonIgnoreIfDefault]
public long TotalItems { get; set; }
查看更多
登录 后发表回答