$lte - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$lte

Amazon DocumentDB 中的$lte運算子用於比對指定欄位的值小於或等於指定值的文件。此運算子適用於根據數值比較篩選和查詢資料。

參數

  • field:要比較的欄位。

  • value:要比較的值。

範例 (MongoDB Shell)

下列範例示範 $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()