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 command currentOp.

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 $currentOpaggregation 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:

  1. db.currentOp(<operations>)

db.currentOp() can take the following _optional_argument:

ParameterTypeDescriptionoperationsboolean 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:

FieldDescription
"$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:

  1. db.currentOp(true)
  2. db.currentOp( { "$all": true } )

db.currentOp and thedatabase profiler report the samebasic diagnostic information for all CRUD operations, including thefollowing:

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:

  1. db.currentOp(
  2. {
  3. "waitingForLock" : true,
  4. $or: [
  5. { "op" : { "$in" : [ "insert", "update", "remove" ] } },
  6. { "query.findandmodify": { $exists: true } }
  7. ]
  8. }
  9. )

Active Operations with no Yields

The following example returns information on all active runningoperations that have never yielded:

  1. db.currentOp(
  2. {
  3. "active" : true,
  4. "numYields" : 0,
  5. "waitingForLock" : false
  6. }
  7. )

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:

  1. db.currentOp(
  2. {
  3. "active" : true,
  4. "secs_running" : { "$gt" : 3 },
  5. "ns" : /^db1\./
  6. }
  7. )

Active Indexing Operations

The following example returns information on index creation operations:

  1. db.currentOp(
  2. {
  3. $or: [
  4. { op: "command", "query.createIndexes": { $exists: true } },
  5. { op: "none", ns: /\.system\.indexes\b/ }
  6. ]
  7. }
  8. )

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 currentOpoutput when run on a standalone:

Changed in version 4.2.

  1. {
  2. "inprog": [
  3. {
  4. "type" : <string>,
  5. "host" : <string>,
  6. "desc" : <string>,
  7. "connectionId" : <number>,
  8. "client" : <string>,
  9. "appName" : <string>,
  10. "clientMetadata" : <document>,
  11. "active" : <boolean>,
  12. "currentOpTime" : <string>,
  13. "effectiveUsers" : [
  14. {
  15. "user" : <string>,
  16. "db" : <string>
  17. }
  18. ],
  19. "opid" : <number>,
  20. "lsid" : {
  21. "id" : <UUID>,
  22. "uid" : <BinData>
  23. },
  24. "secs_running" : <NumberLong()>,
  25. "microsecs_running" : <number>,
  26. "op" : <string>,
  27. "ns" : <string>,
  28. "command" : <document>,
  29. "planSummary": <string>,
  30. "cursor" : { // only for getMore operations
  31. "cursorId" : <NumberLong()>,
  32. "createdDate" : <ISODate()>,
  33. "lastAccessDate" : <ISODate()>,
  34. "nDocsReturned" : <NumberLong()>,
  35. "nBatchesReturned" : <NumberLong()>,
  36. "noCursorTimeout" : <boolean>,
  37. "tailable" : <boolean>,
  38. "awaitData" : <boolean>,
  39. "originatingCommand" : <document>,
  40. "planSummary" : <string>,
  41. "operationUsingCursorId" : <NumberLong()>
  42. },
  43. "msg": <string>,
  44. "progress" : {
  45. "done" : <number>,
  46. "total" : <number>
  47. },
  48. "killPending" : <boolean>,
  49. "numYields" : <number>,
  50. "locks" : {
  51. "ParallelBatchWriterMode" : <string>,
  52. "ReplicationStateTransition" : <string>,
  53. "Global" : <string>,
  54. "Database" : <string>,
  55. "Collection" : <string>,
  56. "Metadata" : <string>,
  57. "oplog" : <string>
  58. },
  59. "waitingForLock" : <boolean>,
  60. "lockStats" : {
  61. "ParallelBatchWriterMode" : {
  62. "acquireCount": {
  63. "r": <NumberLong>,
  64. "w": <NumberLong>,
  65. "R": <NumberLong>,
  66. "W": <NumberLong>
  67. },
  68. "acquireWaitCount": {
  69. "r": <NumberLong>,
  70. "w": <NumberLong>,
  71. "R": <NumberLong>,
  72. "W": <NumberLong>
  73. },
  74. "timeAcquiringMicros" : {
  75. "r" : NumberLong(0),
  76. "w" : NumberLong(0),
  77. "R" : NumberLong(0),
  78. "W" : NumberLong(0)
  79. },
  80. "deadlockCount" : {
  81. "r" : NumberLong(0),
  82. "w" : NumberLong(0),
  83. "R" : NumberLong(0),
  84. "W" : NumberLong(0)
  85. }
  86. },
  87. "ReplicationStateTransition" : {
  88. ...
  89. },
  90. "Global": {
  91. ...
  92. },
  93. "Database" : {
  94. ...
  95. },
  96. ...
  97. }
  98. },
  99. ...
  100. ],
  101. "fsyncLock": <boolean>,
  102. "info": <string>,
  103. "ok": <num>
  104. }

