Wednesday 15 February 2012

mongodb - Find documents where ALL elements of an array have a specific value -



mongodb - Find documents where ALL elements of an array have a specific value -

this simple problem, couldn't find query function it.

example collection: { _id: 1, foo: [ { bar: 9 }, { bar: 16 } ] } { _id: 2, foo: [ { bar: 9 }, { bar: 9 }, { bar: 9 } ] } example output: { _id: 2, foo: [ { bar: 9 }, { bar: 9 }, { bar: 9 } ] }

because document every foo.bar = 9.

the query i'm looking for:

"find documents foo.bar = 9 every foo.bar in document."

or need "find documents not( foo.bar != 9 )"?

thank in advance!

db.c.find({ "foo.bar" : { $exists : true }, "foo" : { $not : { $elemmatch : { "bar" : { $ne : 9 } } } } });

mongodb mongodb-query

No comments:

Post a Comment