对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db.currentOp()(mongosh方法)

db.currentOp()

重要

currentOp自版本 以来,6.2 $currentOpdb.currentOp()数据库命令已弃用。使用 聚合阶段而不是currentOp 命令及其mongosh 辅助方法 。

Returns a document that contains information on in-progress operations for the database instance. The db.currentOp() method wraps the database command currentOp.

注意

Starting in MongoDB 5.0, the $currentOp aggregation stage is used when running the helper method db.currentOp() with mongosh.

Given this, in the 5.0 version of the shell and with mongosh, db.currentOp() result sets are not subject to the 16MB BSON document return size document return size limit for documents of the previous legacy mongo versions.

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

重要

M0 和 Flex 集群不支持此命令。有关更多信息,请参阅不支持的命令。

db.currentOp() 采用以下形式:

db.currentOp(<operations>)

db.currentOp() can take the following optional argument:

Parameter
类型
说明

布尔或文档

可选。指定要报告的操作。可以传递布尔值或文档:

  • 指定 true 以包括针对空闲连接的操作和系统操作。

  • 指定带有查询条件的文档,以仅报告符合这些条件的操作。详情请参阅行为

db.currentOp() 可以接受筛选器文档或布尔参数。

If you pass a filter document to db.currentOp(), the output returns information only for the current operations that match the filter. The filter document can contain:

字段
说明

"$ownOps"

布尔值。如果设为 true,则只返回有关当前用户的操作的信息。

mongod 实例上,用户可以随时运行 db.currentOp( { "$ownOps": true } ) 以查看自己的操作。

"$all"

布尔值。如果设置为 true,则返回所有操作的信息,包括对空闲连接的操作和系统操作。有关更多信息,请参阅 $currentOp(聚合阶段)。

If the document includes "$all": true along with Output Fields conditions, only the "$all":true applies.

<filter>

指定输出字段的过滤条件。请参阅示例。

If the document includes "$all": true along with Output Fields conditions, only the "$all": true applies.

Passing in true to db.currentOp() is equivalent to passing in a document of { "$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:

这些操作也包含在慢查询的日志中。有关慢查询日志的更多信息,请参阅 slowOpThresholdMs

在使用 authorization 运行的系统上,用户必须具有包含 inprog 特权动作的访问权限。

即使没有 inprog 特权动作,用户也可以在 mongod 实例上运行 db.currentOp( { "$ownOps": true } ),来查看自己的操作。

The following examples use the db.currentOp() method with various query documents to filter the output.

以下示例返回有关等待锁的所有写操作的信息:

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

以下示例将返回有关从未出现的所有正在运行的活动操作的信息:

db.currentOp(
{
"active" : true,
"numYields" : 0,
"waitingForLock" : false
}
)

以下示例将返回有关运行时间超过 3 秒且针对数据库 db1 的所有活动操作的信息:

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

以下示例返回有关任意数量字段的索引创建操作的信息:

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

注意

Active Indexing Operations do not apply to rolling index builds.

The following is a prototype of db.currentOp() output.

以下是在独立实例上运行时 currentOp 输出的原型:

{
"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" : <Long()>,
"microsecs_running" : <number>,
"op" : <string>,
"ns" : <string>,
"command" : <document>,
"queryFramework" : <string>,
"planSummary": <string>,
"cursor" : { // only for getMore operations
"cursorId" : <Long()>,
"createdDate" : <ISODate()>,
"lastAccessDate" : <ISODate()>,
"nDocsReturned" : <Long()>,
"nBatchesReturned" : <Long()>,
"noCursorTimeout" : <boolean>,
"tailable" : <boolean>,
"awaitData" : <boolean>,
"originatingCommand" : <document>,
"planSummary" : <string>,
"operationUsingCursorId" : <Long()>
},
"msg": <string>,
"progress" : {
"done" : <number>,
"total" : <number>
},
"killPending" : <boolean>,
"numYields" : <number>,
"dataThroughputLastSecond" : <number>,
"dataThroughputAverage" : <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" : Long(0),
"w" : Long(0),
"R" : Long(0),
"W" : Long(0)
},
"deadlockCount" : {
"r" : Long(0),
"w" : Long(0),
"R" : Long(0),
"W" : Long(0)
}
},
"ReplicationStateTransition" : {
...
},
"Global": {
...
},
"Database" : {
...
},
...
}
},
...
],
"fsyncLock": <boolean>,
"info": <string>,
"ok": <num>
}

以下是在副本集的主节点上运行时 currentOp 输出的原型:

