$lt - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$lt

$lt聚合运算符比较两个值,true如果第一个值小于第二个值,则返回,否则返回false

参数

  • expression1:要比较的第一个值。

  • expression2: 第二个要比较的值。

示例(MongoDB 外壳)

以下示例演示如何使用$lt运算符识别低库存商品。

创建示例文档

db.warehouse.insertMany([ { _id: 1, item: "Bolts", stock: 5 }, { _id: 2, item: "Nuts", stock: 25 }, { _id: 3, item: "Screws", stock: 8 } ]);

查询示例

db.warehouse.aggregate([ { $project: { item: 1, stock: 1, lowStock: { $lt: ["$stock", 10] } } } ]);

输出

[ { _id: 1, item: 'Bolts', stock: 5, lowStock: true }, { _id: 2, item: 'Nuts', stock: 25, lowStock: false }, { _id: 3, item: 'Screws', stock: 8, lowStock: true } ]

代码示例

要查看使用$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('warehouse'); const result = await collection.aggregate([ { $project: { item: 1, stock: 1, lowStock: { $lt: ["$stock", 10] } } } ]).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['warehouse'] result = list(collection.aggregate([ { '$project': { 'item': 1, 'stock': 1, 'lowStock': { '$lt': ['$stock', 10] } } } ])) print(result) client.close() example()