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:

  1. {doc:{_id:1, lookuparray:{doc:1}}}
  2. {doc:{_id:1, lookuparray:{doc:2}}}
  3. null

and when specify

 { $unwind: { path: "$array", preservenullandemptyarrays: true } } 

then get:

  1. {doc:{_id:1, lookuparray:{doc:1}}}
  2. {doc:{_id:1, lookuparray:{doc:2}}}
  3. {doc:{_id:2, lookuparray:[/* empty */]}}

so when want perform search value doc lookuparray, $match this:

   {$match:{'lookuparray.doc':2}} 

any comments welcome!


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -