Find documents with arrays not containing a document with a particular field value in MongoDB -
i'm trying find documents not contain @ least 1 document specific field value. example here sample collection:
{ _id : 1, docs : [ { foo : 1, bar : 2}, { foo : 3, bar : 3} ] }, { _id : 2, docs : [ { foo : 2, bar : 2}, { foo : 3, bar : 3} ] }
i want find every record there not document in docs block not contain @ least 1 record foo = 1. in example above, second document should returned.
i have tried following, tells me if there don't match (which returns document 1.
db.collection.find({"docs": { $not: {$elemmatch: {foo: 1 } } } })
update: query above work. many times happens, data wrong, not code.
i have looked @ $nin operator examples show when array contains list of primitive values, not additional document. when i've tried following, looks exact document rather foo field want.
db.collection.find({"docs": { $nin: {'foo':1 } } })
is there anyway accomplish basic operators?
using $nin
work, have syntax wrong. should be:
db.collection.find({'docs.foo': {$nin: [1]}})