MongoDB聚合运算符:$lt

MongoDB聚合运算符:$lt

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

语法

{ $lt: [ <expression1>, <expression2> ] }

$lt可以用来比较任何类型的值,针对不同的类型使用特定的BSON比较顺序。

举例

inventory集合有下列文档:

{ "_id" : 1, "item" : "abc1", "description": "product 1", "qty": 300 }
{ "_id" : 2, "item" : "abc2", "description": "product 2", "qty": 200 }
{ "_id" : 3, "item" : "xyz1", "description": "product 3", "qty": 250 }
{ "_id" : 4, "item" : "VWZ1", "description": "product 4", "qty": 300 }
{ "_id" : 5, "item" : "VWZ2", "description": "product 5", "qty": 180 }

下面的聚合操作使用$lt运算符来判断qty是否小于250

db.inventory.aggregate(
   [
     {
       $project:
          {
            item: 1,
            qty: 1,
            qtyLt250: { $lt: [ "$qty", 250 ] },
            _id: 0
          }
     }
   ]
)

操作返回下面的结果:

{ "item" : "abc1", "qty" : 300, "qtyLt250" : false }
{ "item" : "abc2", "qty" : 200, "qtyLt250" : true }
{ "item" : "xyz1", "qty" : 250, "qtyLt250" : false }
{ "item" : "VWZ1", "qty" : 300, "qtyLt250" : false }
{ "item" : "VWZ2", "qty" : 180, "qtyLt250" : true }

相关推荐

  1. MongoDB聚合运算符:$lt

    2024-04-02 07:42:04       39 阅读
  2. MongoDB聚合运算符:$ln

    2024-04-02 07:42:04       44 阅读
  3. MongoDB聚合运算符:$lte

    2024-04-02 07:42:04       35 阅读
  4. MongoDB聚合运算符:$add

    2024-04-02 07:42:04       55 阅读
  5. MongoDB聚合运算符:$arrayToObject

    2024-04-02 07:42:04       53 阅读
  6. MongoDB聚合运算符;$dateToParts

    2024-04-02 07:42:04       53 阅读
  7. MongoDB聚合运算符:$dayOfWeek

    2024-04-02 07:42:04       55 阅读
  8. MongoDB聚合运算符:$dayOfMonth

    2024-04-02 07:42:04       52 阅读
  9. MongoDB聚合运算符;$dateToString

    2024-04-02 07:42:04       48 阅读
  10. MongoDB聚合运算符:$dayOfYear

    2024-04-02 07:42:04       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 07:42:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 07:42:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 07:42:04       87 阅读
  4. Python语言-面向对象

    2024-04-02 07:42:04       96 阅读

热门阅读

  1. 云计算概述报告

    2024-04-02 07:42:04       27 阅读
  2. 在课堂中使用 ChatGPT 的 80 个方式(下)

    2024-04-02 07:42:04       34 阅读
  3. 上海大学通信829考研踩坑记录

    2024-04-02 07:42:04       30 阅读
  4. Node.js常用命令

    2024-04-02 07:42:04       35 阅读
  5. R语言nc转tif

    2024-04-02 07:42:04       44 阅读
  6. Kotlin 中的构造方法

    2024-04-02 07:42:04       44 阅读