Select MongoDB documents where a field either does not exist, is null, or is false? -


suppose have collection contains following documents:

{ "_id": 1, name: "apple" } { "_id": 2, name: "banana", "is_reported": null } { "_id": 3, name: "cherry", "is_reported": false } { "_id": 4, name: "kiwi",   "is_reported": true } 

is there simpler query select documents "is_reported" in falsy state; is, either non-existent, null, or false? is, query selects apple, banana, , cherry, not kiwi?

according mongodb faq, { "is_reported": null } select documents "is_reported" either null or nonexistent, still doesn't select documents "is_reported" false.

right have following query, works fine, doesn't seem elegant. if there multiple fields need select on, gets messy fast. there better query achieves same end result?

db.fruits.find({ $or: [ { "is_reported": null }, { "is_reported": false } ] }) 

you can $in:

db.fruits.find({is_reported: {$in: [null, false]}}) 

returns:

{   "_id": 1,   "name": "apple" } {   "_id": 2,   "name": "banana",   "is_reported": null } {   "_id": 3,   "name": "cherry",   "is_reported": false } 

you flip things around logically , use $ne if don't have values besides true exclude:

db.fruits.find({is_reported: {$ne: true}}) 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -