Skip to content

Instantly share code, notes, and snippets.

@gharriso
Created February 15, 2018 00:23
Show Gist options
  • Save gharriso/234b5247c37377c6e8466e97797b3b5c to your computer and use it in GitHub Desktop.
Save gharriso/234b5247c37377c6e8466e97797b3b5c to your computer and use it in GitHub Desktop.
var doc = db.
getCollection('customers').
aggregate(
[
{
$match: { _id: customerId }
},
{
$lookup: {
from: 'orders',
localField: '_id',
foreignField: 'customerId',
as: 'orders'
}
},
{
$lookup: {
from: 'lineitems',
localField: 'orders._id',
foreignField: 'orderId',
as: 'lineitems'
}
},
{
$lookup: {
from: 'products',
localField: 'lineitems.prodId',
foreignField: '_id',
as: 'products'
}
}
],
{ allowDiskUse: true }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment