Categories
JavaScript Answers

How to log all queries that Mongoose fire in the application with JavaScript?

Spread the love

To log all queries that Mongoose fire in the application with JavaScript, we call the set method.

For instance, we write

mongoose.set("debug", (collectionName, method, query, doc) => {
  console.log(`${collectionName}.${method}`, JSON.stringify(query), doc);
});

to call set with a callback to log the values from the query.

We log the collectionName collection, method that’s called, the query made, and the doc results returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *