Mongodb: find embedded element missing some key -


i have document embedded collection, few elements missing key , have find elements. here example:

var foo = {name: 'foo', embedded: [{mykey: "1", value: 3}, {mykey: "2", value: 3}]} db.example.insert(foo) var bar = {name: 'bar', embedded: [{value: 4}, {mykey: "3", value: 1}]} db.example.insert(bar) 

i need query returns 'bar' object because 1 of embedded doesn't have key 'mykey'.

i try use $exists, returns if embedded elements missing key

db.example.find({'embedded.mykey': {$exists: true}}).size() // -> 2 db.example.find({'embedded.mykey': {$exists: false}}).size() // -> 0 

how can find documents @ least 1 embedded element missing key 'mykey'?

if 'value' present, can try command

db.example.find({ embedded : { $elemmatch : { value : {$exists : true},  mykey : {$exists : false}}  }}) { "_id" : objectid("518bbccbc9e49428608691b0"), "name" : "bar", "embedded" : [ { "value" : 4 }, { "mykey" : "3", "value" : 1 } ] } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -