Hi,
I need to collect a full list of current operations from all mongod nodes in different MongoDB cluster setups:
-
standalone
-
replica set (including primary and all secondary nodes)
-
sharded cluster (across all shards, including primary and all secondaries of each replica set)
From what I understand so far:
- In a replica set,
$currentOpreturns operations only from the node where it is executed. - In a sharded cluster,
$currentOpwhen run onmongosreturns operations only from the primary nodes of each shard.
Starting with MongoDB 7.1, the targetAllNodes (SERVER-34633) option was introduced. It’s supposed to collect operations from all nodes. It seems like targetAllNodes works only in sharded clusters, and fails if any secondary is in RECOVERING state.
Example (Replica Set)
db.aggregate([ { $currentOp: { allUsers: true, targetAllNodes: true } } ])
// MongoServerError[FailedToParse]: $currentOp supports targetAllNodes parameter only for sharded clusters
Example (Sharded Cluster)
db.aggregate([ { $currentOp: { allUsers: true, targetAllNodes: true } } ])
// MongoServerError[HostUnreachable]: node is not in primary or recovering state
I also came across this ticket: SERVER-8136, which mentions setting the read preference to “secondary” before running $currentOp, but that appears to only route the request to one random secondary, not all of them.
So far, it seems the only reliable approach is: Connect to each mongod node individually and run $currentOp there.
Is there a more idiomatic or official way to collect current operations from all nodes in a replica set or sharded cluster — including secondaries?
Thanks in advance!