javascript - elemMatch search on array of subdocument -


how search using elemmatch on array of subdocument? have document called reportcollection elements such as:-

/* 0 */ {     "_id" : objectid("5507bfc435e9470c9aaaa2ac"),     "owner" : objectid("5507bfc31e14d78e177ceebd"),     "reports" : {         "xreport" : [              {                 "name" : "xreport",                 "parameters" : {                     "x" : {                         "datetime" : "2015-03-11t18:30:00.000z",                         "unit" : 1,                         "value" : 102                     }                 },                 "createdby" : objectid("5507bfc31e14d78e177ceebd"),                 "modifiedby" : objectid("5507bfc31e14d78e177ceebd"),                 "_id" : objectid("5507bfc41e14d78e177ceebf")             }         ]     } } 

i got reports.xreport[]._id search parameter.

my following attempt failing :-

  db.reports.find ({ {owner: objectid("5507afd3d54bae3513c185cb")},      { 'reports.xreport': {$elemmatch: {_id: objectid("5507afd3d54bae3513c185cd") }}} } ) 

it seems trying put work "projection" should doing in query. rather statement should more this:

db.reports.find(     {         "owner": objectid("5507bfc31e14d78e177ceebd"),         "reports.xreport._id": objectid("5507bfc41e14d78e177ceebf")     },     { "reports.xreport.$": 1 } ) 

so "query" work of matching array position , "projection" uses position in match.

also note never need $elemmatch single field match against. when multiple fields required in condition need use match specific "element".

by same token, should in "query" statement instead.

$elemmatch in it's projected form cannot used sub-document fields through dotted notation.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -