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 }
相关推荐
中微极客3 小时前
Veo视频生成与Gemini Agent平台集成实践
数据库·人工智能·oracle·音视频
一棵星3 小时前
内网 MySQL 表结构一键导出 Word 文档:直连 + Agent 双模式实战
数据库·mysql·word
向日的葵0064 小时前
Redis会话机制vsJWT机制深度解析
数据库·redis·python·缓存·系统架构·jwt
bosins4 小时前
如何配置SQL Server数据库定时自动备份
数据库
烟雨归来4 小时前
RAC数据库OS进程消失,GV$SESSION长期残留KILLED僵尸会话处理
数据库·oracle
小龙报5 小时前
【优选算法】1. 水果成蓝 2.找到字符串中所有字母的异位词
java·c语言·数据结构·数据库·c++·redis·算法
宇宙第一小趴菜5 小时前
六、Oracle 目录结构
数据库·oracle
IvorySQL5 小时前
PG 日报|修复高 IO 并发场景,解决预读机制耗尽本地缓冲区缺陷
数据库·人工智能·sql·postgresql·区块链
TiDB 社区干货传送门5 小时前
TiDB 8.5.7 发版|六项实用功能上线
数据库·mysql·tidb
在繁华处5 小时前
MySQL 学习笔记 01:从概念到约束,搭好库表骨架
数据库·oracle