cursor.comment()

On this page本页内容

Definition定义

cursor.comment()

mongo Shell Method方法

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

New in version 3.2.版本3.2中的新功能。

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描述
comment string The comment to apply to the query.要应用于查询的注释。

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.

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. 以下是mongod日志的摘录。It has been formatted for readability.它的格式是为了可读性。

Important重要的

The verbosity level for QUERY must be greater than 0. See Configure Log Verbosity Levels

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
}