$gte - Amazon DocumentDB

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

$gte

Amazon DocumentDB の $gte演算子は、指定された値以上の値を一致させるために使用されます。この演算子は、数値比較に基づいてデータをフィルタリングおよびクエリするのに役立ちます。

パラメータ

  • field: 指定された値と照合するフィールド。

  • value: フィールドと比較する値。

例 (MongoDB シェル)

次の例は、Amazon DocumentDB で $gte演算子を使用して、「age」フィールドが 25 以上であるすべてのドキュメントを検索する方法を示しています。

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

db.users.insertMany([ { _id: 1, name: "John", age: 20 }, { _id: 2, name: "Jane", age: 25 }, { _id: 3, name: "Bob", age: 30 }, { _id: 4, name: "Alice", age: 35 } ]);

クエリの例

db.users.find({ age: { $gte: 25 } }, { _id: 0, name: 1, age: 1 });

出力

{ "name" : "Jane", "age" : 25 } { "name" : "Bob", "age" : 30 } { "name" : "Alice", "age" : 35 }

コードの例

$gte コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

Node.js
const { MongoClient } = require('mongodb'); async function findUsersAboveAge(age) { 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: { $gte: age } }, { projection: { _id: 0, name: 1, age: 1 } }).toArray(); console.log(result); await client.close(); } findUsersAboveAge(25);
Python
from pymongo import MongoClient def find_users_above_age(age): 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': { '$gte': age } }, { '_id': 0, 'name': 1, 'age': 1 })) print(result) client.close() find_users_above_age(25)