Navigation

$currentOp (aggregation)

Definition

New in version 3.6.

$currentOp

Returns a stream of documents containing information on active and/or dormant operations as well as inactive sessions that are holding locks as part of a transaction. The stage returns a document for each operation or session. To run $currentOp, use the db.aggregate() helper on the admin database.

The $currentOp aggregation stage is preferred over the currentOp command and its mongo shell helper db.currentOp(). Because currentOp command and db.currentOp() helper returns the results in a single document, the total size of the currentOp result set is subject to the maximum 16MB BSON size limit for documents. The $currentOp stage returns a cursor over a stream of documents, each of which reports a single operation. Each operation document is subject to the 16MB BSON limit, but unlike the currentOp command, there is no limit on the overall size of the result set.

$currentOp also enables you to perform arbitrary transformations of the results as the documents pass through the pipeline.

$currentOp takes an options document as its operand:

Changed in version 4.0.

{ $currentOp: { allUsers: <boolean>, idleConnections: <boolean>, idleSessions: <boolean>, localOps: <boolean> } }
Option Description
allUsers

Boolean. If set to false, $currentOp will only report operations belonging to the user who ran the command. If set to true, $currentOp will report operations belonging to all users.

Note

The inprog privilege is necessary to run $currentOp with { allUsers : true }.

Defaults to false.

idleConnections

Boolean. If set to false, $currentOp will only report active operations. If set to true, all operations including idle connections will be returned.

Defaults to false.

idleSessions

Boolean. If set to true, then in addition to reporting on the active/dormant operations, $currentOp returns information on inactive sessions that are holding locks as part of a transaction. Each inactive session will appear as a separate document in the $currentOp stream.

The document for a session includes information on the session id lsid and the transaction number txnNumber.

If set to false, $currentOp will not report on inactive sessions.

Defaults to true.

New in version 4.0.

localOps

Boolean. If set to true for an aggregation running on mongos, $currentOp reports only those operations running locally on that mongos. If false, then the $currentOp will instead report operations running on the shards.

The localOps parameter has no effect for $currentOp aggregations running on mongod.

Defaults to false.

New in version 4.0.

Omitting any of the above parameters will cause $currentOp to use that parameter’s default value. Specify an empty document, as shown below, to use the default values of all parameters.

{ $currentOp: { } }

Constraints

  • $currentOp must be the first stage in the pipeline.
  • Pipelines that start with $currentOp can only be run on the admin database.
  • On a standalone or replica set with authentication enabled, the inprog privilege is required to run $currentOp if the allUsers parameter is set to true.
  • On a sharded cluster, the inprog privilege is required to run all $currentOp pipelines.
  • $currentOp is not allowed in transactions.

Example

The following example runs the $currentOp operation in the first stage and filters the results of that operation in the second stage. Specifically, the first stage returns documents for all active operations as well as inactive sessions that are holding locks as part of a transaction. The second stage filters for just those documents related to inactive sessions that are holding locks as part of a transaction.

use admin
db.aggregate( [
   { $currentOp : { allUsers: true, idleSessions: true } },
   { $match : { active: false, txnNumber : { $exists: true } } }
] )

The operation returns documents of the form:

{
   "host" : "example.mongodb.com:27017",
   "desc" : "inactive transaction",
   "lsid" : {
      "id" : UUID("02039c77-3f05-4dd6-a42d-d2168a73fc95"),
      "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
   },
   "txnNumber" : NumberLong(4),
   "waitingForLock" : false,
   "active" : false,
   "locks" : {
      "Global" : "w",
      "Database" : "w",
      "Collection" : "w"
   },
   "lockStats" : {
      "Global" : {
         "acquireCount" : {
            "r" : NumberLong(2),
            "w" : NumberLong(1)
         }
      },
      "Database" : {
         "acquireCount" : {
            "r" : NumberLong(1),
            "w" : NumberLong(1)
         }
      },
      "Collection" : {
         "acquireCount" : {
            "r" : NumberLong(1),
            "w" : NumberLong(1)
         }
      }
   }
}

Output Fields

Each output document may contain a subset of the following fields as relevant for the operation:

$currentOp.host

The name of the host against which the operation is run.

$currentOp.shard

The name of the shard where the operation is running.

Only present for sharded clusters.

$currentOp.desc

A description of the operation.

$currentOp.connectionId

An identifier for the connection where the specific operation originated.

$currentOp.client

The IP address (or hostname) and the ephemeral port of the client connection where the operation originates.

For standalones and replica sets only

$currentOp.client_s

The IP address (or hostname) and the ephemeral port of the mongos where the operation originates.

For sharded clusters only

$currentOp.clientMetadata

Additional information on the client.

$currentOp.appName

New in version 3.4.

The identifier of the client application which ran the operation. Use the appName connection string option to set a custom value for the appName field.

$currentOp.active

A boolean value specifying whether the operation has started. Value is true if the operation has started or false if the operation is idle, such as an idle connection, an inactive session, or an internal thread that is currently idle. An operation can be active even if the operation has yielded to another operation.

