mongodb - Query on 2 properties of the same embedded object in an array -
i have collection of objects :
db.coll.find().pretty(); { "_id" : "1", "elements" : [ { "key" : "a", "value" : 10 }, { "key" : "b", "value" : 1 }, ] }, { "_id" : "2", "elements" : [ { "key" : "a", "value" : 1 }, { "key" : "c", "value" : 33 }, ] }
i want find documents contain element "key" equal "a" , "value" greater 5.
not sure if possible without using aggregation framework.
without aggregation, using $elemmatch , query below :
db.coll.find({"elements":{"$elemmatch":{"$and":[{"key":"a"},{"value":{"$gt":5}}]}}}).pretty()
or if want use aggregation used following query aggregation
db.coll.aggregate({"$unwind":"$elements"},{"$match":{"$and":[{"elements.key":"a"},{"elements.value":{"$gt":5}}]}}).pretty()
Comments
Post a Comment