Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
bobeZzz opened this issue May 24, 2024 · 3 comments
Closed
2 tasks done

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

bobeZzz opened this issue May 24, 2024 · 3 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@bobeZzz
Copy link

bobeZzz commented May 24, 2024

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()
@vkarpov15
Copy link
Collaborator

Use the following instead:

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

Connection.prototype.client contains the Mongoose connection's MongoClient instance, so that is what you should use to integrate with the MongoDB Node driver directly.

That being said, we will keep this issue open for 8.5 because it would by nice for Mongoose to bubble up the monitorCommands events onto the Mongoose connection.

@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label May 24, 2024
@vkarpov15 vkarpov15 added this to the 8.5 milestone May 24, 2024
@bobeZzz
Copy link
Author

bobeZzz commented May 26, 2024

thanks, works like a charm!
just an FYI mongoose.connection.client doesn't seem to be declared on TS side of things, but there is a method availabel to retrieve client instance mongoose.connection.getClient() which covers it.

image

@vkarpov15
Copy link
Collaborator

Yeah connection.getClient() is equivalent to connection.client

vkarpov15 added a commit that referenced this issue Jun 21, 2024
vkarpov15 added a commit that referenced this issue Jun 24, 2024
feat(connection): bubble up monitorCommands events to Mongoose connection if `monitorCommands` option set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

2 participants