【发布时间】:2020-03-12 21:11:16
【问题描述】:
我有一个集合 A 和一些字段 a,b,c,d...
如果我这样做:
db.A.aggregate([{$match: {a: true, b: false, c: false}, {$limit: 50}]) >> 这很快(0.1s)
如果我这样做了
db.A.aggregate(
[
{
$lookup: {
from: 'B',
localField: 'b',
foreignField: '_id',
as: 'b'
}
},
{$match: {'b.d': true}},
{ $limit: 50 }
]
)
这大约需要 1.5s(文件很多,1.5s 我没问题)
现在,如果我像以前一样做,只是添加 $match(应该使用索引...):
db.A.aggregate(
[
{$match: {a: true, b: false, c: false}},
{
$lookup: {
from: 'B',
localField: 'b',
foreignField: '_id',
as: 'b'
}
},
{$match: {'b.d': true}},
{ $limit: 50 }
]
)
这需要 10 秒?我很困惑为什么。
PS:我对所有这些字段都有索引。
【问题讨论】:
标签: mongodb aggregation-framework aggregate