The following is a prototype of the currentOpoutput when run on a primary of a replica set:

Changed in version 4.2.

  1. {
  2. "inprog": [
  3. {
  4. "type" : <string>,
  5. "host" : <string>,
  6. "desc" : <string>,
  7. "connectionId" : <number>,
  8. "client" : <string>,
  9. "appName" : <string>,
  10. "clientMetadata" : <document>,
  11. "lsid" : {
  12. "id" : <UUID>,
  13. "uid" : <BinData>
  14. },
  15. "transaction" : {
  16. "parameters" : {
  17. "txnNumber" : <NumberLong()>,
  18. "autocommit" : <boolean>,
  19. "readConcern" : {
  20. "level" : <string>
  21. }
  22. },
  23. "readTimestamp" : <Timestamp>,
  24. "startWallClockTime" : <string>,
  25. "timeOpenMicros" : <NumberLong()>,
  26. "timeActiveMicros" : <NumberLong()>,
  27. "timeInactiveMicros" : <NumberLong()>,
  28. "expiryTime" : <string>,
  29. },
  30. "active" : <boolean>,
  31. "currentOpTime" : <string>,
  32. "effectiveUsers" : [
  33. {
  34. "user" : <string>,
  35. "db" : <string>
  36. }
  37. ],
  38. "opid" : <number>,
  39. "secs_running" : <NumberLong()>,
  40. "microsecs_running" : <number>,
  41. "op" : <string>,
  42. "ns" : <string>,
  43. "command" : <document>,
  44. "originatingCommand" : <document>,
  45. "planSummary": <string>,
  46. "prepareReadConflicts" : <NumberLong()>,
  47. "writeConflicts" : <NumberLong()>,
  48. "cursor" : { // only for getMore operations
  49. "cursorId" : <NumberLong()>,
  50. "createdDate" : <ISODate()>,
  51. "lastAccessDate" : <ISODate()>,
  52. "nDocsReturned" : <NumberLong()>,
  53. "nBatchesReturned" : <NumberLong()>,
  54. "noCursorTimeout" : <boolean>,
  55. "tailable" : <boolean>,
  56. "awaitData" : <boolean>,
  57. "originatingCommand" : <document>,
  58. "planSummary" : <string>,
  59. "operationUsingCursorId" : <NumberLong()>
  60. },
  61. "msg": <string>,
  62. "progress" : {
  63. "done" : <number>,
  64. "total" : <number>
  65. },
  66. "killPending" : <boolean>,
  67. "numYields" : <number>,
  68. "locks" : {
  69. "ParallelBatchWriterMode" : <string>,
  70. "ReplicationStateTransition" : <string>,
  71. "Global" : <string>,
  72. "Database" : <string>,
  73. "Collection" : <string>,
  74. "Metadata" : <string>,
  75. "oplog" : <string>
  76. },
  77. "waitingForLock" : <boolean>,
  78. "lockStats" : {
  79. "ParallelBatchWriterMode" : {
  80. "acquireCount": {
  81. "r": <NumberLong>,
  82. "w": <NumberLong>,
  83. "R": <NumberLong>,
  84. "W": <NumberLong>
  85. },
  86. "acquireWaitCount": {
  87. "r": <NumberLong>,
  88. "w": <NumberLong>,
  89. "R": <NumberLong>,
  90. "W": <NumberLong>
  91. },
  92. "timeAcquiringMicros" : {
  93. "r" : NumberLong(0),
  94. "w" : NumberLong(0),
  95. "R" : NumberLong(0),
  96. "W" : NumberLong(0)
  97. },
  98. "deadlockCount" : {
  99. "r" : NumberLong(0),
  100. "w" : NumberLong(0),
  101. "R" : NumberLong(0),
  102. "W" : NumberLong(0)
  103. }
  104. },
  105. "ReplicationStateTransition" : {
  106. ...
  107. },
  108. "Global" : {
  109. ...
  110. },
  111. "Database" : {
  112. ...
  113. },
  114. ...
  115. }
  116. },
  117. ...
  118. ],
  119. "fsyncLock": <boolean>,
  120. "info": <string>,
  121. "ok": <num>,
  122. "operationTime": <timestamp>,
  123. "$clusterTime": <document>
  124. }

The following is an example of the currentOpoutput when run on a mongos of a shardedcluster (Fields may vary depending on the operation beingreported):

