aggregation framework - Using $match after a $lookup in MongoDB -
i have 2 collections , want fields both, i'm using $lookup in aggregation pipeline.
this works fine , returns documents field, array 0 or 1 elements (an object). if 0 elements, means join(in sql world) didn't return anything. if 1 element, means there match , element in object fields of second collection.
now have results, i'd use $match in order filter of results.
in order use $match first want use $unwind on new field in order extract array. problem once insert $unwind stage, result of query single document.
why happening? how can $unwind , $match documents got $lookup stage?
assume have documents after lookup:
{doc:{_id:1, lookuparray:[{doc:1},{doc:2}]}} and
{doc:{_id:2, lookuparray:[/* empty */]}} when $unwind without options get:
- {doc:{_id:1, lookuparray:{doc:1}}}
- {doc:{_id:1, lookuparray:{doc:2}}}
- null
and when specify
{ $unwind: { path: "$array", preservenullandemptyarrays: true } } then get:
- {doc:{_id:1, lookuparray:{doc:1}}}
- {doc:{_id:1, lookuparray:{doc:2}}}
- {doc:{_id:2, lookuparray:[/* empty */]}}
so when want perform search value doc lookuparray, $match this:
{$match:{'lookuparray.doc':2}} any comments welcome!
Comments
Post a Comment