MongoDB $ lookup管道:这是否使用索引?

问题描述 投票:0回答:1

我有一个查询,它使用$ lookup的管道功能,它也使用$ expr。很好,它可以工作,但是性能不是很好。它在大约4000个文档的集合中查找内容,并加入其他2个集合(使用$ lookup块)。即使每个集合中只有几千个文档,运行仍需要2000毫秒。

查询看起来像这样:

            {
                $match: {
                   language: 'str'
                }
            },
            {
                $lookup: {
                    from: 'somecollection',
                    let: { someId: '$someId' },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        {
                                            $eq: [
                                                '$_id',
                                                '$$someId'
                                            ]
                                        },
                                        {
                                            $gte: ['$field',value]
                                        },
                                        {
                                            $lte: ['$field2',value]
                                        }
                                       ....
                                       // some more conditions..

                                    ]
                                }
                            }
                        }
                    ]

正在运行的explain()仅提供有关第一个$ match块的信息。但是如何确定管道中的$ expr是否正在使用索引?

[我试图将索引添加到管道中使用的所有字段,并且我还尝试创建复合索引,但是无法使其更快。

我如何提高性能?

我的查询结构:

匹配(按语言过滤),查找(col1加入)查找(col2连接)项目,排序

我正在使用Mongo 4.0

提前感谢!

mongodb join aggregate pipeline
1个回答
0
投票
{$lookup:
 {
   from: <collection to join>,
   localField: <field from the input documents>,
   foreignField: <field from the documents of the "from" collection>,
   as: <output array field>
 }

}

© www.soinside.com 2019 - 2024. All rights reserved.