Changed in version 4.2.

  1. {
  2. "inprog": [
  3. {
  4. "shard": <string>,
  5. "type" : <string>,
  6. "host" : <string>,
  7. "desc" : <string>,
  8. "connectionId" : <number>,
  9. "client_s" : <string>,
  10. "appName" : <string>,
  11. "clientMetadata" : <document>,
  12. "lsid" : {
  13. "id" : <UUID>,
  14. "uid" : <BinData>
  15. },
  16. "transaction" : {
  17. "parameters" : {
  18. "txnNumber" : <NumberLong()>,
  19. "autocommit" : <boolean>,
  20. "readConcern" : {
  21. "level" : <string>
  22. }
  23. },
  24. "readTimestamp" : <Timestamp>,
  25. "startWallClockTime" : <string>,
  26. "timeOpenMicros" : <NumberLong()>,
  27. "timeActiveMicros" : <NumberLong()>,
  28. "timeInactiveMicros" : <NumberLong()>,
  29. "expiryTime" : <string>,
  30. },
  31. "active" : <boolean>,
  32. "currentOpTime" : <string>,
  33. "effectiveUsers" : [
  34. {
  35. "user" : <string>,
  36. "db" : <string>
  37. }
  38. ],
  39. "runBy" : [
  40. {
  41. "user" : <string>,
  42. "db" : <string>
  43. }
  44. ],
  45. "twoPhaseCommitCoordinator" : { // Starting in 4.2.1
  46. "lsid" : {
  47. "id" : <UUID>,
  48. "uid" : <BinData>
  49. },
  50. "txnNumber" : <NumberLong>,
  51. "numParticipants" : <NumberLong>,
  52. "state" : <string>,
  53. "commitStartTime" : <ISODate>,
  54. "hasRecoveredFromFailover" : <boolean>,
  55. "stepDurations" : <document>,
  56. "decision" : <document>,
  57. "deadline" : <ISODate>
  58. }
  59. "opid" : <string>,
  60. "secs_running" : <NumberLong()>,
  61. "microsecs_running" : <number>,
  62. "op" : <string>,
  63. "ns" : <string>,
  64. "command" : <document>,
  65. "planSummary": <string>,
  66. "prepareReadConflicts" : <NumberLong()>,
  67. "writeConflicts" : <NumberLong()>,
  68. "cursor" : { // only for getMore operations
  69. "cursorId" : <NumberLong()>,
  70. "createdDate" : <ISODate()>,
  71. "lastAccessDate" : <ISODate()>,
  72. "nDocsReturned" : <NumberLong()>,
  73. "nBatchesReturned" : <NumberLong()>,
  74. "noCursorTimeout" : <boolean>,
  75. "tailable" : <boolean>,
  76. "awaitData" : <boolean>,
  77. "originatingCommand" : <document>,
  78. "planSummary" : <string>,
  79. "operationUsingCursorId" : <NumberLong()>
  80. },
  81. "msg": <string>,
  82. "progress" : {
  83. "done" : <number>,
  84. "total" : <number>
  85. },
  86. "killPending" : <boolean>,
  87. "numYields" : <number>,
  88. "locks" : {
  89. "ParallelBatchWriterMode" : <string>,
  90. "ReplicationStateTransition" : <string>,
  91. "Global" : <string>,
  92. "Database" : <string>,
  93. "Collection" : <string>,
  94. "Metadata" : <string>,
  95. "oplog" : <string>
  96. },
  97. "waitingForLock" : <boolean>,
  98. "lockStats" : {
  99. "ParallelBatchWriterMode": {
  100. "acquireCount": {
  101. "r": <NumberLong>,
  102. "w": <NumberLong>,
  103. "R": <NumberLong>,
  104. "W": <NumberLong>
  105. },
  106. "acquireWaitCount": {
  107. "r": <NumberLong>,
  108. "w": <NumberLong>,
  109. "R": <NumberLong>,
  110. "W": <NumberLong>
  111. },
  112. "timeAcquiringMicros" : {
  113. "r" : NumberLong(0),
  114. "w" : NumberLong(0),
  115. "R" : NumberLong(0),
  116. "W" : NumberLong(0)
  117. },
  118. "deadlockCount" : {
  119. "r" : NumberLong(0),
  120. "w" : NumberLong(0),
  121. "R" : NumberLong(0),
  122. "W" : NumberLong(0)
  123. }
  124. },
  125. "ReplicationStateTransition" : {
  126. ...
  127. },
  128. "Global" : {
  129. ...
  130. },
  131. "Database" : {
  132. ...
  133. },
  134. ...
  135. }
  136. },
  137. ...
  138. ],
  139. "ok": <num>,
  140. "operationTime": <timestamp>,
  141. "$clusterTime": <document>
  142. }

Output Fields

For a complete list of db.currentOp() output fields, seecurrentOp.