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 }
相关推荐
伤不起bb1 小时前
MySQL 高可用
linux·运维·数据库·mysql·安全·高可用
Yushan Bai6 小时前
ORACLE RAC环境REDO日志量突然增加的分析
数据库·oracle
躺着听Jay6 小时前
Oracle-相关笔记
数据库·笔记·oracle
瀚高PG实验室7 小时前
连接指定数据库时提示not currently accepting connections
运维·数据库
运维成长记7 小时前
mysql数据库-中间件MyCat
数据库·mysql·中间件
尘客.8 小时前
DataX从Mysql导数据到Hive分区表案例
数据库·hive·mysql
TiDB 社区干货传送门9 小时前
从开发者角度看数据库架构进化史:JDBC - 中间件 - TiDB
数据库·oracle·中间件·tidb·数据库架构
虾球xz9 小时前
游戏引擎学习第280天:精简化的流式实体sim
数据库·c++·学习·游戏引擎
uwvwko9 小时前
BUUCTF——web刷题第一页题解
android·前端·数据库·php·web·ctf
扶尔魔ocy10 小时前
【Linux C/C++开发】轻量级关系型数据库SQLite开发(包含性能测试代码)
linux·数据库·c++·sqlite