question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre count hook not being called with countDocuments()

See original GitHub issue

Since 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:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
lineuscommented, Aug 25, 2018

@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

#!/usr/bin/env node
'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;

var hookCalled = false; 

const schema = new Schema({
  name: String
});

schema.pre('countDocuments', function() {
  hookCalled = true;
});

const Test = mongoose.model('test', schema);

const tests = [];

for(let i = 0; i < 10; i++) {
  tests.push(new Test({ name: `name${i}`}));
}

async function run() {
  await conn.dropDatabase();
  await Test.create(tests);
  assert.strictEqual(hookCalled, false);
  await Test.countDocuments();
  assert.strictEqual(hookCalled, true);
  console.log(`countDocuments pre-hook called on version ${mongoose.version}!`);
  return conn.close();
}

run();

Output:

issues: for i in 2 3 4 5 6 7 8 9;do npm install mongoose@5.2.${i} >/dev/null 2>&1 && ./6776.js;done
countDocuments pre-hook called on version 5.2.2!
countDocuments pre-hook called on version 5.2.3!
countDocuments pre-hook called on version 5.2.4!
countDocuments pre-hook called on version 5.2.5!
countDocuments pre-hook called on version 5.2.6!
countDocuments pre-hook called on version 5.2.7!
countDocuments pre-hook called on version 5.2.8!
countDocuments pre-hook called on version 5.2.9!
issues:
1reaction
vkarpov15commented, Jul 30, 2018

Use pre('countDocuments') instead

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found