{"id":20551414,"url":"https://github.com/marcosfede/mongoose-query-logger","last_synced_at":"2025-04-14T11:21:10.022Z","repository":{"id":52466536,"uuid":"303447746","full_name":"marcosfede/mongoose-query-logger","owner":"marcosfede","description":"Mongoose plugin for query execution logging and explaining","archived":false,"fork":false,"pushed_at":"2021-04-28T09:21:27.000Z","size":1767,"stargazers_count":7,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T00:41:18.446Z","etag":null,"topics":["explain","mongo","mongodb","mongoose","mongoose-queries","nodejs","schemas"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcosfede.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-12T16:19:09.000Z","updated_at":"2023-06-14T05:41:38.000Z","dependencies_parsed_at":"2022-09-20T14:54:09.888Z","dependency_job_id":null,"html_url":"https://github.com/marcosfede/mongoose-query-logger","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcosfede%2Fmongoose-query-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcosfede%2Fmongoose-query-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcosfede%2Fmongoose-query-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcosfede%2Fmongoose-query-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcosfede","download_url":"https://codeload.github.com/marcosfede/mongoose-query-logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248868767,"owners_count":21174758,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["explain","mongo","mongodb","mongoose","mongoose-queries","nodejs","schemas"],"created_at":"2024-11-16T02:30:44.393Z","updated_at":"2025-04-14T11:21:09.923Z","avatar_url":"https://github.com/marcosfede.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongoose Query Logger\n\n![CI](https://github.com/marcosfede/mongoose-query-logger/workflows/CI/badge.svg) \n![npm version](https://badgen.net/npm/v/mongoose-query-logger)\n![types](https://badgen.net/npm/types/mongoose-query-logger)\n![license](https://badgen.net/npm/license/mongoose-query-logger)\n![bundlephobia](https://badgen.net/bundlephobia/min/react)\n\nMongoose middleware to log your mongoose queries and execution timings.\n\nOptionally, it also logs index usage and warns you about full collection scans\n\n## Screenshots\n### Query logging and execution timing\n![Query](img/query.png)\n\n### Query Explain\n![Collscan](img/collscan.png)\n![IXScan](img/ixscan.png)\n\n\n## Installation\n\n```\nnpm install --save-dev mongoose-query-logger\n```\n\n## Usage\n\nApply the plugin to all schemas:\n\n```typescript\nimport { MongooseQueryLogger } from 'mongoose-query-logger';\n\nexport const queryLogger = new MongooseQueryLogger();\n \n// optionally add custom configuration eg:\n// queryLogger\n//    .setExplain(true)\n//    .setAdditionalLogProperties(true)\n//    .setQueryLogger(myCustomQueryLogger)\n//    .setExplainLogger(myCustomExplainLogger);\n\nmongoose.plugin(queryLogger.getPlugin());\n```\n\nApply the plugin to specific schemas:\n\n```typescript\nimport { MongooseQueryLogger } from 'mongoose-query-logger';\n\nexport const queryLogger = new MongooseQueryLogger();\n\n// optionally add custom configuration eg:\n// queryLogger\n//    .setExplain(true)\n//    .setAdditionalLogProperties(true)\n//    .setQueryLogger(myCustomQueryLogger)\n//    .setExplainLogger(myCustomExplainLogger);\n\nconst schema = new mongoose.Schema({\n  /* schema definition */\n});\n\nschema.plugin(queryLogger.getPlugin());\n\n// compile the model AFTER registering plugins\nconst User = mongoose.model('User', schema);\n```\n\n## Explain logging\n\nThis is turned off by default. It will fire an explain query for supported operations.\nTurn this on by calling: \n\n```plugin.setExplain(true)```\n\nwarning: don't use `explain` in production, it will run each query twice.\n\n## Supported query logging methods\nThe following methods are supported for query logging\n\n| method | supported |\n| --------------- | --------------- |\n| count | :heavy_check_mark: |\n| countDocuments | :heavy_check_mark: |\n| estimatedDocumentCount | :heavy_check_mark: |\n| find | :heavy_check_mark: |\n| findOne | :heavy_check_mark: |\n| findOneAndUpdate | :heavy_check_mark: |\n| findOneAndRemove | :heavy_check_mark: |\n| findOneAndDelete | :heavy_check_mark: |\n| findOneAndRemove | :heavy_check_mark: |\n| update | :heavy_check_mark: |\n| updateOne | :heavy_check_mark: |\n| updateMany | :heavy_check_mark: |\n| deleteOne | :heavy_check_mark: |\n| deleteMany | :heavy_check_mark: |\n| aggregate | :heavy_check_mark: |\n| remove |  |\n| insertMany |  |\n| distinct |  |\n\nIf you want only a subset of these to be logged, you can provide an array of supported methods like so:\n\n```\nplugin.setQueryMethods({targetMethods: ['find', 'aggregate']})\n```\n\n## Supported explain logging methods\nThe following methods are supported for query explaining\n\n| method | supported |\n| --------------- | --------------- |\n| find | :heavy_check_mark: |\n| findOne | :heavy_check_mark: |\n| aggregate | :heavy_check_mark: |\n\nIf you want only a subset of these to be logged, you can provide an array of supported methods like so:\n\n```\nplugin.setQueryMethods({explainMethods: ['find', 'aggregate']})\n```\n\n## Custom query logger\nYou can provide a custom logging function by calling `plugin.setQueryLogger(myCustomLogger)`\nThe logger should be a function that accepts a single argument of type object with the following keys:\n\n| key | type | description | example |\n| --------------- | --------------- | --------------- | --------------- |\n| operation | string | executed operation | find, aggregate |\n| collectionName | string | collection name | tasks |\n| executionTimeMS | number | query execution time in ms | 320ms |\n| filter | Object or null  | filter object provided to the query | {\"name\": \"john\"} |\n| fields | Object or null | projection fields | {\"name\": 1} |\n| options | any | query options | {\"sort\": \"name\"} |\n| update | Object or null |  |  |\n| additionalLogProperties | any | additional log options |  |\n\n\n## Custom explain logger\nYou can provide a custom explain logging function by calling `plugin.setExplainLogger(myCustomExplainLogger)`\nThe logger should be a function that accepts a single argument of type object with the following keys:\n\n| key | type | description |\n| --------------- | --------------- | --------------- |\n| queryPlanners | any[] | array of query execution plans as returned from mongodb |\n\n## Additional log properties\nYou can include additional metadata in your queries by turning this on with \n\n```plugin.setAdditionalLogProperties(true)```\n\nand using it like `await User.find({\"name\": \"john\"}).additionalLogProperties('something')`\n\n## Supported versions\n\nThis was tested under mongoose 4.4 and node.js \u003e= 12\n\n## License\n\nMIT © [Federico Marcos](http://github.com/marcosfede)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcosfede%2Fmongoose-query-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcosfede%2Fmongoose-query-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcosfede%2Fmongoose-query-logger/lists"}