QA
Intermediate LevelMongoDB & Mongoose

How do db.currentOp() and db.killOp() diagnose and terminate long-running queries?

❓ Interview Prompt: Explain db.currentOp(), identifying un-indexed long-running queries, microsecond execution times, and db.killOp(opid).

Diagnosing stuck or slow queries relies on administrative diagnostic commands:

•
'db.currentOp()': Displays active database operations and execution durations.
•
'db.killOp(opid)': Aborts a specific running operation by its 'opid'.

Code Implementation Example

typescript
import mongoose from 'mongoose';

async function killSlowQueries() {
  const adminDb = mongoose.connection.db!.admin();
  const inprog = await adminDb.command({ currentOp: 1, secs_running: { $gt: 10 } });

  for (const op of inprog.inprog) {
    console.log('Killing slow op: ${op.opid}');
    await adminDb.command({ killOp: 1, op: op.opid });
  }
}
💡Diagnosing and killing queries running longer than 10 seconds.
🎯 Interview Key Takeaway

Use currentOp() to locate un-indexed queries and killOp() to terminate operations stalling the cluster.

#currentOp#killOp#Diagnostics#Intermediate
Back to all MongoDB & Mongoose Questions→

More MongoDB & Mongoose Interview Questions

Beginner LevelMongoDB & Mongoose

How does the MongoDB Aggregation Pipeline work? Explain $match, $group, $lookup, and $project.

Beginner LevelMongoDB & Mongoose

What is MongoDB and how does the BSON document data model work?

Beginner LevelMongoDB & Mongoose

What is the structure of a MongoDB ObjectId and how is it generated?