How to check if property is undefined using LINQ query for MongoDB?

Viewed 28

To check whether a property is null using LINQ for MongoDB, we can write

var query = collection
    .AsQueryable()
    .Where(x => x.MyProperty == null);

But in MongoDB, null and undefined are different from each other. How can we test using LINQ(!) whether a property is (or is not) undefined?

1 Answers

This is what I came up with as a workaround, but perhaps there is a better solution:

var filter = Builders<MyType>.Filter.Exists(
    x => x.MyProperty, false);

var query = collection
    .AsQueryable()
    .Where(x => filter.Inject() && ...)
Related