0
0
MongodbHow-ToBeginner · 3 min read

How to Check Current Operations in MongoDB Quickly

Use the db.currentOp() command in MongoDB to see all current operations running on the database. This command returns details like operation type, running time, and query info, helping you monitor active tasks.
📐

Syntax

The db.currentOp() command returns information about operations currently running on the MongoDB server.

  • db.currentOp(): Lists all current operations.
  • You can pass a filter object to narrow down results, e.g., db.currentOp({"active": true}) to show only active operations.
mongodb
db.currentOp()
💻

Example

This example shows how to list all current operations and then filter to only active ones.

mongodb
/* List all current operations */
db.currentOp()

/* List only active operations */
db.currentOp({"active": true})
Output
{ "inprog": [ { "opid": 12345, "active": true, "secs_running": 10, "op": "query", "ns": "test.collection", "query": {"field": "value"} }, { "opid": 12346, "active": false, "op": "command", "ns": "admin.$cmd" } ] }
⚠️

Common Pitfalls

One common mistake is expecting db.currentOp() to show only active operations by default; it shows all operations including idle ones.

Another pitfall is not having the required privileges; you need appropriate permissions to run db.currentOp().

mongodb
/* Wrong: expecting only active ops without filter */
db.currentOp()

/* Right: filter for active operations */
db.currentOp({"active": true})
📊

Quick Reference

CommandDescription
db.currentOp()Lists all current operations on the server
db.currentOp({"active": true})Lists only active (running) operations
db.currentOp({"secs_running": {"$gt": 5}})Lists operations running longer than 5 seconds
db.killOp(opid)Terminates the operation with the given opid

Key Takeaways

Use db.currentOp() to view all current operations in MongoDB.
Add filters like {"active": true} to see only running operations.
You need proper permissions to run db.currentOp().
Use db.killOp(opid) to stop a problematic operation.
Remember db.currentOp() shows both active and idle operations by default.