简体   繁体   English

如何防止在 $lookup 中从磁盘检索数据?

[英]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.

相关问题 从 Parse Server Query 检索数据不起作用? - Data retrieval from Parse Server Query not working? 从mongodb java api中检索数据 - Data retrieval from mongodb java api 如何使用find()显示检索数据的反馈? - How to show feedback of retrieval data using find()? 如何将sql数据检索转换为mongodb? - How to convert an sql data retrieval into mongodb? 如何使用来自 Mongo 和 PostgreSQL 的数据作为内存查找表? - How to use data from Mongo and PostgreSQL as in-memory lookup tables? 如何在 mongoDB 的嵌套数据中使用聚合 $lookup? - How to use aggregate $lookup in nested data from mongoDB? 如何使用查找从 2 个不同集合中获取数据以及如何在查找中添加一些条件以根据某些条件获取数据? - How to get the data from 2 different collections using lookup and how to add some condition in lookup to get the data based on some condition? 使用烧瓶pymongo从嵌套字段检索mongodb数据 - mongodb data retrieval from nested fields using flask pymongo MongoDB 聚合:如何重构数据以使查找数据是根数据但包含来自父级的一些数据? - MongoDB aggregation: How to restructure data so that the lookup data is root data but contains some data from the parent? 如何使用查找也通过查找出现的数据? - How to use lookup for data which appear by lookup too?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM