{"library":"mongoose-profiling-middleware","title":"Mongoose Profiling Middleware","description":"mongoose-profiling-middleware is a utility package for Node.js applications that integrates with Mongoose to leverage MongoDB's native profiling capabilities. It automatically injects the current source file and line number as a `$comment` into every Mongoose query executed against a MongoDB instance. This feature allows developers to easily identify the exact code location responsible for slow or frequent database operations when analyzing the MongoDB `system.profile` collection. The current stable version, 1.0.2, offers a straightforward, side-effect-based integration. This middleware simplifies the process of identifying performance bottlenecks by automating query annotation, distinguishing it from manual comment insertion. Its primary function is to provide immediate context for database query performance, thereby aiding optimization efforts without requiring significant application code changes.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install mongoose-profiling-middleware"],"cli":null},"imports":["require('mongoose-profiling-middleware');","import 'mongoose-profiling-middleware';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const mongoose = require('mongoose');\n\nasync function run() {\n  try {\n    await mongoose.connect('mongodb://localhost:27017/myprofiledb', {\n      useNewUrlParser: true,\n      useUnifiedTopology: true,\n    });\n    console.log('MongoDB connected.');\n\n    const MySchema = new mongoose.Schema({\n      name: String,\n      value: Number,\n    });\n    const MyModel = mongoose.model('MyItem', MySchema);\n\n    // IMPORTANT: Require the middleware AFTER mongoose is connected and models are defined.\n    require('mongoose-profiling-middleware');\n\n    console.log('Mongoose profiling middleware loaded.');\n    console.log('To see profiling data, ensure you run: db.setProfilingLevel(1) or db.setProfilingLevel(2) in your mongo shell for the \"myprofiledb\" database.');\n\n    // Perform some Mongoose operations that will be profiled\n    await MyModel.create({ name: 'TestItemA', value: 100 });\n    await MyModel.findOne({ name: 'TestItemA' });\n    await MyModel.updateOne({ name: 'TestItemA' }, { value: 200 });\n    await MyModel.find({});\n    await MyModel.deleteOne({ name: 'TestItemA' });\n\n    console.log('Example Mongoose operations completed. Check the system.profile collection in MongoDB for details.');\n\n  } catch (error) {\n    console.error('Error during Mongoose operations:', error);\n  } finally {\n    await mongoose.disconnect();\n    console.log('MongoDB disconnected.');\n  }\n}\n\nrun();","lang":"javascript","description":"This quickstart demonstrates how to set up Mongoose, load the profiling middleware, and execute several Mongoose operations that will be annotated with source code comments for MongoDB profiling.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}