Mongo find documents that do not contain a given value (using $not)

Viewed 25297

I have two items in MongoDB:

{'title':'active item',
 'tags':[
        {'tag':'active'},
        {'tag':'anothertag'}
]}
{'title':'completed item',
 'tags':[
        {'tag':'completed'}
]}

It works to find items which are tagged as completed:

db.items.find({'tags.tag':'completed'})
RESULT: [<completed item>]

Now, I want to select all items which are not tagged as completed, so I tried:

db.items.find({$not:{'tags.tag':'completed'}})
DESIRED RESULT: [<active item>]
ACTUAL RESULT: []

But somehow this doesn't return any results. Clearly I misunderstand $not in Mongo, but why? How do I query to find the records that do not contain a given value in their tags?

2 Answers
Related