Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

db.currentOp()

在此页面上

  • 定义
  • 兼容性
  • 语法
  • 行为
  • 访问控制
  • 示例
  • 输出示例
  • 特定输出示例
  • 输出字段
db.currentOp()

返回一份文档,其中包含有关数据库实例正在进行的操作的信息。db.currentOp() 方法封装了数据库命令 currentOp

使用 $currentOp 聚合阶段,而不是 currentOp 命令及其 mongosh 辅助方法 db.currentOp()

注意

从 MongoDB 5.0 开始,在运行带有 mongosh 的辅助方法 db.currentOp() 时使用$currentOp 聚合阶段。

鉴于此,在 5.0 版本的 shell 以及使用 mongosh 时,db.currentOp() 结果集不受先前旧版 mongo16 MB BSON 文档返回大小限制的约束。

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

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

重要

M 0 、M 2和 M 5集群不支持此命令。 有关更多信息,请参阅不支持的命令。

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

db.currentOp(<operations>)

db.currentOp() 可以采用以下可选参数:

Parameter
类型
说明

布尔或文档

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

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

  • 指定具有查询条件的文档,以仅报告符合条件的操作。有关详细信息,请参阅行为。

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

如果向 db.currentOp() 传递过滤器文档,输出将只返回与过滤器匹配的当前操作的信息。过滤器文档可以包含:

字段
说明

"$ownOps"

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

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

"$all"

布尔值。如果设置为 true,则返回所有操作的信息,包括对空闲连接的操作和系统操作。

如果文档包含"$all": true 和输出字段条件,则仅"$all":true 适用。

<filter>

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

如果文档包含"$all": true 和输出字段条件,则仅"$all": true 适用。

true 传递给 db.currentOp() 等同于传入 { "$all": true } 文档。以下操作具有同等效果:

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

db.currentOp()数据库分析器会为所有 CRUD 操作报告相同的基本诊断信息,其中包括以下内容:

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

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

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

提示

另请参阅:

以下示例使用 db.currentOp() 方法和各种查询文档来过滤输出结果。

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

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.adminCommand(
{
currentOp: true,
$or: [
{ op: "command", "command.createIndexes": { $exists: true } },
{ op: "command", "command.$truncated": /^\{ createIndexes/ },
{ op: "none", "msg" : /^Index Build/ }
]
}
)

以下是 db.currentOp() 输出的原型。

以下是在独立实例上运行时 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" : <NumberLong()>,
"microsecs_running" : <number>,
"op" : <string>,
"ns" : <string>,
"command" : <document>,
"queryFramework" : <string>,
"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>,
"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" : 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>
}

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

{
"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>,
"queryFramework" : <string>,
"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>,
"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" : 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>
}

下面是在分片集群的 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" : <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" : {
"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>,
"configTime" : <Timestamp>, // Starting in 5.0
"topologyTime" : <Timestamp>, // Starting in 5.0
"queryFramework" : <string>, // Starting in 6.2
"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>,
"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" : 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>
}

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

{
type: "op",
desc: "Resharding{Donor, Recipient, Coordinator}Service <reshardingUUID>",
op: "command",
ns: "<database>.<collection>",
originatingCommand: {
reshardCollection: "<database>.<collection>",
key: <shardkey>,
unique: <boolean>,
collation: {locale: "simple"},
// Other options to the reshardCollection command are omitted
// to decrease the likelihood the output is truncated.
},
{donor, coordinator, recipient}State : "<service state>",
approxDocumentsToCopy: NumberLong(<count>),
approxBytesToCopy: NumberLong(<count>),
bytesCopied: NumberLong(<count>),
countWritesToStashCollections: NumberLong(<count>),
countWritesDuringCriticalSection : NumberLong(<count>),
countReadsDuringCriticalSection: NumberLong(<count>),
deletesApplied: NumberLong(<count>),
documentsCopied: NumberLong(<count>),
insertsApplied: NumberLong(<count>),
oplogEntriesFetched: NumberLong(<count>),
oplogEntriesApplied: NumberLong(<count>),
remainingOperationTimeEstimatedSecs: NumberLong(<count>),
allShardsLowestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>),
allShardsHighestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>),
totalApplyTimeElapsedSecs: NumberLong(<count>),
totalCopyTimeElapsedSecs: NumberLong(<count>),
totalCriticalSectionTimeElapsedSecs : NumberLong(<count>),
totalOperationTimeElapsedSecs: NumberLong(<count>),
updatesApplied: NumberLong(<count>),
}
{
type: "op",
desc: "GlobalIndex{Donor, Recipient, Coordinator}Service <globalIndexUUID}",
op: "command",
ns: "<database>.<collection>",
originatingCommand: {
createIndexes: "<database>.<collection>",
key: <indexkeypattern>,
unique: <boolean>,
<Additional createIndexes options>
},
{donor, coordinator, recipient}State : "<service state>",
approxDocumentsToScan: NumberLong(<count>),
approxBytesToScan: NumberLong(<count>),
bytesWrittenFromScan: NumberLong(<count>),
countWritesToStashCollections: NumberLong(<count>),
countWritesDuringCriticalSection : NumberLong(<count>),
countReadsDuringCriticalSection: NumberLong(<count>),
keysWrittenFromScan: NumberLong(<count>),
remainingOperationTimeEstimatedSecs: NumberLong(<count>),
allShardsLowestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>),
allShardsHighestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>),
totalCopyTimeElapsedSecs: NumberLong(<count>),
totalCriticalSectionTimeElapsedSecs : NumberLong(<count>),
totalOperationTimeElapsedSecs: NumberLong(<count>),
}

有关 db.currentOp() 输出字段的完整列表,请参阅 currentOp

后退

db.createView