UpTheCreek
UpTheCreek

Reputation: 32391

MongoDb: Matching both records missing a Boolean, as well as those that are false

I have documents in my db that may be missing a particular Boolean field. I need to return the set of documents where either this field is missing or exists, but is false. How would I accomplish this?

Upvotes: 2

Views: 91

Answers (3)

Patrick Canfield
Patrick Canfield

Reputation: 1655

You can use a JavaScript function as a filter in which the current document is this. It's not as efficient as using a combination of $exists $or $eq but it allows you to use the familiar JS idiom for testing the falsy-ness (and/or non-existence) of a property:

db.myCollection.find( function() { return !this.field; } );

More at: http://docs.mongodb.org/manual/reference/operator/query/where/

Upvotes: 0

Jetson John
Jetson John

Reputation: 3829

it's possible using $exists:

db.document.find( { bln : { $exists : true } } );

This query will select all documents in the document collection where the bln field exists

db.document.find( { bln : { $exists : false } } );

This query will select all documents in the document collection where the bln field not exists

Upvotes: 1

Related Questions