db.currentOp()

On this page本页内容

Definition定义

db.currentOp()

Returns a document that contains information on in-progress operations for the database instance. 返回一个文档,其中包含有关数据库实例正在进行的操作的信息。The db.currentOp() method wraps the database command currentOp.db.currentOp()方法包装数据库命令currentOp

Note

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.由于currentOp命令和db.currentOp()助手在单个文档中返回结果,因此currentOp结果集的总大小受文档的最大16MB BSON大小限制的约束。

Starting in version 3.6, MongoDB provides $currentOp aggregation stage. 从3.6版开始,MongoDB提供了$currentOp聚合阶段。The $currentOp stage returns a cursor over a stream of documents, each of which reports a single operation. $currentOp阶段返回一个游标到一个文档流上,每个文档都报告一个操作。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.每个操作文档都受16MB BSON限制,但与currentOp命令不同,结果集的总体大小没有限制。

For this reason, the $currentOp aggregation stage is preferred over the currentOp command and its mongo shell helper db.currentOp().因此,$currentOp聚合阶段优于currentOp命令及其mongo shell助手db.currentOp()

Syntax语法

db.currentOp() has the following form:具有以下形式:

db.currentOp(<operations>)

db.currentOp() can take the following optional argument:可以采用以下可选参数:

Parameter参数Type类型Description描述
operations boolean or document

Optional. 可选。Specifies the operations to report on. 指定要报告的操作。Can pass either a boolean or a document:可以传递布尔值或文档:

  • Specify true to include operations on idle connections and system operations.指定true以包括空闲连接上的操作和系统操作。
  • Specify a document with query conditions to report only on those operations that match the conditions. 指定具有查询条件的文档,以便仅报告与条件匹配的操作。See Behavior for details.有关详细信息,请参阅行为

Behavior行为

db.currentOp() can accept a filter document or a boolean parameter.可以接受筛选文档或布尔参数。

If you pass a filter document to db.currentOp(), the output returns information only for the current operations that match the filter. 如果将筛选器文档传递给db.currentOp(),则输出仅返回与筛选器匹配的当前操作的信息。The filter document can contain:筛选文档可以包含:

Field字段Description描述
"$ownOps"

Boolean. 布尔型。If set to true, returns information on the current user’s operations only.如果设置为true,则仅返回有关当前用户操作的信息。

On mongod instances, users can always run db.currentOp( { "$ownOps": true } ) to view their own operations.mongod实例上,用户始终可以运行db.currentOp( { "$ownOps": true } )来查看自己的操作。

New in version 3.2.9.3.2.9版新增。

"$all"

Boolean. 布尔型。If set to true, returns information on all operations, including operations on idle connections and system operations.如果设置为true,则返回所有操作的信息,包括空闲连接操作和系统操作。

If the document includes "$all": true along with Output Fields conditions, only the "$all":true applies.如果文档包括"$all": true以及输出字段条件,则只有"$all":true适用。

<filter>

Specify filter conditions on the Output Fields. 输出字段上指定筛选条件。See Examples.请参阅示例

If the document includes "$all": true along with Output Fields conditions, only the "$all": true applies.如果文档包括"$all": true以及输出字段条件,则只有“$all”:true适用。

Passing in true to db.currentOp() is equivalent to passing in a document of { "$all": true }. db.currentOp()传入true相当于传入一个{ "$all": true }的文档。The following operations are equivalent:以下操作是等效的:

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

db.currentOp and the database profiler report the same basic diagnostic information for all CRUD operations, including the following:以及数据库探查器为所有CRUD操作报告相同的基本诊断信息,包括:

These operations are also included in the logging of slow queries (see slowOpThresholdMs for more information about slow query logging).这些操作也包括在慢速查询的日志记录中(有关慢速查询日志记录的更多信息,请参阅slowOpThresholdMs)。

Access Control访问控制

On systems running with authorization, the user must have access that includes the inprog privilege action.在使用authorization运行的系统上,用户必须具有包括inprog权限操作的访问权限。

Starting in 3.2.9, users can run db.currentOp( { "$ownOps": true } ) on mongod instances to view their own operations even without the inprog privilege action.从3.2.9开始,用户可以在mongod实例上运行db.currentOp( { "$ownOps": true } ),以查看自己的操作,即使没有inprog权限操作。

Examples示例

The following examples use the db.currentOp() method with various query documents to filter the output.以下示例将db.currentOp()方法与各种查询文档一起使用,以筛选输出。

Write Operations Waiting for a Lock等待锁定的写操作

Changed in version 3.6.在版本3.6中更改。

The following example returns information on all write operations that are waiting for a lock:以下示例返回有关等待锁定的所有写入操作的信息:

db.currentOp(
   {
     "waitingForLock" : true,
     $or: [
        { "op" : { "$in" : [ "insert", "update", "remove" ] } },
        { "command.findandmodify": { $exists: true } }
    ]
   }
)

Active Operations with no Yields无收益的积极行动

The following example returns information on all active running operations 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 for database db1 that have been running longer than 3 seconds:以下示例返回数据库db1的所有活动操作的信息,这些操作已运行超过3秒:

db.currentOp(
   {
     "active" : true,
     "secs_running" : { "$gt" : 3 },
     "ns" : /^db1\./
   }
)

Active Indexing Operations主动索引操作

Changed in version 3.6.已在3.6版中更改。

The following example returns information on index creation operations:以下示例返回有关索引创建操作的信息:

db.adminCommand(
    {
      currentOp: true,
      $or: [
        { op: "command", "command.createIndexes": { $exists: true }  },
        { op: "none", "msg" : /^Index Build/ }
      ]
    }
)

Output Example输出示例

The following is a prototype of db.currentOp() output.以下是db.currentOp()输出的原型。

The following is a prototype of the currentOp output when run on a standalone:以下是在单机上运行时currentOp输出的原型:

Changed in version 4.2.在版本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>,
         "dataThroughputLastSecond" : <number>, // Starting in MongoDB 4.4 for validate operations
         "dataThroughputAverage" : <number>,    // Starting in MongoDB 4.4 for validate operations
         "waitingForLatch" : {                   // Starting in MongoDB 4.2.2
             "timestamp" : <ISODate()>,
             "captureName" : <string>
         },
         "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:以下是在副本集的主设备上运行时currentOp输出的原型:

Changed in version 4.2.在版本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>,
         "dataThroughputLastSecond" : <number>, // Starting in MongoDB 4.4 for validate operations
         "dataThroughputAverage" : <number>,    // Starting in MongoDB 4.4 for validate operations
         "waitingForLatch" : {                   // Starting in MongoDB 4.2.2
              "timestamp" : <ISODate()>,
              "captureName" : <string>
          },
         "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 sharded cluster (Fields may vary depending on the operation being reported):以下是在分片集群的mongos上运行currentOp输出的示例(字段可能因报告的操作而异):

Changed in version 4.2.在版本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>,
         "dataThroughputLastSecond" : <number>, // Starting in MongoDB 4.4 for validate operations
         "dataThroughputAverage" : <number>,    // Starting in MongoDB 4.4 for validate operations
         "waitingForLatch" : {                   // Starting in MongoDB 4.2.2
             "timestamp" : <ISODate()>,
             "captureName" : <string>
         },
         "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, see currentOp.有关db.currentOp()输出字段的完整列表,请参阅currentOp