node.js - Using mongose aggregation to populate from collection in mongo -
sample document
{ _id:"123", "completed" : [ { "id" : objectid("57caae00b2c40dd21ba089be") "subname" : "oiyt", "name" : "endo", }, { "id" : objectid("57caae00b2c40dd21ba089be"), "subname" : "oiyt", "name" : "endo", } ] }
how access name
, subname
complete
_id
matches?
you can use $filter
or $unwind
(or both).
this example shows how use $filter
document 1 matched element in array, , $unwind
easier access matched element.
but there many more options desired result.
db.collection.aggregate([ { $project: { filtered_completed: { $filter:{ input: "$completed", as: "complete", cond: { $eq: [input_id, "$$complete.id"] } } } } }, { $unwind: "$filtered_completed" // because filtered 'completed' array, 1 document. // can use first aggreagation pipeline stage , match _id }, { $project: { "filtered_completed.name": 1, "filtered_completed.subname": 1 } } ])
Comments
Post a Comment