MongoDB - Using Aggregate to get more than one Matching Object in an Array -
i'm trying poster in this link trying accomplish. have documents same structure poster; in documents there array of objects, each many keys. want bring objects (not first, can $elemmatch
) in array key's value matches query. want query's result array of objects, there key in each object matches query. example, in case of linked question, want return array of objects "class":"s2"
. want returned:
"filtermetric" : [ { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s2" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s2" } ]
i tried queries in answer. first 2 queries bring empty array in robomongo. in shell, command nothing , return me next line. here's screenshot of robomongo:
on third query in answer, unexpected token
line "input"
is.
i'm using mongodb version 3.0.2. appears if op successful answer, i'm wondering if there version issue, or if i'm approaching wrong way.
the problem answers in question seems they're using wrong casing filtermetric
. example, works:
db.sample.aggregate([ { "$match": { "filtermetric.class": "s2" } }, { "$unwind": "$filtermetric" }, { "$match": { "filtermetric.class": "s2" } }, { "$group": { "_id": "$_id", "filtermetric": { "$push": "$filtermetric" } }} ])
Comments
Post a Comment