Node.js MongoDB 驱动游标计数不正确

标签 node.js mongodb count

我在使用 MongoDB Node.js native 驱动程序版本 2.2.29 时遇到问题。

这是我正在运行的代码:

let cursor = db.collection( 'log' )
               .find({timestamp: { '$lte': 1498556839 }})
               .sort( { create_date_ttl: -1 } )
               .limit( 3 );

如果我现在运行 cursor.count() 并处理 Promise,我会看到计数为我提供了 56 条记录而不是 3 条(指定的限制)。

cursor.count().then( (count) => {
   // count here is 56
});

但是,如果我使用回调运行 cursor.count( function (err, count) {}),则只有 3 条记录是正确的。

cursor.count( function (err, count) {
  // count here is 3 according to the limit specified.
});

有没有人遇到同样的问题或者有人可以解释我这怎么可能?也许我遗漏了什么,但根据官方的说法似乎没问题documentation .

提前致谢。

最佳答案

明确设置第一个参数 (applySkipLimit) 为真,然后 skiplimit 将被应用。

cursor.count(true).then( (count) => {
   // count here will be 3
});

似乎文档没有明确说明,因为写着 true 应该是默认值。如评论中所述,这是回调的行为。

关于Node.js MongoDB 驱动游标计数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44777576/

相关文章:

MySQL COUNT 查询仅从 CASE 表达式返回一个值

python - 使用 python 聚合数据

node.js - 在 Visual Studio 2015 for Angular2 上启动项目

node.js - 无法使用 NodeJS/node-outlook 访问 Office 365

javascript - 使用 mongodb 收集 meteor 时出现重复的键

mongodb - 与单线程客户端相比,为什么 MongoDB 使用多线程客户端性能更好?

mysql - 如何在 mySQL 表中计算 10 个不同的 IP

node.js - 使用 ES6 Proxy 和 node.js 的非法调用错误

javascript - html5up! meteor 条纹

MongoDb 将 $group '_id' 空值聚合为不同的