MongoDB - How can I get the size of a collection using Node.js?
.count()
is an asynchronous function, just like .find()
is.
Try the following code:
collection.count({}, function(error, numOfDocs) {
console.log('I have '+numOfDocs+' documents in my collection');
// ..
});
The first argument to .count()
should be a search query. Since you want to count all documents, I'm passing an empty object {}
as a search query.