Mongoose middleware to log your mongoose queries and execution timings.
Optionally, it also logs index usage and warns you about full collection scans
npm install --save-dev mongoose-query-logger
Apply the plugin to all schemas:
import { MongooseQueryLogger } from 'mongoose-query-logger';
export const queryLogger = new MongooseQueryLogger();
// optionally add custom configuration eg:
// queryLogger
// .setExplain(true)
// .setAdditionalLogProperties(true)
// .setQueryLogger(myCustomQueryLogger)
// .setExplainLogger(myCustomExplainLogger);
mongoose.plugin(queryLogger.getPlugin());
Apply the plugin to specific schemas:
import { MongooseQueryLogger } from 'mongoose-query-logger';
export const queryLogger = new MongooseQueryLogger();
// optionally add custom configuration eg:
// queryLogger
// .setExplain(true)
// .setAdditionalLogProperties(true)
// .setQueryLogger(myCustomQueryLogger)
// .setExplainLogger(myCustomExplainLogger);
const schema = new mongoose.Schema({
/* schema definition */
});
schema.plugin(queryLogger.getPlugin());
// compile the model AFTER registering plugins
const User = mongoose.model('User', schema);
This is turned off by default. It will fire an explain query for supported operations. Turn this on by calling:
plugin.setExplain(true)
warning: don't use explain
in production, it will run each query twice.
The following methods are supported for query logging
method | supported |
---|---|
count | ✔️ |
countDocuments | ✔️ |
estimatedDocumentCount | ✔️ |
find | ✔️ |
findOne | ✔️ |
findOneAndUpdate | ✔️ |
findOneAndRemove | ✔️ |
findOneAndDelete | ✔️ |
findOneAndRemove | ✔️ |
update | ✔️ |
updateOne | ✔️ |
updateMany | ✔️ |
deleteOne | ✔️ |
deleteMany | ✔️ |
aggregate | ✔️ |
remove | |
insertMany | |
distinct |
If you want only a subset of these to be logged, you can provide an array of supported methods like so:
plugin.setQueryMethods({targetMethods: ['find', 'aggregate']})
The following methods are supported for query explaining
method | supported |
---|---|
find | ✔️ |
findOne | ✔️ |
aggregate | ✔️ |
If you want only a subset of these to be logged, you can provide an array of supported methods like so:
plugin.setQueryMethods({explainMethods: ['find', 'aggregate']})
You can provide a custom logging function by calling plugin.setQueryLogger(myCustomLogger)
The logger should be a function that accepts a single argument of type object with the following keys:
key | type | description | example |
---|---|---|---|
operation | string | executed operation | find, aggregate |
collectionName | string | collection name | tasks |
executionTimeMS | number | query execution time in ms | 320ms |
filter | Object or null | filter object provided to the query | {"name": "john"} |
fields | Object or null | projection fields | {"name": 1} |
options | any | query options | {"sort": "name"} |
update | Object or null | ||
additionalLogProperties | any | additional log options |
You can provide a custom explain logging function by calling plugin.setExplainLogger(myCustomExplainLogger)
The logger should be a function that accepts a single argument of type object with the following keys:
key | type | description |
---|---|---|
queryPlanners | any[] | array of query execution plans as returned from mongodb |
You can include additional metadata in your queries by turning this on with
plugin.setAdditionalLogProperties(true)
and using it like await User.find({"name": "john"}).additionalLogProperties('something')
This was tested under mongoose 4.4 and node.js >= 12
MIT © Federico Marcos