db.currentOp()
Definition
db.
currentOp
()- Returns a document that contains information on in-progressoperations for the database instance. The
db.currentOp()
method wraps the database commandcurrentOp
.
Note
Because currentOp
command anddb.currentOp()
helper returns the results in a singledocument, the total size of the currentOp
result setis subject to the maximum 16MB BSON size limit for documents.
Starting in version 3.6, MongoDB provides $currentOp
aggregation stage. The $currentOp
stage returns acursor over a stream of documents, each of which reports a singleoperation. Each operation document is subject to the 16MB BSONlimit, but unlike the currentOp
command, there is nolimit on the overall size of the result set.
For this reason, the $currentOp
aggregation stage ispreferred over the currentOp
command and itsmongo
shell helper db.currentOp()
.
db.currentOp()
has the following form:
- db.currentOp(<operations>)
db.currentOp()
can take the following _optional_argument:
ParameterTypeDescriptionoperations
boolean or documentOptional. Specifies the operations to report on. Can pass either a boolean or adocument.
Specify true
to include operations on idleconnections and system operations. Specify a document with queryconditions to report only onoperations that match the conditions. See Behaviorfor details.
Behavior
db.currentOp()
can accept a filter document or a booleanparameter.
If you pass a filter document to db.currentOp()
, the outputreturns information only for the current operations that match thefilter. The filter document can contain:
Field | Description |
---|---|
"$ownOps" | Boolean. If set to true , returns information on the current user’soperations only.On mongod instances, users can always rundb.currentOp( { "$ownOps": true } ) to view their ownoperations.New in version 3.2.9. |
"$all" | Boolean. If set to true , returns information on all operations,including operations on idle connections and system operations.If the document includes "$all": true along withOutput Fields conditions, only the"$all":true applies. |
<filter> | Specify filter conditions on the Output Fields.See Examples.If the document includes "$all": true along withOutput Fields conditions, only the "$all":true applies. |
Passing in true
to db.currentOp()
is equivalent topassing in a document of { "$all": true }
. The following operationsare equivalent:
- db.currentOp(true)
- db.currentOp( { "$all": true } )
db.currentOp
and thedatabase profiler report the samebasic diagnostic information for all CRUD operations, including thefollowing:
aggregate
count
delete
distinct
find
(OP_QUERY andcommand
)findAndModify
getMore
(OP_GET_MORE andcommand
)insert
mapReduce
update
These operations are also included in the logging ofslow queries (see slowOpThresholdMs
formore information about slow query logging).
Access Control
On systems running with authorization
, the usermust have access that includes the inprog
privilegeaction.
Changed in version 3.2.9: On mongod
instances, users can run db.currentOp( {"$ownOps": true } )
to view their own operations even without theinprog
privilege action.
See also
Create a Role to Manage Current Operations
Examples
The following examples use the db.currentOp()
method withvarious query documents to filter the output.
Write Operations Waiting for a Lock
The following example returns information on all write operations thatare waiting for a lock:
- db.currentOp(
- {
- "waitingForLock" : true,
- $or: [
- { "op" : { "$in" : [ "insert", "update", "remove" ] } },
- { "query.findandmodify": { $exists: true } }
- ]
- }
- )
Active Operations with no Yields
The following example returns information on all active runningoperations that have never yielded:
- db.currentOp(
- {
- "active" : true,
- "numYields" : 0,
- "waitingForLock" : false
- }
- )
Active Operations on a Specific Database
The following example returns information on all active operations fordatabase db1
that have been running longer than 3 seconds:
- db.currentOp(
- {
- "active" : true,
- "secs_running" : { "$gt" : 3 },
- "ns" : /^db1\./
- }
- )
Active Indexing Operations
The following example returns information on index creation operations:
- db.currentOp(
- {
- $or: [
- { op: "command", "query.createIndexes": { $exists: true } },
- { op: "none", ns: /\.system\.indexes\b/ }
- ]
- }
- )
Output Example
The following is a prototype of db.currentOp()
output.
- Standalone
- Replica Set (Primary)
- Sharded Cluster (mongos)
The following is a prototype of the currentOp
output when run on a standalone:
Changed in version 4.2.
- {
- "inprog": [
- {
- "type" : <string>,
- "host" : <string>,
- "desc" : <string>,
- "connectionId" : <number>,
- "client" : <string>,
- "appName" : <string>,
- "clientMetadata" : <document>,
- "active" : <boolean>,
- "currentOpTime" : <string>,
- "effectiveUsers" : [
- {
- "user" : <string>,
- "db" : <string>
- }
- ],
- "opid" : <number>,
- "lsid" : {
- "id" : <UUID>,
- "uid" : <BinData>
- },
- "secs_running" : <NumberLong()>,
- "microsecs_running" : <number>,
- "op" : <string>,
- "ns" : <string>,
- "command" : <document>,
- "planSummary": <string>,
- "cursor" : { // only for getMore operations
- "cursorId" : <NumberLong()>,
- "createdDate" : <ISODate()>,
- "lastAccessDate" : <ISODate()>,
- "nDocsReturned" : <NumberLong()>,
- "nBatchesReturned" : <NumberLong()>,
- "noCursorTimeout" : <boolean>,
- "tailable" : <boolean>,
- "awaitData" : <boolean>,
- "originatingCommand" : <document>,
- "planSummary" : <string>,
- "operationUsingCursorId" : <NumberLong()>
- },
- "msg": <string>,
- "progress" : {
- "done" : <number>,
- "total" : <number>
- },
- "killPending" : <boolean>,
- "numYields" : <number>,
- "locks" : {
- "ParallelBatchWriterMode" : <string>,
- "ReplicationStateTransition" : <string>,
- "Global" : <string>,
- "Database" : <string>,
- "Collection" : <string>,
- "Metadata" : <string>,
- "oplog" : <string>
- },
- "waitingForLock" : <boolean>,
- "lockStats" : {
- "ParallelBatchWriterMode" : {
- "acquireCount": {
- "r": <NumberLong>,
- "w": <NumberLong>,
- "R": <NumberLong>,
- "W": <NumberLong>
- },
- "acquireWaitCount": {
- "r": <NumberLong>,
- "w": <NumberLong>,
- "R": <NumberLong>,
- "W": <NumberLong>
- },
- "timeAcquiringMicros" : {
- "r" : NumberLong(0),
- "w" : NumberLong(0),
- "R" : NumberLong(0),
- "W" : NumberLong(0)
- },
- "deadlockCount" : {
- "r" : NumberLong(0),
- "w" : NumberLong(0),
- "R" : NumberLong(0),
- "W" : NumberLong(0)
- }
- },
- "ReplicationStateTransition" : {
- ...
- },
- "Global": {
- ...
- },
- "Database" : {
- ...
- },
- ...
- }
- },
- ...
- ],
- "fsyncLock": <boolean>,
- "info": <string>,
- "ok": <num>
- }
The following is a prototype of the currentOp
output when run on a primary of a replica set:
Changed in version 4.2.
- {
- "inprog": [
- {
- "type" : <string>,
- "host" : <string>,
- "desc" : <string>,
- "connectionId" : <number>,
- "client" : <string>,
- "appName" : <string>,
- "clientMetadata" : <document>,
- "lsid" : {
- "id" : <UUID>,
- "uid" : <BinData>
- },
- "transaction" : {
- "parameters" : {
- "txnNumber" : <NumberLong()>,
- "autocommit" : <boolean>,
- "readConcern" : {
- "level" : <string>
- }
- },
- "readTimestamp" : <Timestamp>,
- "startWallClockTime" : <string>,
- "timeOpenMicros" : <NumberLong()>,
- "timeActiveMicros" : <NumberLong()>,
- "timeInactiveMicros" : <NumberLong()>,
- "expiryTime" : <string>,
- },
- "active" : <boolean>,
- "currentOpTime" : <string>,
- "effectiveUsers" : [
- {
- "user" : <string>,
- "db" : <string>
- }
- ],
- "opid" : <number>,
- "secs_running" : <NumberLong()>,
- "microsecs_running" : <number>,
- "op" : <string>,
- "ns" : <string>,
- "command" : <document>,
- "originatingCommand" : <document>,
- "planSummary": <string>,
- "prepareReadConflicts" : <NumberLong()>,
- "writeConflicts" : <NumberLong()>,
- "cursor" : { // only for getMore operations
- "cursorId" : <NumberLong()>,
- "createdDate" : <ISODate()>,
- "lastAccessDate" : <ISODate()>,
- "nDocsReturned" : <NumberLong()>,
- "nBatchesReturned" : <NumberLong()>,
- "noCursorTimeout" : <boolean>,
- "tailable" : <boolean>,
- "awaitData" : <boolean>,
- "originatingCommand" : <document>,
- "planSummary" : <string>,
- "operationUsingCursorId" : <NumberLong()>
- },
- "msg": <string>,
- "progress" : {
- "done" : <number>,
- "total" : <number>
- },
- "killPending" : <boolean>,
- "numYields" : <number>,
- "locks" : {
- "ParallelBatchWriterMode" : <string>,
- "ReplicationStateTransition" : <string>,
- "Global" : <string>,
- "Database" : <string>,
- "Collection" : <string>,
- "Metadata" : <string>,
- "oplog" : <string>
- },
- "waitingForLock" : <boolean>,
- "lockStats" : {
- "ParallelBatchWriterMode" : {
- "acquireCount": {
- "r": <NumberLong>,
- "w": <NumberLong>,
- "R": <NumberLong>,
- "W": <NumberLong>
- },
- "acquireWaitCount": {
- "r": <NumberLong>,
- "w": <NumberLong>,
- "R": <NumberLong>,
- "W": <NumberLong>
- },
- "timeAcquiringMicros" : {
- "r" : NumberLong(0),
- "w" : NumberLong(0),
- "R" : NumberLong(0),
- "W" : NumberLong(0)
- },
- "deadlockCount" : {
- "r" : NumberLong(0),
- "w" : NumberLong(0),
- "R" : NumberLong(0),
- "W" : NumberLong(0)
- }
- },
- "ReplicationStateTransition" : {
- ...
- },
- "Global" : {
- ...
- },
- "Database" : {
- ...
- },
- ...
- }
- },
- ...
- ],
- "fsyncLock": <boolean>,
- "info": <string>,
- "ok": <num>,
- "operationTime": <timestamp>,
- "$clusterTime": <document>
- }
The following is an example of the currentOp
output when run on a mongos
of a shardedcluster (Fields may vary depending on the operation beingreported):
Changed in version 4.2.
- {
- "inprog": [
- {
- "shard": <string>,
- "type" : <string>,
- "host" : <string>,
- "desc" : <string>,
- "connectionId" : <number>,
- "client_s" : <string>,
- "appName" : <string>,
- "clientMetadata" : <document>,
- "lsid" : {
- "id" : <UUID>,
- "uid" : <BinData>
- },
- "transaction" : {
- "parameters" : {
- "txnNumber" : <NumberLong()>,
- "autocommit" : <boolean>,
- "readConcern" : {
- "level" : <string>
- }
- },
- "readTimestamp" : <Timestamp>,
- "startWallClockTime" : <string>,
- "timeOpenMicros" : <NumberLong()>,
- "timeActiveMicros" : <NumberLong()>,
- "timeInactiveMicros" : <NumberLong()>,
- "expiryTime" : <string>,
- },
- "active" : <boolean>,
- "currentOpTime" : <string>,
- "effectiveUsers" : [
- {
- "user" : <string>,
- "db" : <string>
- }
- ],
- "runBy" : [
- {
- "user" : <string>,
- "db" : <string>
- }
- ],
- "twoPhaseCommitCoordinator" : { // Starting in 4.2.1
- "lsid" : {
- "id" : <UUID>,
- "uid" : <BinData>
- },
- "txnNumber" : <NumberLong>,
- "numParticipants" : <NumberLong>,
- "state" : <string>,
- "commitStartTime" : <ISODate>,
- "hasRecoveredFromFailover" : <boolean>,
- "stepDurations" : <document>,
- "decision" : <document>,
- "deadline" : <ISODate>
- }
- "opid" : <string>,
- "secs_running" : <NumberLong()>,
- "microsecs_running" : <number>,
- "op" : <string>,
- "ns" : <string>,
- "command" : <document>,
- "planSummary": <string>,
- "prepareReadConflicts" : <NumberLong()>,
- "writeConflicts" : <NumberLong()>,
- "cursor" : { // only for getMore operations
- "cursorId" : <NumberLong()>,
- "createdDate" : <ISODate()>,
- "lastAccessDate" : <ISODate()>,
- "nDocsReturned" : <NumberLong()>,
- "nBatchesReturned" : <NumberLong()>,
- "noCursorTimeout" : <boolean>,
- "tailable" : <boolean>,
- "awaitData" : <boolean>,
- "originatingCommand" : <document>,
- "planSummary" : <string>,
- "operationUsingCursorId" : <NumberLong()>
- },
- "msg": <string>,
- "progress" : {
- "done" : <number>,
- "total" : <number>
- },
- "killPending" : <boolean>,
- "numYields" : <number>,
- "locks" : {
- "ParallelBatchWriterMode" : <string>,
- "ReplicationStateTransition" : <string>,
- "Global" : <string>,
- "Database" : <string>,
- "Collection" : <string>,
- "Metadata" : <string>,
- "oplog" : <string>
- },
- "waitingForLock" : <boolean>,
- "lockStats" : {
- "ParallelBatchWriterMode": {
- "acquireCount": {
- "r": <NumberLong>,
- "w": <NumberLong>,
- "R": <NumberLong>,
- "W": <NumberLong>
- },
- "acquireWaitCount": {
- "r": <NumberLong>,
- "w": <NumberLong>,
- "R": <NumberLong>,
- "W": <NumberLong>
- },
- "timeAcquiringMicros" : {
- "r" : NumberLong(0),
- "w" : NumberLong(0),
- "R" : NumberLong(0),
- "W" : NumberLong(0)
- },
- "deadlockCount" : {
- "r" : NumberLong(0),
- "w" : NumberLong(0),
- "R" : NumberLong(0),
- "W" : NumberLong(0)
- }
- },
- "ReplicationStateTransition" : {
- ...
- },
- "Global" : {
- ...
- },
- "Database" : {
- ...
- },
- ...
- }
- },
- ...
- ],
- "ok": <num>,
- "operationTime": <timestamp>,
- "$clusterTime": <document>
- }
Output Fields
For a complete list of db.currentOp()
output fields, seecurrentOp.