$currentOp.currentOpTime

The start time of the operation.

New in version 3.6.

$currentOp.opid

The identifier for the operation. You can pass this value to db.killOp() in the mongo shell to terminate the operation.

Warning

Terminate running operations with extreme caution. Only use db.killOp() to terminate operations initiated by clients and do not terminate internal database operations.

$currentOp.secs_running

The duration of the operation in seconds. MongoDB calculates this value by subtracting the current time from the start time of the operation.

Only present if the operation is running; i.e. if active is true.

$currentOp.microsecs_running

The duration of the operation in microseconds. MongoDB calculates this value by subtracting the current time from the start time of the operation.

Only present if the operation is running; i.e. if active is true.

$currentOp.lsid

The session identifier.

Only present if the operation is associated with a session.

New in version 3.6.

$currentOp.txnNumber

The transaction number.

Only present if the operation is part of a transaction.

New in version 4.0.

$currentOp.op

A string that identifies the type of operation. The possible values are:

  • "none"
  • "update"
  • "insert"
  • "query"
  • "command"
  • "getmore"
  • "remove"
  • "killcursors"

"command" operations include most commands such as the createIndexes, aggregate, and findandmodify.

"query" operations include find operations and OP_QUERY operations.

$currentOp.ns

The namespace the operation targets. A namespace consists of the database name and the collection name concatenated with a dot (.); that is, "<database>.<collection>".

$currentOp.command

Changed in version 3.6.

A document containing the full command object associated with this operation. If the command document exceeds 1 kilobyte, the document has the following form:

"command" : {
  "$truncated": <string>,
  "comment": <string>
}

The $truncated field contains a string summary of the document excluding the document’s comment field if present. If the summary still exceeds 1 kilobyte then it is further truncated, denoted by an ellipsis (…) at the end of the string.

The comment field is present if a comment was passed to the operation.

The following example output contains the command object for a find operation on a collection named items in a database named test:

"command" : {
  "find" : "items",
  "filter" : {
    "sku" : 1403978
  },
  "$db" : "test"
}

The following example output contains the command object for a getMore operation generated by a command with cursor id 80336119321 on a collection named items in a database named test:

"command" : {
  "getMore" : NumberLong("80336119321"),
  "collection" : "items",
  "$db" : "test"
}
$currentOp.originatingCommand

Changed in version 3.6: For "getmore" operations which retrieve the next batch of results from a cursor, the originatingCommand field contains the full command object (e.g. find or aggregate) which originally created that cursor.

$currentOp.planSummary

A string that contains the query plan to help debug slow queries.

$currentOp.numYields

numYields is a counter that reports the number of times the operation has yielded to allow other operations to complete.

Typically, operations yield when they need access to data that MongoDB has not yet fully read into memory. This allows other operations that have data in memory to complete quickly while MongoDB reads in data for the yielding operation.

$currentOp.locks

The locks document reports the type and mode of locks the operation currently holds. The possible lock types are as follows:

Lock Type Description
Global Represents global lock.
MMAPV1Journal Represents MMAPv1 storage engine specific lock to synchronize journal writes; for non-MMAPv1 storage engines, the mode for MMAPV1Journal is empty.
Database Represents database lock.
Collection Represents collection lock.
Metadata Represents metadata lock.
oplog Represents lock on the oplog.

The possible modes are as follows:

Lock Mode Description
R Represents Shared (S) lock.
W Represents Exclusive (X) lock.
r Represents Intent Shared (IS) lock.
w Represents Intent Exclusive (IX) lock.
$currentOp.waitingForLock

Returns a boolean value. waitingForLock is true if the operation is waiting for a lock and false if the operation has the required lock.

$currentOp.msg

The msg provides a message that describes the status and progress of the operation. In the case of indexing or mapReduce operations, the field reports the completion percentage.

$currentOp.progress

Reports on the progress of mapReduce or indexing operations. The progress fields corresponds to the completion percentage in the msg field. The progress specifies the following information:

$currentOp.progress.done

Reports the number of work items completed.

$currentOp.progress.total

Reports the total number of work items.

$currentOp.killPending

Returns true if the operation is currently flagged for termination. When the operation encounters its next safe termination point, the operation will terminate.

$currentOp.lockStats

For each lock type and mode (see $currentOp.locks for descriptions of lock types and modes), returns the following information:

$currentOp.lockStats.acquireCount

Number of times the operation acquired the lock in the specified mode.

$currentOp.lockStats.acquireWaitCount

Number of times the operation had to wait for the acquireCount lock acquisitions because the locks were held in a conflicting mode. acquireWaitCount is less than or equal to acquireCount.

$currentOp.lockStats.timeAcquiringMicros

Cumulative time in microseconds that the operation had to wait to acquire the locks.

timeAcquiringMicros divided by acquireWaitCount gives an approximate average wait time for the particular lock mode.

$currentOp.lockStats.deadlockCount

Number of times the operation encountered deadlocks while waiting for lock acquisitions.