MongoDB: Find a document by non-existence of a field?

Mongodb

Mongodb Problem Overview


Is there a way to specify a condition of "where document doesn't contain 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"}

Mongodb Solutions


Solution 1 - Mongodb

Try the $exists operator:

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

and see its documentation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionk00kView Question on Stackoverflow
Solution 1 - MongodbdampierView Answer on Stackoverflow