MongoDB在聚合组上的性能非常差

MongoDB在聚合组上的性能非常差,mongodb,Mongodb,我有这样的关系: [boards]1-n[threads]1-n[posts]n-1[users] 现在我想加载所有的板,每个板的线程数,每个板的每个帖子数 这是我的疑问: db.getCollection('boards').aggregate([ { $lookup : { from: "threads", localField:"_id", foreignField: "BoardId",

我有这样的关系:

[boards]1-n[threads]1-n[posts]n-1[users]

现在我想加载所有的板,每个板的线程数,每个板的每个帖子数

这是我的疑问:

db.getCollection('boards').aggregate([
    {
        $lookup : {
            from: "threads", 
            localField:"_id", 
            foreignField: "BoardId",
            as: "threads"
        }
    },
    { $unwind : "$threads" },
    {
        $lookup:{
            from: "posts",
            localField:"threads._id", 
            foreignField: "ThreadId",
            as: "threads.posts"
        }
    },
    { 
        $group: {
            _id: "$_id",
            threadCount: { $sum : 1},
            postCount : { $sum: { $size: "$threads.posts" }}
        }
    }
])
这是难以置信的缓慢。大约8秒(50块板,100k线,1M柱)。 通常情况下,我希望每个查询加载更多的数据,但为什么这么慢?只是因为团队


我们希望使用不同的集合,因为有专用标识符,而MongoDB不支持子文档的唯一标识符。

您是否了解了有关查询计划的信息,以及索引是否有帮助?@VinceBowdren$组不使用索引,我认为这是主要问题。因为没有$group,速度相当快。但是如何让这个查询运行得更快…并使用其他任何东西而不是$group?