$gt - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$gt

$gt 集計演算子は 2 つの値を比較し、最初の値が 2 番目より大きいtrue場合は を返し、それ以外の場合は を返しますfalse

パラメータ

  • expression1: 比較する最初の値。

  • expression2: 比較する 2 番目の値。

例 (MongoDB シェル)

次の例は、 $gt演算子を使用して、価格しきい値を超える製品を識別する方法を示しています。

サンプルドキュメントを作成する

db.products.insertMany([ { _id: 1, name: "Laptop", price: 1200 }, { _id: 2, name: "Mouse", price: 25 }, { _id: 3, name: "Monitor", price: 400 } ]);

クエリの例

db.products.aggregate([ { $project: { name: 1, price: 1, expensive: { $gt: ["$price", 100] } } } ]);

出力

[ { _id: 1, name: 'Laptop', price: 1200, expensive: true }, { _id: 2, name: 'Mouse', price: 25, expensive: false }, { _id: 3, name: 'Monitor', price: 400, expensive: true } ]

コードの例

$gt 集計演算子を使用するコード例を表示するには、使用する言語のタブを選択します。

Node.js
const { MongoClient } = require('mongodb'); async function example() { 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 collection = db.collection('products'); const result = await collection.aggregate([ { $project: { name: 1, price: 1, expensive: { $gt: ["$price", 100] } } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['products'] result = list(collection.aggregate([ { '$project': { 'name': 1, 'price': 1, 'expensive': { '$gt': ['$price', 100] } } } ])) print(result) client.close() example()