Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$lte
Amazon DocumentDB 中的$lte运算符用于匹配指定字段值小于或等于指定值的文档。此运算符对于根据数值比较筛选和查询数据非常有用。
参数
-
field:要比较的字段。
-
value:要比较的值。
示例(MongoDB 外壳)
以下示例演示如何使用$lte运算符来检索quantity字段小于或等于 10 的文档。
创建示例文档
db.inventory.insertMany([
{ item: "canvas", qty: 100 },
{ item: "paint", qty: 50 },
{ item: "brush", qty: 10 },
{ item: "paper", qty: 5 }
]);
查询示例
db.inventory.find({ qty: { $lte: 10 } });
输出
{ "_id" : ObjectId("..."), "item" : "brush", "qty" : 10 },
{ "_id" : ObjectId("..."), "item" : "paper", "qty" : 5 }
代码示例
要查看使用该$lte命令的代码示例,请选择要使用的语言的选项卡:
- Node.js
-
const { MongoClient } = require("mongodb");
async function main() {
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("inventory");
const result = await collection.find({ qty: { $lte: 10 } }).toArray();
console.log(result);
await client.close();
}
main();
- Python
-
from pymongo import MongoClient
def main():
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["inventory"]
result = list(collection.find({ "qty": { "$lte": 10 } }))
print(result)
client.close()
if __name__ == "__main__":
main()