【发布时间】:2020-11-07 03:27:54
【问题描述】:
我有两个收藏:
matches:
[{
date: "2020-02-15T17:00:00Z",
players: [
{_id: "5efd9485aba4e3d01942a2ce"},
{_id: "5efd9485aba4e3d01942a2cf"}
]
},
{...}]
和players:
[{
_id: "5efd9485aba4e3d01942a2ce",
name: "Rafa Nadal"
},
{
_id: "5efd9485aba4e3d01942a2ce",
name: "Roger Federer"
},
{...}]
我需要使用查找管道,因为我正在构建一个带有递归函数的 graphql 解析器,并且我需要嵌套查找。我已经按照这个例子https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage#nested-example
我的问题是管道查找需要 11 秒,但基本查找只需 0.67 秒。而且我的测试数据库很短!大约 1300 名玩家和 700 场比赛。
这是我的管道查找(11 秒解决)
db.collection('matches').aggregate([{
$lookup: {
from: 'players',
let: { ids: '$players' },
pipeline: [{ $match: { $expr: { $in: ['$_id', '$$ids' ] } } }],
as: 'players'
}
}]);
这是我的基本查找(0.67 秒解决)
db.collection('matches').aggregate([{
$lookup: {
from: "players",
localField: "players",
foreignField: "_id",
as: "players"
}
}]);
为什么差别这么大?我可以通过什么方式进行更快的管道查找?
【问题讨论】:
标签: mongodb