Moi*_*hín 3 count mongodb node.js
我在使用MongoDB Node.js本机驱动程序版本2.2.29时遇到问题.
这是我正在运行的代码:
let cursor = db.collection( 'log' )
.find({timestamp: { '$lte': 1498556839 }})
.sort( { create_date_ttl: -1 } )
.limit( 3 );
Run Code Online (Sandbox Code Playgroud)
如果我现在运行cursor.count()并处理Promise,我看到计数给了我56条记录而不是3条(指定的限制).
cursor.count().then( (count) => {
// count here is 56
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我cursor.count( function (err, count) {})使用回调运行,则计数只有3条记录正确.
cursor.count( function (err, count) {
// count here is 3 according to the limit specified.
});
Run Code Online (Sandbox Code Playgroud)
有没有人有相同的问题或有人可以解释我这是怎么可能的?也许我错过了一些东西,但根据官方文档似乎没问题.
提前致谢.
明确地设置第一个参数(applySkipLimit)为true,然后skip和limit将appilied.
cursor.count(true).then( (count) => {
// count here will be 3
});
Run Code Online (Sandbox Code Playgroud)
似乎文档是未澄清的,因为有人写道,true应该是默认值.正如评论中所提到的,它是回调的行为.
| 归档时间: |
|
| 查看次数: |
350 次 |
| 最近记录: |