$currentOp is a MongoDB aggregation stage that streams live operation documents from the server—running queries, writes, aggregations, and related sessions—so you can diagnose slowness.
Remember
$currentOp = "what is MongoDB doing right now?"
db.getSiblingDB("admin").aggregate([
{ $currentOp: { allUsers: true } },
{ $match: { active: true } }
])
must run on admin database
must be the FIRST stage
allUsers: true → see everyone's ops (needs privilege)
secs_running → how long an active op has run
opid → use with db.killOp() carefully
Prefer this over deprecated db.currentOp()
Run on admin, put $currentOp first, then filter and project.
Switch to admin: db.getSiblingDB("admin").
Start with { $currentOp: { allUsers: true } } (or {} for defaults).
Filter with $match (e.g. active: true, secs_running).
Trim fields with $project; sort/limit for readable output.
Option
Default idea
Role
allUsers
false
Include other users’ operations
idleConnections
false
Include idle connection entries
idleCursors
false
Include idle cursors
idleSessions
true
Include inactive sessions
Pseudocode
Pseudocode
assert: pipeline runs on admin
assert: $currentOp is stage 1
ops = snapshot of current server operations
(respect allUsers / idle* options)
for each op in ops:
if later $match fails → skip
emit op document
{ op, ns, active, secs_running?, opid?, client?, … }
Reference
Cheat Sheet
Keep these facts handy while debugging live MongoDB load.
Topic
Detail
Type
Aggregation stage (admin diagnostics)
Database
Must run on admin
Position
Must be first in the pipeline
Key fields
op, ns, active, secs_running, opid, client
Slow filter
active: true + secs_running: { $gte: N }
Kill
db.killOp(opid) — use carefully
Privilege
Often needs inprog for allUsers
Try it
Live Preview
Filter a sample snapshot of operations (teaching model—not a live MongoDB connection).
Sample ops include a slow query, a fast insert, and an idle connection.
Goal: list active operations that have been running for at least 5 seconds, longest first.
Calculation
1. Run on admin with $currentOp first
2. Keep rows where:
active === true
secs_running >= 5
op in [query, command, getmore]
3. Project op, ns, secs_running, opid
4. Sort by secs_running descending
Example hit:
{ op: "query", ns: "shop.orders",
secs_running: 47, opid: 12345 }
Optional next step (careful):
db.killOp(12345)
Code
MongoDB Examples
These pipelines run on the admin database and read live operation metadata from the server—not a seeded collection. Results depend on what is running when you execute them.
Example 1 — Setup / Prerequisites
Goal: on admin, snapshot operations that are active right now. Each row is one server operation (query, write, command, etc.)—not a document you inserted for this tutorial.
ns format: "databaseName.collectionName"
example: "shop.orders"
Use when one collection suddenly spikes CPU.
How It Works
Zoom in.ns filters to one collection’s active work.
See the caller.client and effectiveUsers help assign ownership.
Inspect the command. The command field shows what the client asked for.
Regex option./^shop\./ covers every collection in the shop database.
Edge Cases & Pitfalls
Check these before using $currentOp in production triage.
admin
Must run on the admin database
Starting $currentOp on an app database fails. Always use db.getSiblingDB("admin").
order
Must be the first stage
Put $match / $project after $currentOp, never before it.
kill
Be careful with killOp
Only kill client-initiated ops you understand. Stopping internal server operations can hurt the node.
priv
Privilege limits what you see
Without inprog (and related rights), allUsers: true may fail or show only your own ops.
Analysis
Performance Notes
Approach
Cost
Best for
$currentOp + tight $match
Snapshot + filter
Slow-query triage
$currentOp + idle flags
Larger result set
Connection / session audits
Polling every second
Extra admin load
Avoid unless needed
Prefer narrow filters and $project so dashboards stay light. Enable idleConnections only when auditing connection pools.
Remember
Key Takeaways
Role: see what MongoDB is doing right now.
Rules: run on admin; stage must be first.
Slow ops: filter active + secs_running.
opid: for killOp—only when you are sure.
One line:$currentOp answers “what is the server doing right now?”
Frequently Asked Questions
$currentOp returns documents describing in-progress and dormant operations—queries, writes, aggregations, idle cursors, and inactive sessions. Each document is one operation or session. Prefer it over the deprecated db.currentOp() helper.
Always run on admin: db.getSiblingDB("admin").aggregate([{ $currentOp: {} }, …]). $currentOp must be the first stage and cannot start on a regular app database.
Common ones: allUsers (see everyone’s ops), idleConnections, idleCursors, idleSessions (default true), plus localOps / targetAllNodes on sharded setups. An empty object { $currentOp: {} } uses defaults.
Chain $match after $currentOp: { active: true, secs_running: { $gte: 5 }, op: { $in: ["query", "command", "getmore"] } }. secs_running appears only on active operations.
Yes. Active docs include opid. Pass it to db.killOp(opid)—but only stop client ops you understand. Killing internal server work can destabilize the node.
With access control, the inprog privilege is often required—especially for allUsers: true or on sharded clusters. Without it you may only see your own operations.