本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$lt
Amazon DocumentDB 中的$lt運算子用於比對小於指定值的值。此運算子適用於根據數值比較篩選和查詢資料。
參數
-
field:要檢查的欄位名稱。
-
value:要比較的值。
範例 (MongoDB Shell)
下列範例示範如何使用 $lt運算子擷取Inventory.OnHand值小於 50 的文件。
建立範例文件
db.example.insertMany([
{ "_id": 1, "Inventory": { "OnHand": 25, "Sold": 60 }, "Colors": ["Red", "Blue"] },
{ "_id": 2, "Inventory": { "OnHand": 75, "Sold": 40 }, "Colors": ["Red", "White"] },
{ "_id": 3, "Inventory": { "OnHand": 10, "Sold": 85 }, "Colors": ["Red", "Green"] }
]);
查詢範例
db.example.find({ "Inventory.OnHand": { $lt: 50 } })
輸出
{ "_id" : 1, "Inventory" : { "OnHand" : 25, "Sold" : 60 }, "Colors" : [ "Red", "Blue" ] }
{ "_id" : 3, "Inventory" : { "OnHand" : 10, "Sold" : 85 }, "Colors" : [ "Red", "Green" ] }
程式碼範例
若要檢視使用 $lt命令的程式碼範例,請選擇您要使用的語言標籤:
- 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('example');
const result = await collection.find({ "Inventory.OnHand": { $lt: 50 } }).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['example']
result = list(collection.find({ "Inventory.OnHand": { "$lt": 50 } }))
print(result)
client.close()
example()