$lookup
The $lookup aggregation stage in Amazon DocumentDB allows you to perform a left outer join between two collections. This operation lets you combine data from multiple collections based on matching field values. It is particularly useful when you need to incorporate data from related collections into your query results.
Parameters
-
from: The name of the collection to perform the join with. -
localField: The field from the input documents to match against theforeignField. -
foreignField: The field from the documents in thefromcollection to match against thelocalField. -
as: The name of the new field to add to the output documents containing the matching documents from thefromcollection.
Example (MongoDB Shell)
The following example demonstrates a simple $lookup operation that joins data from the orders collection into the customers collection.
Create sample documents
db.customers.insertMany([ { _id: 1, name: "Alice" }, { _id: 2, name: "Bob" }, { _id: 3, name: "Charlie" } ]); db.orders.insertMany([ { _id: 1, customer_id: 1, total: 50 }, { _id: 2, customer_id: 1, total: 100 }, { _id: 3, customer_id: 2, total: 75 } ]);
Query example
db.customers.aggregate([ { $lookup: { from: "orders", localField: "_id", foreignField: "customer_id", as: "orders" } } ]);
Output
[
{
_id: 1,
name: 'Alice',
orders: [
{ _id: 2, customer_id: 1, total: 100 },
{ _id: 1, customer_id: 1, total: 50 }
]
},
{ _id: 3, name: 'Charlie', orders: [] },
{
_id: 2,
name: 'Bob',
orders: [ { _id: 3, customer_id: 2, total: 75 } ]
}
]
Code examples
To view a code example for using the $lookup command, choose the tab for the language that you want to use: