翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$gte
$gte 集計演算子は 2 つの値を比較し、最初の値が 2 番目以上のtrue場合は を返し、それ以外の場合は を返しますfalse。
パラメータ
例 (MongoDB シェル)
次の例は、 $gte演算子を使用して、学生が試験に合格したかどうかを確認する方法を示しています。
サンプルドキュメントを作成する
db.students.insertMany([
{ _id: 1, name: "Alice", score: 85 },
{ _id: 2, name: "Bob", score: 60 },
{ _id: 3, name: "Charlie", score: 72 }
]);
クエリの例
db.students.aggregate([
{
$project: {
name: 1,
score: 1,
passed: { $gte: ["$score", 70] }
}
}
]);
出力
[
{ _id: 1, name: 'Alice', score: 85, passed: true },
{ _id: 2, name: 'Bob', score: 60, passed: false },
{ _id: 3, name: 'Charlie', score: 72, passed: true }
]
コードの例
$gte 集計演算子を使用するコード例を表示するには、使用する言語のタブを選択します。
- 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('students');
const result = await collection.aggregate([
{
$project: {
name: 1,
score: 1,
passed: { $gte: ["$score", 70] }
}
}
]).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['students']
result = list(collection.aggregate([
{
'$project': {
'name': 1,
'score': 1,
'passed': { '$gte': ['$score', 70] }
}
}
]))
print(result)
client.close()
example()