MongoDB $currentOp Stage

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $currentOp?

$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()
UsePrefer
Live ops / slow queries$currentOp
Collection size / index bytes$collStats
Index usage since restart$indexStats

How to Use $currentOp in MongoDB

Run on admin, put $currentOp first, then filter and project.

  1. Switch to admin: db.getSiblingDB("admin").
  2. Start with { $currentOp: { allUsers: true } } (or {} for defaults).
  3. Filter with $match (e.g. active: true, secs_running).
  4. Trim fields with $project; sort/limit for readable output.
OptionDefault ideaRole
allUsersfalseInclude other users’ operations
idleConnectionsfalseInclude idle connection entries
idleCursorsfalseInclude idle cursors
idleSessionstrueInclude 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?, … }

Cheat Sheet

Keep these facts handy while debugging live MongoDB load.

TopicDetail
TypeAggregation stage (admin diagnostics)
DatabaseMust run on admin
PositionMust be first in the pipeline
Key fieldsop, ns, active, secs_running, opid, client
Slow filteractive: true + secs_running: { $gte: N }
Killdb.killOp(opid) — use carefully
PrivilegeOften needs inprog for allUsers

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.

Result
{ op: "query", ns: "shop.orders", secs_running: 47, active: true }
{ op: "insert", ns: "shop.orders", secs_running: 0, active: true }

Worked Walkthrough — Find a Slow Query

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)

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.

mongosh
db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true } },
  { $match: { active: true } },
  {
    $project: {
      _id: 0,
      op: 1,
      ns: 1,
      secs_running: 1,
      client: 1,
      desc: 1
    }
  },
  { $limit: 20 }
])

How It Works

  1. Admin + stage 1. $currentOp must run first on admin.
  2. Live snapshot. MongoDB emits one document per operation it is tracking now.
  3. Filter active. active: true drops idle connections and dormant rows.
  4. Readable output. $project and $limit keep the list short; run a real query elsewhere to see more rows.

Example 2 — Slow Queries

Goal: after Example 1, find active queries/commands running for at least 5 seconds, worst first. Save opid if you must stop one.

mongosh
db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true } },
  {
    $match: {
      active: true,
      secs_running: { $gte: 5 },
      op: { $in: ["query", "command", "getmore"] }
    }
  },
  {
    $project: {
      op: 1,
      ns: 1,
      secs_running: 1,
      opid: 1,
      "command.filter": 1,
      "command.aggregate": 1
    }
  },
  { $sort: { secs_running: -1 } }
])

// Terminate only if you understand the impact:
// db.killOp(<opid>)

How It Works

  1. Require active. secs_running is meaningful on active ops.
  2. Threshold. $gte: 5 keeps only ops that feel “stuck.”
  3. Focus on reads/commands. The op list skips pure idle noise.
  4. Sort worst first. Highest secs_running rises to the top for triage.

Example 3 — Filter by Namespace

Goal: after Example 1, show active operations touching one hot collection (shop.orders).

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
    }
  }
])

// Whole database with a regex:
// { $match: { ns: /^shop\./, active: true } }

How It Works

  1. Zoom in. ns filters to one collection’s active work.
  2. See the caller. client and effectiveUsers help assign ownership.
  3. Inspect the command. The command field shows what the client asked for.
  4. 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.

Performance Notes

ApproachCostBest for
$currentOp + tight $matchSnapshot + filterSlow-query triage
$currentOp + idle flagsLarger result setConnection / session audits
Polling every secondExtra admin loadAvoid unless needed

Prefer narrow filters and $project so dashboards stay light. Enable idleConnections only when auditing connection pools.

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.

Next: $facet

Run several aggregation sub-pipelines in parallel and get one combined result document.

$facet tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful