MongoDB聚合运算符:$isNumber

文章目录

    • 语法
    • 举例
      • [用`isNumber\`检查字段是否是数值类型](#用`isNumber`检查字段是否是数值类型)
      • [用`isNumber\`有条件的修改字段值](#用`isNumber`有条件的修改字段值)

$isNumber聚合运算符返回操作数是否是一个数值(Integer、Decimal、Double、Long),返回一个布尔值,如果是数值返回true,否则返回false

语法

js 复制代码
{ $isNumber: <expression> }

举例

用$isNumber检查字段是否是数值类型

使用下面的脚本创建sensors集合:

js 复制代码
db.sensors.insertMany([
  { "_id" : 1, "reading" : NumberDecimal(26.0) },
  { "_id" : 2, "reading" : NumberLong(25.0) },
  { "_id" : 3, "reading" : NumberInt(24) },
  { "_id" : 4, "reading" : 24.0 },
  { "_id" : 5, "reading" : "24" },
  { "_id" : 6, "reading" : [ NumberDecimal(26) ]}
])

下面的聚合使用$addFields阶段添加下面两个字段:

  • isNumber:表示reading字段是否为数值类型
  • type:表示reading的BSON类型
js 复制代码
db.sensors.aggregate([{
  $addFields : {
    "isNumber" : { $isNumber : "$reading" },
    "hasType" : {$type : "$reading"}
  }
}])

聚合操作返回下面的结果:

json 复制代码
{ "_id" : 1, "reading" : NumberDecimal("26.0000000000000"), "isNum " : true, "type" : "decimal" }
{ "_id" : 2, "reading" : NumberLong(25), "isNum " : true, "type" : "long" }
{ "_id" : 3, "reading" : 24, "isNum " : true, "type" : "int" }
{ "_id" : 4, "reading" : 24, "isNum " : true, "type" : "double" }
{ "_id" : 5, "reading" : "24", "isNum " : false, "type" : "string" }
{ "_id" : 6, "reading" : [ NumberDecimal("26.0000000000000") ], "isNum " : false, "type" : "array" }

用$isNumber有条件的修改字段值

使用下面的脚本创建grades集合,包含了学生成绩的数据,grade字段可能是数值类型的分数或字符类型的级别:

js 复制代码
db.getSiblingDB("examples").grades.insertMany([
  {
    "student_id" : 457864153,
    "class_id" : "M044",
    "class_desc" : "Introduction to MongoDB 4.4",
    "grade" : "A"
  },
  {
    "student_id" : 457864153,
    "class_id" : "M103",
    "class_desc" : "Basic Cluster Administration",
    "grade" : 3.0
  },
  {
    "student_id" : 978451637,
    "class_id" : "M320",
    "class_desc" : "MongoDB Data Modeling",
    "grade" : "C"
  },
  {
    "student_id" : 978451637,
    "class_id" : "M001",
    "class_desc" : "MongoDB Basics",
    "grade" : 4.0
  }
])

下面的聚合使用$addFields阶段添加points字段,包含了课程的成绩值。该阶段使用$cond运算符根据$isNumber的输出设置points的值:

  • 如果为 true,则成绩已包含数字分值。设定分数等于成绩。
  • 如果为 false,则 Grades包含字符串字母值。使用$switch将字母等级转换为其等效的分数,并赋予points。

然后,聚合管道使用$group阶段对Student_id进行分组并计算学生的平均GPA。

js 复制代码
 db.getSiblingDB("examples").grades.aggregate([
   {
     $addFields: {
       "points" : {
         $cond : {
           if : { $isNumber : "$grade" },
           then: "$grade" ,
           else: {
             $switch : {
               branches: [
                 { case: {$eq : ["$grade" , "A"]}, then : 4.0 },
                 { case: {$eq : ["$grade" , "B"]}, then : 3.0 },
                 { case: {$eq : ["$grade" , "C"]}, then : 2.0 },
                 { case: {$eq : ["$grade" , "D"]}, then : 1.0 },
                 { case: {$eq : ["$grade" , "F"]}, then : 0.0 }
               ]
             }
           }
         }
       }
     }
   },
   {
     $group : {
       _id : "$student_id",
       GPA : {
         $avg : "$points"
       }
     }
   }
])

聚合管道为每个唯一的student_id输出一份文档以及该学生的 GPA 平均绩点:

json 复制代码
{ "_id" : 457864153, "GPA" : 3.5 }
{ "_id" : 978451637, "GPA" : 3 }
相关推荐
希和20273 小时前
江科大 51 单片机笔记:DS18B20 温度传感器 + LCD1602 显示代码详解
笔记·单片机·mongodb
城北徐宫9 小时前
MySQL数据类型整理
数据库·mysql
数据库小学妹10 小时前
数据库容量规划:四个数与扩容触发线
运维·数据库·后端
时间的拾荒人10 小时前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
纆兰13 小时前
MySQL的多表查询
数据库·mysql
七夜zippoe13 小时前
Nexent 模型配置:4 字段与 9 个槽位
数据库·人工智能·模型·nexent
进击的荆棘13 小时前
MySQL——表的操作
数据库·mysql
nice_lcj52015 小时前
MySQL 慢查询与 EXPLAIN:怎么定位、怎么优化
java·数据库·后端·mysql
OceanRoy1 天前
数据库表的设计
数据库·内连接·外连接·er图·表的约束·三大范式·多表连接
Pniubi1 天前
怎么排查慢SQL,怎么解决?
java·数据库·sql