Reputation:
I'm very newbie on Javascript-NodeJS-MongoDB, I try to know the number of documents found by a query.
...
var page = req.params.page;
var db = require('mongojs').connect('localhost:27017/foo', ['bar']);
var docs = db.bar.find({x:'MME'}).sort({y:1}).skip(10*(page-1)).limit(10);
var nbDocs = db.bar.find({x:'MME'}).count(); /*docs.count();*/
console.log(nbDocs);
But unfortunately the log gives me 'undefined', the same if I code
var nbDocs = docs.count();
Thank's a lot for your precious help.
Gilles.
Upvotes: 19
Views: 13878
Reputation: 4021
According to the docs the result will be in the second argument of the callback to the cursor.count()
method. This might be a little more challenging to implement for somebody who's new to javascript, but I think something like this should work:
docs.count(function(error, nbDocs) {
// Do what you need the count for here.
});
Upvotes: 31