Background
I have a mongo collection tags:
...
{ "_id" : 901, "tagkey" : "color", "tagvalue" : "black" }
{ "_id" : 902, "tagkey" : "color", "tagvalue" : "white" }
{ "_id" : 903, "tagkey" : "store", "tagvalue" : "supercenter" }
{ "_id" : 904, "tagkey" : "store", "tagvalue" : "gas station" }
...
And another collection items:
...
{ "_id" : 12, "itemname" : "truck", "tags" : [901] }
{ "_id" : 13, "itemname" : "ink", "tags" : [901, 903] }
{ "_id" : 14, "itemname" : "paper", "tags" : [902, 903] }
{ "_id" : 14, "itemname" : "gas", "tags" : [904] }
...
I am trying to ask for all the objects that are in a store. This means I want a list of all items that have a tag with a keyname: "store". So I am outputting all the items containing 903 or 904 in their tags list. So, I should get back a list containing ink, paper and gas.
Question
How do I use the output from one table's $match as the value for my next query?
My closest guess
db.tags.aggregate (
{$match: {tagkey: "store"}}, # now I have a list of all the tag items
{$match: {tags: {$elemMatch: {$in :[****]} } }} ) #Here I need to run the match query on a different collection and match the tags element to one of the _ids from the first match
)
More specific questions
- How do I get the second
$matchto refer to a different collection - How do I get that array of elements to be the _ids of the output of
the first
$match