Skip to content

command events are not fired after setting monitorCommands: true #14611

Closed

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.0

Node.js version

18.19.1

MongoDB server version

6.0.1

Typescript version (if applicable)

No response

Description

MongoDB dirver supports Command Monitoring

Problem: unable to subscribe for command monitoring events using mongoose

Steps to Reproduce

using mongoose@8.4.0
After following setup steps from the doc above, events are not emitted / reaching event listeners

const mongoose = require('mongoose');

const initMongoose = async () => {
  await mongoose.connect(
    'mongodb://localhost:27017/mydatabase',
    {
      monitorCommands: true,
    }
  );

  const db = mongoose.connection;
  db.on('commandStarted', event => console.log(event));
  db.on('commandFailed', event => console.log(event));
  db.on('commandSucceeded', event => console.log(event));
}

const User = mongoose.model('User', new mongoose.Schema({
  name: String,
  email: String,
  age: Number,
}));

const main = async () => {
  await initMongoose();
  await User.findOne()
  console.log('done!')
}

main()

Expected Behavior

but when using mongodb@6.6.2 directly which is installed together with mongoose@8.4.0
Events are fired and able to reach event listeners

const { MongoClient } = require("mongodb");

const client = new MongoClient(
  'mongodb://localhost:27017/mydatabase',
  { monitorCommands: true }
);

client.on('commandStarted', event => console.log(event));
client.on('commandFailed', event => console.log(event));
client.on('commandSucceeded', event => console.log(event));

async function main() {
  try {
    await client
      .db("mydatabase")
      .collection('users1')
      .findOne()
  } finally {
    await client.close();
  }
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions