Pre count hook not being called with countDocuments()
See original GitHub issueSince count
method is deprecated, using countDocuments
seems not to fire the pre count hook.
let count = await Model.countDocuments({ id: 1});
schema.pre('count', function (next) {
console.log('About to count');
next();
});
Using count
instead of countDocuments
works fine though.
Mongoose: 5.2.2 Node.js: 10.0.0 MongoDB: 3.6.5
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Get model count in Mongoose pre hooks - Stack Overflow
How can these two callbacks be unified to get document count? I would prefer to stick to this and not hard-code a model,...
Read more >Why does "estimatedDocumentCount()" get a different result ...
The estimatedDocumentCount() result is based on metadata for the count of all documents in a collection. As noted in the documentation @ ...
Read more >Mongoose v6.8.1: Middleware
Middleware (also called pre and post hooks) are functions which are passed ... Query middleware executes when you call exec() or then() on...
Read more >Cloud Firestore triggers | Cloud Functions for Firebase
With Cloud Functions, you can handle events in Cloud Firestore with no need to ... when a document is updated using the onUpdate()...
Read more >Mongoose Middleware | The Javascript
When you call `doc.save()`, Mongoose calls your pre save middleware ... is a static function on the model class, not a method like...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@adeelhussain what version of mongoose are you using? this script and it’s output show that it works on at least 5.2.2 ( the version mentioned in this bug ) to the current version:
6776.js
Output:
Use
pre('countDocuments')
instead