Mongoose: How to use count() ?

Created on 28 Jun 2010  路  5Comments  路  Source: Automattic/mongoose

I tried many things but I can't make count() work.
Based on the documentation, it is supposed to work this may:
User.find({username: 'john'}).count()

I've got this error:
DEBUG: TypeError: undefined is not a function
at CALL_NON_FUNCTION (native)
at /lib/mongoose/lib/model.js:177:9

I've also tried with
User.find({username: 'john'}).count(fn(r) { sys.debug(r); })
Or
User.count();
...

Thanks for your support.

Most helpful comment

I've solved the issue.
The correct code is:
User.count({},function(count) { } )

All 5 comments

The error up here is where I test with User.count(), with User.find().count() I have the following error:
Error: Object [object Object] has no method 'count'

I've solved the issue.
The correct code is:
User.count({},function(count) { } )

I encountered with the same problem, the correct code is:

User.count({},function(err, count) { } )

If you omit the err parameter the count result will be always null (if no error occured).

DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead

Was this page helpful?
0 / 5 - 0 ratings