Docs Menu

$lookup

On this page

  • Syntax
  • from Field Object
  • Examples
  • Basic Example
  • Nested Example

$lookup performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection for processing. For more information, see $lookup. In Atlas Data Lake, $lookup can be used to perform a join of collections from different databases.

The $lookup syntax is described in the MongoDB server manual. In Data Lake, the from field in $lookup has the following alternate syntax to support specifying an object containing an optional database name and a required collection name:

Field
Type
Description
Necessity
db
string

The database name.

If the database name is specified, data is read from the collection in the specified database. If you specify a database name that is different from the database upon which the command is operating, all nested $lookup stages must also specify a database name.

If the database name is not specified within a $lookup stage, collections in the stage inherit the database name specified in the closest parent $lookup stage if it exists or the database upon which the command is operating.

Conditional
coll
string
The collection name.
Required

Suppose there are three databases named sourceDB1, sourceDB2, and sourceDB3 with the following collections:

The following examples use the $lookup aggregation stage to join documents from one collection with the documents from the collection in the other databases.

The following aggregation operation on the sourceDB1.orders collection joins the documents from the orders collection with the documents from the sourceDB2.catalog collection using the item field from the orders collection and the sku field from the catalog collection:

db.getSiblingDb("sourceDB1").orders.aggregate(
{
$lookup: {
from: { db: "sourceDB2", coll: "catalog" },
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
)

The following aggregation operation on the sourceDB1.orders collection joins the documents from the orders collection with the documents from the sourceDB2.catalog collection and the documents from the sourceDB3.warehouses collection using the item field from the orders collection, the sku field from the catalog collection, and the stock_item and instock fields from the warehouses collection:

db.getSiblingDb(“sourceDB1”).orders.aggregate(
[
{
$lookup: {
from: db: "sourceDB2", coll: "catalog",
let: { "order_sku": "$item" },
pipeline: [
{
$match: {
$expr: {
$eq: ["$sku", "$$order_sku"]
}
}
},
{
$lookup: {
from: db: "sourceDB3", coll: "warehouses",
pipeline: [
{
$match: {
$expr:{
$eq : ["$stock_item", "$$order_sku"]
}
}
},
{
$project : { "instock": 1, "_id": 0}
}
],
as: "wh"
}
},
{ "$unwind": "$wh" },
{
$project : { "description": 1, "instock": "$wh.instock", "_id": 0}
}
],
as: "inventory"
},
},
]
)
←  $collStats$merge →
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.