【问题标题】:Can't get limit to work in mongojs nodejs无法限制在 mongojs nodejs 中工作
【发布时间】:2017-10-22 04:57:36
【问题描述】:

我的代码只有在我删除限制函数时才有效,我做错了什么?

var db = mongojs('yansite', ['yansite']);
(function () {
    var bulk = db.ips.initializeOrderedBulkOp();
    bulk.find({}).limit(2).remove();
    bulk.execute(function (err, res) {
        console.log('Done!')
    });
})();

错误是:

TypeError: bulk.find(...).limit 不是函数 在 D:\nodeprojects\mysite\server.js:281:19 在对象。 (D:\nodeprojects\mysite\server.js:285:3) 在 Module._compile (module.js:570:32) 在 Object.Module._extensions..js (module.js:579:10) 在 Module.load (module.js:487:32) 在 tryModuleLoad (module.js:446:12) 在 Function.Module._load (module.js:438:3) 在 Module.runMain (module.js:604:10) 运行时(bootstrap_node.js:390:7) 启动时 (bootstrap_node.js:150:9)

在 mongojs github README 中显示了这个例子:

db.mycollection.find({}).limit(2).skip(1, function (err, docs) { ... })

【问题讨论】:

  • limit 不能与remove 一起使用。可能是stackoverflow.com/questions/19065615/…
  • 我只是尝试不使用批量并找到限制是否工作必须是批量不的一种方式?,我看到该解决方案我只是不知道它在返回的地方是如何工作的,所以我可以删除? , removeIdsArray 里面没有设置
  • find limit 工作,当然,但不是 remove。
  • 需要删除

标签: node.js mongodb mongojs


【解决方案1】:

我做到了,但有点丑,我希望有办法,很难相信没有

(function () {
    db.usersChatMessages.find({}).limit(2, function (err, doc) {
        console.log('Done!');
        var docIds = [];
        for (var i=0; i < doc.length; i++) {
            docIds.push(doc[i]._id);
        }
        db.usersChatMessages.remove({_id: {$exists: true, $in: docIds}}, function (err, doc) {
            console.log('removed!');
        });
    });
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2012-11-19
    • 1970-01-01
    • 2021-05-16
    • 2018-04-26
    相关资源
    最近更新 更多