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 [email protected]
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 [email protected]
directly which is installed together with [email protected]
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()