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         }     } ]) 

read more $filter , $unwind


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -