[英]how to prevent retrieval of data from disk in a $lookup?
In the $lookup
stage of my aggregation pipeline, it needs to join based on _id
, which is indexed on the joined collection.在我的聚合管道的$lookup
阶段,它需要基于_id
进行连接,它在连接的集合上建立索引。 The purpose is to simply check whether there are any matches in joined collection.目的是简单地检查连接集合中是否有任何匹配项。 The actual data of the joined document(s) does not matter, and hence does not need to be retrieved at all whether on disk or RAM.连接文档的实际数据无关紧要,因此根本不需要在磁盘或 RAM 上检索。
So, how do I write the $lookup
to ensure that data is never retrieved from disk.那么,我该如何编写$lookup
以确保永远不会从磁盘检索数据。 Instead, a value of true
can be returned if matching records were found?相反,如果找到匹配的记录,可以返回一个true
值?
EDITED:编辑:
Retrieving data from disk is expensive, hence the reason to avoid it.从磁盘中检索数据是昂贵的,因此避免它的原因。
In the $lookup, use a pipeline that starts with a $project stage that includes only the _id field.在 $lookup 中,使用以仅包含 _id 字段的 $project 阶段开始的管道。
{$lookup: {
from: "targetCollection",
localField: "fieldName",
foreignField: "_id",
as: "matched",
pipeline: [
{ $project: {_id: 1}},
{ $count: "count" }
]
}}
The query executor should realize that all of the data needed to satisfy that part of the query can be obtained from the index, and not load any documents.查询执行器应该意识到满足该部分查询所需的所有数据都可以从索引中获取,而不是加载任何文档。
Note: this assumes you are using MongoDB 5.0+注意:这假设您使用的是 MongoDB 5.0+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.