use*_*996 6 pipeline join aggregate mongodb
我有一个查询,它利用 $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..
]
}
}
}
]
Run Code Online (Sandbox Code Playgroud)
对此运行解释()仅提供有关第一个 $match 块的信息。但是如何判断管道中的 $expr 是否使用索引呢?
我尝试向管道中使用的所有字段添加索引,并且还尝试创建复合索引,但我无法使其更快。
我怎样才能提高性能?
我的查询的结构:
match (filter by language),
lookup (col1 join)
lookup (col2 join)
project,
sort
Run Code Online (Sandbox Code Playgroud)
我正在使用蒙戈4.0
编辑:
实际上,管道中的操作可以使用索引吗?我听说只有 $eq 能够使用索引,其他操作不能。这是真的???
提前致谢!
小智 -3
{$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
Run Code Online (Sandbox Code Playgroud)