$gt - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$gt

Operator $gt agregasi membandingkan dua nilai dan kembali true jika nilai pertama lebih besar dari yang kedua, jika tidak kembali. false

Parameter

  • expression1: Nilai pertama untuk membandingkan.

  • expression2: Nilai kedua untuk membandingkan.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan penggunaan $gt operator untuk mengidentifikasi produk yang melebihi ambang harga.

Buat dokumen sampel

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

Contoh kueri

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

Keluaran

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

Contoh kode

Untuk melihat contoh kode untuk menggunakan operator $gt agregasi, pilih tab untuk bahasa yang ingin Anda gunakan:

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()