Skip to content

Instantly share code, notes, and snippets.

@PonomareVlad
Created March 2, 2023 21:36
Show Gist options
  • Save PonomareVlad/3f8d0c7418cb5d6261b9d83a673232a9 to your computer and use it in GitHub Desktop.
Save PonomareVlad/3f8d0c7418cb5d6261b9d83a673232a9 to your computer and use it in GitHub Desktop.
Aggregation with pipeline
db.Vehicle.aggregate([
{
$match: {vin: "..."}
},
{
$lookup: {
from: 'Address', //i want all addresses
localField: '_vin',
foreignField: 'vehicle.vin',
as: 'addresses'
}
},
{
$lookup: {
from: 'Driver', //i wand all drivers
localField: '_vin',
foreignField: "vehicle.vin",
as: "drivers"
}
},
{
$lookup: {
from: "Upgrade", //i only want upgrades of type "external" (the document has a field "type":"external")
localField: "_vin",
foreignField: "vehicle.vin",
as: "upgrades",
pipeline: [
{
$match: {
type: "external"
}
}
]
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment