$comment - Amazon DocumentDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$comment

Amazon DocumentDB 中的$comment运算符用于为查询添加注释。这对于提供有关查询的其他上下文或信息很有用,这可能有助于调试或记录目的。所附注释将作为诸如 db.currenTop () 之类的操作输出的一部分出现。

参数

  • string:查询所附的评论。

示例(MongoDB 外壳)

以下示例演示了如何在 Amazon DocumentDB 中使用$comment运算符。

创建示例文档

db.users.insertMany([ { name: "John Doe", age: 30, email: "john.doe@example.com" }, { name: "Jane Smith", age: 25, email: "jane.smith@example.com" }, { name: "Bob Johnson", age: 35, email: "bob.johnson@example.com" } ]);

查询示例

db.users.find({ age: { $gt: 25 } }, { _id: 0, name: 1, age: 1 }).comment("Retrieve users older than 25");

输出

{ "name" : "John Doe", "age" : 30 } { "name" : "Bob Johnson", "age" : 35 }

代码示例

要查看使用该$comment命令的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const users = db.collection('users'); const result = await users.find({ age: { $gt: 25 } }, { projection: { _id: 0, name: 1, age: 1 } }) .comment('Retrieve users older than 25') .toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient def main(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test users = db.users result = list(users.find({ 'age': { '$gt': 25 }}, { '_id': 0, 'name': 1, 'age': 1 }) .comment('Retrieve users older than 25')) print(result) client.close() if __name__ == '__main__': main()