Database Manual / Reference / mongosh Methods / Cursors

cursor.comment() (mongosh method方法)

Definition定义

cursor.comment()

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Adds a comment field to the query.在查询中添加comment字段。

cursor.comment() has the following syntax:具有以下语法:

cursor.comment( <string> )

comment() has the following parameter:具有以下参数:

Parameter参数Type类型Description描述
commentstring字符串The comment to apply to the query.应用于查询的注释。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Behavior行为

comment() associates a comment string with the find operation. This can make it easier to track a particular query in the following diagnostic outputs:将注释字符串与查找操作相关联。这可以更容易地在以下诊断输出中跟踪特定查询:

See configure log verbosity for the mongod log, the Database Profiler tutorial, or the db.currentOp() command.请参阅mongod日志的配置日志详细程度数据库探查器教程db.currentOp()命令。

Example示例

The following operation attaches a comment to a query on the restaurants collection:以下操作将注释附加到restaurants集合的查询中:

db.restaurants.find(
{ "borough" : "Manhattan" }
).comment( "Find all Manhattan restaurants" )

Output Examples输出示例

system.profile

The following is an excerpt from the system.profile:以下是system.profile的摘录:

{
"op" : "query",
"ns" : "guidebook.restaurant",
"query" : {
"find" : "restaurant",
"filter" : {
"borough" : "Manhattan"
},
"comment" : "Find all Manhattan restaurants"
},
...
}

mongod log

The following is an excerpt from the mongod log. It has been formatted for readability.以下是蒙古日志的摘录。它的格式是为了可读性。

Important

The verbosity level for QUERY must be greater than 0. See Configure Log Verbosity LevelsQUERY的详细程度必须大于0。请参阅配置日志详细程度级别

2015-11-23T13:09:16.202-05:00 I COMMAND  [conn1]
command guidebook.restaurant command: find {
find: "restaurant",
filter: { "borough" : "Manhattan" },
comment: "Find all Manhattan restaurants"
}
...

db.currentOp()

Suppose the following operation is currently running on a mongod instance:假设以下操作当前正在mongod实例上运行:

db.restaurants.find(
{ "borough" : "Manhattan" }
).comment("Find all Manhattan restaurants")

Running the db.currentOp() command returns the following:运行db.currentOp()命令返回以下结果:

{
"inprog" : [
{
"host" : "198.51.100.1:27017",
"desc" : "conn3",
"connectionId" : 3,
...

"op" : "query",
"ns" : "test.$cmd",
"command" : {
"find" : "restaurants",
"filter" : {
"borough" : "Manhattan"
},
"comment" : "Find all Manhattan restaurants",
"$db" : "test"
},
"numYields" : 0,
...
}
],
"ok" : 1
}