【问题标题】:How to fetch/count millions of records in mongodb with nodejs如何使用nodejs在mongodb中获取/计算数百万条记录
【发布时间】:2017-08-16 00:48:57
【问题描述】:

我们在 mongoDB 中有一个包含数百万条记录的集合。它需要花费大量时间和时间来计算和创建这些记录的分页。使用 nodejs 最好的方法是什么。我想创建一个页面,在该页面中可以看到带有分页、计数、删除、搜索记录的记录。下面是不同条件下对 Mongo 进行查询的代码。

crowdResult.find({ "auditId":args.audit_id,"isDeleted":false})
            .skip(args.skip)
            .limit(args.limit)
            .exec(function (err, data) {
                if (err) 
                    return callback(err,null);
                console.log(data);
                return callback(null,data);
            })

【问题讨论】:

  • 在查询 docs.mongodb.org/manual/reference/method/cursor.skip 中使用 limitskip 顺便说一句,这不是一个好问题,您应该向我们展示您的实际代码以给您答案
  • 谢谢米歇尔姆。我们已经在使用它了。我已经用代码更新了问题。

标签: node.js mongodb heroku


【解决方案1】:

如果目标是在不超时的情况下通过大型数据集,那么我使用以下方法逐页获取页面,并在分页结果集可用时立即处理: https://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f

在深入研究之前,请关注以下几行以快速了解代码的作用:

  1. getPage()处理工作,您可以根据自己的喜好设置pageSizequeryhttps://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f#file-sample-js-L68
  2. 方法签名: https://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f#file-sample-js-L29
  3. pagedResults 可用时立即处理: https://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f#file-sample-js-L49
  4. 转到下一页: https://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f#file-sample-js-L53
  5. 当没有更多数据时,代码将停止: https://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f#file-sample-js-L41
  6. 或者在处理最后一页数据时会停止: https://gist.github.com/pulkitsinghal/2f3806670439fa137210fc26b134237f#file-sample-js-L46

我希望这能提供一些启发,即使它不能完全满足您的需求。

【讨论】:

    猜你喜欢
    • 2014-05-24
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2012-04-19
    相关资源
    最近更新 更多