I have a C# class with some fields and some of them are null. Those that are null I do not want to be inserted into db with null value. I do not want them inserted into db at all. How do I achieve that?
class User
{
public string FirstName;
public string LastName;
public string MidName;
}
Not every user has a MidName, but Mongo inserts into db with null value on MidName field.
Using the aptly named [BsonIgnoreIfNull]
attribute:
class User
{
public string FirstName;
public string LastName;
[BsonIgnoreIfNull]
public string MidName;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With