mongoose聚合阻止节点服务器被阻止

时间:2017-03-28 11:18:23

标签: node.js mongodb asynchronous mongoose nonblocking

我的收藏中有几十万个文档,每个文档都有一个timestamp字段。

我想计算上个月每天的记录数量。

我正在运行一个mongoose聚合命令来执行此操作,不幸的是,这比我期望的要长。

以下是聚合函数:

function dailyStats()
{
    var lastMonth = new Date();
    lastMonth.setMonth(lastMonth.getMonth() - 1);

    MyModel.aggregate(
        {
            $match: {timestamp: {$gte: lastMonth}}
        },
        {
            $group: {
                // Data count grouped by date
                _id: {$dateToString: {format: "%Y-%m-%d", date: "$timestamp"}},
                count: {$sum: 1}
            }
        },
        {$sort: {"_id": 1}},
        function (err, docs)
        {
            console.log(docs);
        });
}

现在,整个回调点都是非阻塞代码。但是,执行此功能时,大约需要20-25秒。在这个过程中,我的节点应用程序不响应其他API!

首先,我认为我的CPU变得如此繁忙,以至于它没有响应其他任何东西。所以我有一个小节点应用程序随之运行,这很好用! 所以我不明白为什么这个应用程序不会响应其他请求,直到mongodb驱动程序返回结果。

0 个答案:

没有答案
相关问题