dakt
dakt

Reputation: 650

Find all docs where field doesn't exists, plus if field exists apply condition

Is there a way to make a query that:

In the end we will receive all documents where certain field have matched condition AND all document where such field doesn't exist at all.

Upvotes: 24

Views: 24263

Answers (1)

John Petrone
John Petrone

Reputation: 27487

How about something like this:

db.stackoverflow.find({
  $or: [
    { howmuch: { $exists:false } },
    { howmuch:5 }
  ]})

In the stackoverflow collection, this will find all documents that do not have the howmuch field plus all documents that do have howmuch set to 5.

Upvotes: 56

Related Questions