翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Amazon DocumentDB の $comment演算子は、クエリにコメントをアタッチするために使用されます。これは、クエリに関する追加のコンテキストや情報を提供するのに役立ちます。これは、デバッグやドキュメント化の目的で役立ちます。添付されたコメントは、db.currentOp() などのオペレーションの出力の一部として表示されます。
パラメータ
次の例は、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()