Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$gte
Amazon DocumentDB 中的$gte运算符用于匹配大于或等于指定值的值。此运算符可用于根据数值比较筛选和查询数据。
参数
-
field:要根据提供的值进行检查的字段。
-
value:要与字段进行比较的值。
示例(MongoDB 外壳)
以下示例演示如何在 Amazon DocumentDB 中使用$gte运算符来查找 “年龄” 字段大于或等于 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)