The $currentOp stage streams live operation documents from the server—running queries, writes, aggregations, idle cursors, and transaction sessions—so DBAs can diagnose slowness and connection load.
01
Live ops feed
One doc per operation.
02
admin database
Required run context.
03
First stage
Then $match filter.
04
secs_running
Spot slow queries.
05
allUsers
See every client op.
06
opid + killOp
Terminate if needed.
Fundamentals
Definition and Usage
$currentOp is an aggregation stage that reports what MongoDB is doing right now. Unlike stages that read collection data, it reads internal server state and outputs diagnostic documents you can filter with normal pipeline stages.
💡
Beginner tip
Run on admin, not on your app database: db.getSiblingDB("admin").aggregate([{ $currentOp: {} }]). MongoDB recommends $currentOp over the deprecated db.currentOp() method because you can add $match, $project, and $sort afterward.
Use $currentOp to find long-running queries, inspect who holds connections to a collection, detect idle transaction sessions, and gather opid values before calling db.killOp().
Foundation
📝 Syntax
$currentOp takes an options object (all fields optional):
secs_running exists only when active is true. Sort descending to see the worst offenders first. Save opid if you must call killOp after confirming the query is safe to stop.
Example 3 — Operations on a specific collection
mongosh
db.getSiblingDB("admin").aggregate([
{ $currentOp: { allUsers: true } },
{
$match: {
ns: "shop.orders",
active: true
}
},
{
$project: {
op: 1,
secs_running: 1,
client: 1,
effectiveUsers: 1,
command: 1
}
}
])
/* Shows every active op touching shop.orders —
useful when one collection suddenly spikes CPU. */
type: "idleSession"
active: false
transaction.timeOpenMicros → how long open
locks → what the session still holds
How It Works
Idle sessions are transactions waiting on the client—not actively executing but still holding locks. Long timeOpenMicros values often mean an app forgot to commit or abort.
Compare
📋 $currentOp vs related admin tools
Tool
Effect
Best for
$currentOp stage
Operation stream + pipeline filtering
Slow query dashboards, custom admin reports
db.currentOp()
Legacy helper (deprecated 6.2+)
Quick shell glance—prefer $currentOp
db.killOp(opid)
Terminate one operation by id
After identifying op via $currentOp
Database profiler
Historical completed operations
Post-mortem analysis, not live state
mongotop / Atlas metrics
Time spent per collection
High-level heat maps, not per-query detail
🧠 How $currentOp Works
1
Admin aggregate starts
Client runs admin.aggregate([{ $currentOp: options }]) with optional flags.
Connect
2
Server collects ops
MongoDB gathers active ops, idle connections/cursors/sessions per your options.
Collect
3
Pipeline filters
$match, $project, $group shape the diagnostic stream like normal data.
Filter
=
⚙️
Actionable ops list
DBA sees slow queries, hot namespaces, or stale transactions—and can act.
Important
📝 Notes
Run on admin—pipelines starting with $currentOp fail on other databases.
allUsers: true needs the inprog privilege when access control is enabled.
secs_running appears only when active: true—idle sessions use transaction timing fields instead.
On sharded clusters, add localOps: true on mongos for a unified transaction view.
targetAllNodes: true returns one doc per data-bearing node (not just per shard).
$currentOp is MongoDB’s pipeline-native way to see what the server is doing right now. Start on admin, enable allUsers for ops visibility, filter with $match, and use secs_running to catch slow queries early.
Treat killOp as a last resort after identifying client-owned operations. Next in the series: $facet for parallel sub-pipelines.
Always use db.getSiblingDB("admin") for $currentOp pipelines
Add $project to trim noisy fields before returning to dashboards
Sort by secs_running descending when hunting slow queries
Check idle transaction sessions when locks block unrelated writes
Grant inprog only to trusted admin roles
❌ Don’t
Run $currentOp on application databases—it must be admin
Call killOp on operations you have not verified
Assume db.currentOp() is the modern approach—it is deprecated
Use inside multi-document transactions
Poll $currentOp in tight loops in production—rate-limit monitoring queries
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $currentOp
Use these points when monitoring MongoDB operations.
5
Core concepts
⚙️01
Live ops
Stream of docs.
Basics
🔒02
admin DB
Required context.
Setup
⏱️03
secs_running
Slow query hunt.
Perf
👥04
allUsers
Full cluster view.
Option
🚫05
opid
killOp carefully.
Action
❓ Frequently Asked Questions
$currentOp returns a stream of documents describing in-progress and dormant operations—queries, writes, aggregations, idle cursors, and inactive transaction sessions. Each document represents one operation or session. It replaces the deprecated db.currentOp() helper.
Always run the pipeline on the admin database: db.getSiblingDB("admin").aggregate([{ $currentOp: {} }, …]). $currentOp must be the first stage and cannot start on a regular user database.
allUsers (see all users' ops), idleConnections, idleCursors, idleSessions (default true), localOps (mongos-only), and targetAllNodes (sharded). An empty object { $currentOp: {} } uses all 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 operation documents include opid. Pass it to db.killOp(opid) in mongosh—but only terminate client-initiated ops you understand; killing internal server operations can destabilize the node.
On deployments with access control enabled, the inprog privilege is required on sharded clusters. On standalone/replica sets, inprog is required when allUsers: true. Without it you may only see your own operations.
Did you know?
MongoDB deprecated the currentOp command and db.currentOp() in favor of $currentOp because the aggregation form supports $match, $group, and $sort—turning raw ops dumps into focused admin reports. See the official $currentOp docs.