On this page本页内容
$comment
The $comment
query operator associates a comment to any expression taking a query predicate.$comment
查询运算符将注释与采用查询谓词的任何表达式相关联。
Because comments propagate to the 由于注释传播到profile
log, adding a comment can make your profile data easier to interpret and trace.profile
日志,添加注释可以使配置文件数据更易于解释和跟踪。
The $comment
operator has the form:$comment
运算符的形式如下:
db.collection.find( { <query>, $comment: <comment> } )
You can use the 您可以将$comment
with any expression taking a query predicate, such as the query predicate in db.collection.updateOne()
or in the $match
stage of the aggregation pipeline. $comment
与任何使用查询谓词的表达式一起使用,例如db.collection.updateOne()
中的查询谓词或聚合管道的$match
阶段中的查询断言。For an example, see Attach a Comment to an Aggregation Expression.有关示例,请参阅将注释附加到聚合表达式。
find
The following example adds a 以下示例将$comment
to a find()
operation:$comment
添加到find()
操作中:
db.records.find( { x: { $mod: [ 2, 0 ] }, $comment: "Find even values." } )
If the Database Profiler is enabled, the following output shows the comment in the 如果启用了数据库探查器,则以下输出将显示system.profile
collection:system.profile
集合中的注释:
{ "op" : "query", "ns" : "test.records", "command" : { "find" : "records", "filter" : { "x" : { "$mod" : [ 2, 0 ] }, "$comment" : "Find even values." }, "comment" : "Find even values.", ...
Comments also appear in the MongoDB log if the database profiler level is set to 2 and slowms is set to 0ms. 如果数据库探查器级别设置为2,并且slowms设置为0ms,则注释也会出现在MongoDB日志中。This 此db.setProfilingLevel()
command sets these two parameters:db.setProfilingLevel()
命令设置这两个参数:
db.setProfilingLevel(2, 0)
The comment for the previous 上一个db.records.find()
example then appears as follows in the MongoDB log:db.records.find()
示例的注释在MongoDB日志中显示如下:
{"t":{"$date":"2020-09-17T11:32:20.415-07:00"},"s":"I", "c":"COMMAND", "id":51803, "ctx":"conn7","msg":"Slow query", "attr":{"type":"command","ns":"test.records","appName":"MongoDB Shell","command":{"find":"records","filter":{"x":{"$mod":[2.0,0.0]}, "$comment":"Find even values."},"comment":"Find even values." ...
You can use the 您可以将$comment
with any expression taking a query predicate.$comment
用于接受查询谓词的任何表达式。
The following example uses the 以下示例使用$comment
operator in the $match
stage to clarify the operation:$match
阶段中的$comment
运算符来阐明操作:
db.records.aggregate( [ { $match: { x: { $gt: 0 }, $comment: "Don't allow negative inputs." } }, { $group : { _id: { $mod: [ "$x", 2 ] }, total: { $sum: "$x" } } } ] )