Docs 菜单
Docs 主页
/
MongoDB 阿特拉斯
/ / /

$lookup

在此页面上

  • 语法
  • from 字段对象
  • 举例
  • 基本示例
  • 嵌套示例

MongoDB 服务器 $lookup执行一个未分片集合与同一数据库中另一个未分片集合的左外连接。 查找非常有用,因为它们允许您从“已联接”集合中筛选文档以进行处理。

在联合数据库实例中,您可以使用$lookup连接来自Atlas、 Amazon Web Services S3以及HTTPHTTPS数据存储的同一数据库或不同数据库的分片和非collection集合。

注意

对于分片集合, $lookup仅适用于运行 MongoDB 5.1及更高版本的 Atlas 集群。

MongoDB 服务器手册中描述了$lookup语法

在 Data Federation 中, $lookup中的from字段采用以下备用语法。 这允许您指定一个包含可选的数据库名称和必需的collection名称的对象:

{
$lookup: {
localField: "<fieldName>",
from: <collection-to-join>|{db: <db>, coll: <collection-to-join>},
foreignField: "<fieldName>",
as: "<output-array-field>",
}
}
{
$lookup: {
from: <collection to join>|{db: <db>, coll: <collection-to-join>},
let: { <var_1>: <expression>,, <var_n>: <expression> },
pipeline: [ <pipeline to execute on the collection to join> ],
as: <output array field>
}
}
字段
类型
说明
必要性
db
字符串

数据库名称。

如果指定数据库名称, Data Federation将从指定数据库的集合中读取数据。 如果指定的数据库名称与命令正在操作的数据库不同,则所有嵌套$lookup阶段也必须指定此数据库名称。

如果未在$lookup阶段中指定数据库名称,则该阶段中的集合将继承最近的父$lookup阶段中指定的数据库名称(如果存在),或者是命令正在操作的数据库的名称。

可选的
coll
字符串
collection名称。
必需

假设存在三个名为sourceDB1sourceDB2sourceDB3的数据库,其中包含以下集合:

db.orders.insertMany([
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
{ "_id" : 3 }
])
db.catalog.insertMany([
{ "_id" : 1, "sku" : "almonds", "description": "product 1" },
{ "_id" : 2, "sku" : "bread", "description": "product 2" },
{ "_id" : 3, "sku" : "cashews", "description": "product 3" },
{ "_id" : 4, "sku" : "pecans", "description": "product 4" },
{ "_id" : 5, "sku": null, "description": "Incomplete" },
{ "_id" : 6 }
])
db.warehouses.insertMany([
{ "_id" : 1, "stock_item" : "almonds", "warehouse": "A", "instock" : 120 },
{ "_id" : 2, "stock_item" : "pecans", "warehouse": "A", "instock" : 70 },
{ "_id" : 3, "stock_item" : "cashews", "warehouse": "B", "instock" : 60 },
{ "_id" : 4, "stock_item" : "bread", "warehouse": "B", "instock" : 80 },
{ "_id" : 5, "stock_item" : "cookies", "warehouse": "A", "instock" : 80 }
])

以下示例使用$lookup聚合阶段将一个集合中的文档与其他数据库中该集合中的文档连接起来。

以下针对sourceDB1.orders集合的聚合操作使用orders sourceDB2.catalog ordersitem字段以及 sku catalog集合:

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

以下针对 集合的聚合操作使用sourceDB1.orders orders集合中的 字段将sourceDB2.catalog 集合中的文档与 集合中的文档以及sourceDB3.warehouses itemorders集合中的文档连接起来,sku 集合中的catalog 字段,以及stock_item instock集合中的 和warehouses 字段:

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