Solution: Grouping Across Collections
We'll cover the following...
We'll cover the following...
The explanation of the query is given below:
Line 1: This begins an aggregation pipeline on the
orderscollection.Lines 4–7:
$matchfilters documents before processing further. This keeps only orders placed on or after January 1, 2025. Whereas,$gtemeans “greater than or equal to.”Lines 10–12:
$unwindtakes an array field (items) and creates a separate document for each element. If one order has three items, this stage creates three separate documents, one per item.Lines 16–21:
$lookupperforms a left join between collections. It looks in theproductscollection to find documents where:orders.items(product name in the order) matchesproducts.name(product name in the ...