Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

So I'm attempting to find all records who have a fieldset and aren't null.

I try using $exists, however, according to the MongoDB documentation, this query will return fields who equal null.

$exists does match documents that contain the field that stores the null value.

So I'm now assuming I'll have to do something like this:

db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })

Whenever I try this, however, I get the error [invalid use of $not] Anyone has an idea of how to query for this?

1 Answer

0 votes
by (106k points)

To check that field exists with MongoDB you can use $ne (for "not equal"):-

db.collection.find({ "fieldToCheck": { $exists: true, $ne: null } })

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...