The $lookup operator joins information from multiple collections into a single document. While the $lookup operation is useful when used infrequently, it can be slow and resource-intensive compared to operations that only query a single collection. $lookup运算符将多个集合中的信息连接到一个文档中。虽然$lookup操作在不经常使用时很有用,但与仅查询单个集合的操作相比,它可能很慢且资源密集。If you often use 如果您经常使用$lookup operations, consider restructuring your schema to store related data in a single collection. This can improve query performance and reduce the cost of operations.$lookup操作,请考虑重构模式以将相关数据存储在单个集合中。这可以提高查询性能并降低操作成本。
About this Task关于此任务
Consider the following schema with two separate collections: 考虑以下具有两个单独集合的模式:products and orders. Each order can contain multiple products, and you want to keep track of product details within each order for quick access. The two separate collections are joined by a $lookup operation.products和orders。每个订单可以包含多个产品,您希望跟踪每个订单中的产品详细信息,以便快速访问。这两个单独的集合通过$lookup操作连接在一起。
//products collection
db.products.insertMany( [
{
_id: 1,
name: "Laptop",
price: 1000,
manufacturer: "TechCorp",
category: "Electronics",
description: "Fastest computer on the market."
},
{
_id: 2,
name: "Headphones",
price: 100,
manufacturer: "Sound",
category: "Accessories",
description: "The latest sound technology."
},
{
_id: 3,
name: "Tablet",
price: 200,
manufacturer: "TechCorp",
category: "Electronics",
description: "The most compact tablet."
}
] )
//orders collection
db.orders.insertMany( [
{
_id: 101,
customer_name: "John Doe",
timestamp: "2024-05-11T010:00:00Z",
product_ids: [1, 2],
total: 1200
},
{
_id: 102,
customer_name: "Jane Smith",
timestamp: "2024-05-11T012:00:00Z",
product_ids: [2],
total: 100
}
] )
In this schema, you need to use the 在此模式中,每次访问订单信息时都需要使用$lookup operation everytime you access order information. Running the $lookup operation adds query complexity and degrades performance. To reduce the use of $lookup operations, store data that is accessed together in a single collection .$lookup操作。运行$lookup操作会增加查询复杂性并降低性能。为了减少$lookup操作的使用,请将一起访问的数据存储在单个集合中。
Example示例
You can use the subset schema design pattern to embed a subset of product details in the 您可以使用子集模式设计模式在orders collection. This lets you query a single collection to return the required results. Product details and documents not relevant to the orders collection remain in the products collection.orders集合中嵌入产品详细信息的子集。这使您可以查询单个集合以返回所需的结果。与orders集合无关的产品详细信息和文件仍保留在产品集合中。
//orders collection
db.orders.insertMany( [
{
_id: 101,
customer_name: "John Doe",
timestamp: "2024-05-11T10:00:00Z",
products: [
{
product_id: 1,
name: "Laptop",
price: 1000
},
{
product_id: 2,
name: "Headphones",
price: 100
}
],
total: 1100
},
{
_id: 102,
customer_name: "Jane Smith",
timestamp: "2024-05-11T12:00:00Z",
products: [
{
product_id: 2,
name: "Headphones",
price: 100
}
],
total: 100
}
] )
//products collection
db.products.insertMany( [
{
_id: 1,
name: "Laptop",
price: 1000,
manufacturer: "TechCorp",
category: "Electronics",
description: "Fastest computer on the market."
},
{
_id: 2,
name: "Headphones",
price: 100,
manufacturer: "Sound",
category: "Accessories",
description: "The latest sound technology."
},
{
_id: 3,
name: "Tablet",
price: 200,
manufacturer: "TechCorp",
category: "Electronics",
description: "The most compact tablet."
}
] )
This approach lets you keep collections separate while avoiding multiple queries by embedding key fields from the 这种方法允许您将集合分开,同时通过将product collection into the orders collection. This improves read performance and simplifies data retrieval, as you can access all necessary information in a single query. However, it's important to consider potential document size limitations and data duplication.product集合中的键段嵌入orders集合来避免多个查询。这提高了读取性能并简化了数据检索,因为您可以在单个查询中访问所有必要的信息。然而,重要的是要考虑潜在的文档大小限制和数据重复。