Node.js MongoDB driver pointer not counting correctly
I am having problems using MongoDB Node.js native driver version 2.2.29.
This is the code I am running:
let cursor = db.collection( 'log' )
.find({timestamp: { '$lte': 1498556839 }})
.sort( { create_date_ttl: -1 } )
.limit( 3 );
If I now run cursor.count()
and process the Promise, I see that the count gives me 56 records instead of 3 (the specified limit).
cursor.count().then( (count) => {
// count here is 56
});
However, if I run cursor.count( function (err, count) {})
with callbacks, the counter is only correct with 3 entries.
cursor.count( function (err, count) {
// count here is 3 according to the limit specified.
});
Does anyone have the same problem or can someone explain to me how this is possible? Maybe I am missing something, but it seems to be ok with the official documentation .
Thanks in advance.
source to share
The explanation sets the first argument ( applySkipLimit
) to true and then skip
and limit
will be appended.
cursor.count(true).then( (count) => {
// count here will be 3
});
The documentation doesn't seem to be up to date because it is written that true should be the default. As mentioned in the comment, this is callback behavior.
source to share