As stated in the mongoose documentation and in Benjamin's answer, the Model.count () method is deprecated. Instead of using count (), the following options are possible:
Model.countDocuments (filterObject, callback)
Counts how many documents match the filter in the collection. Passing an empty object {} as a filter performs a full scan of the collection. If the collection is large, you can use the following method.
Model.estimatedDocumentCount ()
This model method estimates the number of documents in the MongoDB collection. This method is faster than the previous countDocuments (), because it uses collection metadata, rather than scrolling through the entire collection. However, as the name of the method implies, and depending on the configuration of the database, the result is estimated, since metadata may not reflect the actual number of documents in the collection at the time the method was executed.
Both methods return a mongoose request object, which can be performed in one of the following two ways. Use .exec () if you want to execute the request later.
1) pass callback function
For example, count all documents in a collection using .countDocuments ():
SomeModel.countDocuments({}, function(err, count) { if (err) { return handleError(err) }
Or count all the documents in the collection that have a specific name using .countDocuments ():
SomeModel.countDocuments({ name: 'Snow' }, function(err, count) {
2) Use .then ()
The mongoose request has .then (), so it can be used. This is for convenience, and the request itself is not a promise.
For example, count all documents in a collection using .estimatedDocumentCount ():
SomeModel .estimatedDocumentCount() .then(count => { console.log(count) //and do one super neat trick }) .catch(err => { //handle possible errors })
Hope this helps!