{
"inprog": [
{
"type" : <string>,
"host" : <string>,
"desc" : <string>,
"connectionId" : <number>,
"client" : <string>,
"appName" : <string>,
"clientMetadata" : <document>,
"lsid" : {
"id" : <UUID>,
"uid" : <BinData>
},
"transaction" : {
"parameters" : {
"txnNumber" : <Long()>,
"autocommit" : <boolean>,
"readConcern" : {
"level" : <string>
}
},
"readTimestamp" : <Timestamp>,
"startWallClockTime" : <string>,
"timeOpenMicros" : <Long()>,
"timeActiveMicros" : <Long()>,
"timeInactiveMicros" : <Long()>,
"expiryTime" : <string>,
},
"active" : <boolean>,
"currentOpTime" : <string>,
"effectiveUsers" : [
{
"user" : <string>,
"db" : <string>
}
],
"opid" : <number>,
"secs_running" : <Long()>,
"microsecs_running" : <number>,
"op" : <string>,
"ns" : <string>,
"command" : <document>,
"originatingCommand" : <document>,
"queryFramework" : <string>,
"planSummary": <string>,
"prepareReadConflicts" : <Long()>,
"writeConflicts" : <Long()>,
"cursor" : { // only for getMore operations
"cursorId" : <Long()>,
"createdDate" : <ISODate()>,
"lastAccessDate" : <ISODate()>,
"nDocsReturned" : <Long()>,
"nBatchesReturned" : <Long()>,
"noCursorTimeout" : <boolean>,
"tailable" : <boolean>,
"awaitData" : <boolean>,
"originatingCommand" : <document>,
"planSummary" : <string>,
"operationUsingCursorId" : <Long()>
},
"msg": <string>,
"progress" : {
"done" : <number>,
"total" : <number>
},
"killPending" : <boolean>,
"numYields" : <number>,
"dataThroughputLastSecond" : <number>,
"dataThroughputAverage" : <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" : Long(0),
"w" : Long(0),
"R" : Long(0),
"W" : Long(0)
},
"deadlockCount" : {
"r" : Long(0),
"w" : Long(0),
"R" : Long(0),
"W" : Long(0)
}
},
"ReplicationStateTransition" : {
...
},
"Global" : {
...
},
"Database" : {
...
},
...
}
},
...
],
"fsyncLock": <boolean>,
"info": <string>,
"ok": <num>,
"operationTime": <timestamp>,
"$clusterTime": <document>
}

下面是在分片集群的 mongos 上运行时的 currentOp 输出示例(字段可能因报告的操作而异):

{
"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" : <Long()>,
"autocommit" : <boolean>,
"readConcern" : {
"level" : <string>
}
},
"readTimestamp" : <Timestamp>,
"startWallClockTime" : <string>,
"timeOpenMicros" : <Long()>,
"timeActiveMicros" : <Long()>,
"timeInactiveMicros" : <Long()>,
"expiryTime" : <string>,
},
"active" : <boolean>,
"currentOpTime" : <string>,
"effectiveUsers" : [
{
"user" : <string>,
"db" : <string>
}
],
"runBy" : [
{
"user" : <string>,
"db" : <string>
}
],
"twoPhaseCommitCoordinator" : {
"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" : <Long()>,
"microsecs_running" : <number>,
"op" : <string>,
"ns" : <string>,
"command" : <document>,
"configTime" : <Timestamp>, // Starting in 5.0
"topologyTime" : <Timestamp>, // Starting in 5.0
"queryFramework" : <string>, // Starting in 6.2
"planSummary": <string>,
"prepareReadConflicts" : <Long()>,
"writeConflicts" : <Long()>,
"cursor" : { // only for getMore operations
"cursorId" : <Long()>,
"createdDate" : <ISODate()>,
"lastAccessDate" : <ISODate()>,
"nDocsReturned" : <Long()>,
"nBatchesReturned" : <Long()>,
"noCursorTimeout" : <boolean>,
"tailable" : <boolean>,
"awaitData" : <boolean>,
"originatingCommand" : <document>,
"planSummary" : <string>,
"operationUsingCursorId" : <Long()>
},
"msg": <string>,
"progress" : {
"done" : <number>,
"total" : <number>
},
"killPending" : <boolean>,
"numYields" : <number>,
"dataThroughputLastSecond" : <number>,
"dataThroughputAverage" : <number>,
"locks" : {
"ParallelBatchWriterMode" : <string>,
"ReplicationStateTransition" : <string>,
"Global" : <string>,
"Database" : <string>,
"Collection" : <string>,
"Metadata" : <string>,
"DDLDatabase" : <string>,
"DDLCollection" : <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" : Long(0),
"w" : Long(0),
"R" : Long(0),
"W" : Long(0)
},
"deadlockCount" : {
"r" : Long(0),
"w" : Long(0),
"R" : Long(0),
"W" : Long(0)
}
},
"ReplicationStateTransition" : {
...
},
"Global" : {
...
},
"Database" : {
...
},
...
}
},
...
],
"ok": <num>,
"operationTime": <timestamp>,
"$clusterTime": <document>
}

这些输出示例说明了特定操作的 currentOp 输出。构成实际输出的字段因服务器角色而异。

{
shard: '<string>',
totalCopyTimeElapsedSecs: Long('<count>'),
totalApplyTimeElapsedSecs: Long('<count>'),
totalCriticalSectionTimeElapsedSecs: Long('<count>'),
totalIndexBuildTimeElapsedSecs: Long('<count>'),
indexesToBuild: Long('<count>'),
indexesBuilt: Long('<count>'),
oplogEntriesFetched: Long('<count>'),
oplogEntriesApplied: Long('<count>'),
insertsApplied: Long('<count>'),
updatesApplied: Long('<count>'),
deletesApplied: Long('<count>'),
type: 'op',
desc: 'ReshardingMetrics{Donor|Recipient|Coordinator}Service <reshardingUUID>',
op: 'command',
ns: '<database>.<collection>',
originatingCommand: {
reshardCollection: '<database>.<collection>',
key: '<shardkey>',
unique:'<boolean>',
collation: { locale: 'simple' }
},
totalOperationTimeElapsedSecs: Long('<count>'),
recipientState: '<service state>',
remainingOperationTimeEstimatedSecs: Long('<count>'),
approxDocumentsToCopy: Long('<count>'),
approxBytesToCopy: Long('<count>'),
bytesCopied: Long('<count>'),
countWritesToStashCollections: Long('<count>'),
documentsCopied: Long('<count>'),
provenance: 'reshardCollection'
}

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