Back

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

Is there a way to specify a condition of "where the document doesn't contain a field"?

For example, I want to only find the first of these 2 because it doesn't have the "price" field.

{"fruit":"apple", "color":"red"} 

{"fruit":"banana", "color":"yellow", "price":"2.00"} 

1 Answer

0 votes
by (106k points)

To find a document by non-existence of a field in MongoDB you should try the $exists operator:

db.mycollection.find({ "price" : { "$exists" : false } })

Related questions

0 votes
1 answer
0 votes
2 answers
asked Sep 13, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 4, 2019 in SQL by Sammy (47.6k points)

Browse Categories

...