简体   繁体   English

Mongodb查询花费太长时间

[英]Mongodb query taking too long

I'm trying to get the hang of Mongodb, and right now, I have a very small collection with only a few hundred records, from which I'd like to update some fields dynamically in the future with javascript. 我正在尝试摆脱Mongodb的困扰,现在,我有一个很小的集合,其中只有几百条记录,我希望以后可以使用javascript动态更新一些字段。 But, although right now there are too few documents, the below query and update takes a minimum of 24 secs. 但是,尽管目前文件太少,但是下面的查询和更新至少需要24秒。 I don't think it's gonna be any use to me once we go into production and have loads of documents to update. 我认为一旦我们投入生产并有大量文档需要更新,对我来说就没有任何用处。 Anyone could help me with what is wrong with my code below? 任何人都可以帮助我解决下面的代码有什么问题吗?

Thanks in advance 提前致谢

var d = new Date();
var sapm = 1.8;
i = 0;
db.getCollection('content').find().forEach(function(x) {

    var window = (d - x.updated_at) / (24 * 60 * 60 * 1000);
    var feed = db.content.find({}, {
        _id: x._id
    });
    var comment = x.total_comment || 0;
    var up = x.total_up || 0;
    var interaction = up + (comment / 2);
    var hourage = (d - (x.updated_at));
    var rank = interaction / Math.pow((hourage - 4) * sapm);

    if (window <= 15) {
        db.content.update({
            '_id': x._id
        }, {

            $set: {
                "rank": rank,
            }
        }, {
            multi: true
        });
    }
});

Your query is fast, it's the updating each document individually that is taking time. 您的查询速度很快,这是单独更新每个文档所花费的时间。

You should look to push all docs to update into an array, then use something like async.js to save / update in parallel. 您应该将所有文档更新到一个数组中,然后使用async.js之类的东西来并行保存/更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM