Mongodb聚合操作$isNumber

又是mongodb集合的不强制文档具有相同一致的结构产生的问题。Mongodb这种特点,确实带来了灵活强大的优势,但Mongodb的一些操作符和特性看起来在为这种灵活的结构买单。

这次有两个问题。某一个气温监控系统,采用了不同的传感器厂家生产的系统,不同厂家返回的温度数据有不同的类型,有数字类型的,有字符类型的。还有的传感器损坏,返回来了空数据。

另外一个问题是学生在学校考试当中取得的成绩。不同课程成绩按照不同的录入方式进入到系统。有的按照分数评级,录入了ABCDF这五个等级。而有的课程录入了学生绩点。现在到了评奖学籍的时候,要计算学生成绩绩点的平均值。这个为难坏了工程师。

Mongodb聚合操作用,方法$isNumber来判断字段值或表达式的返回值是否为数字类型。

定义

isNumber,判断指定表达式返回值的类型是否是数字类型。若返回数据类型是Integer, Decimal, Double, Long当中的一种,isNumber返回true,其他的非数字类型,null或文档没有该字段,则返回false。

$isNumber使用下面的语法来判断表达式的值。其中expression是任何一个可用的表达式。

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

应用

判断字段值是否为数字类型

有一个传感器表格,记录了下面的信息。其中reading字段中有各种数据类型。

复制代码
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)] }
])

使用聚合查询,为查询结果添加新字段isNumber, 并返回reading字段的类型

复制代码
db.sensors.aggregate([{
    $addFields: { //在聚合查询中添加新字段isNumber和type
        isNumber: {$isNumber: "$reading"}, //判断reading字段是否为数字类型
        type: {$type: "$reading"}
    }
}])

/* 1 */
{
	"_id" : 1,
	"reading" : Decimal128("26.0"),
	"isNumber" : true,
	"type" : "decimal"
},

/* 2 */
{
	"_id" : 2,
	"reading" : Long("25"),
	"isNumber" : true,
	"type" : "long"
},

/* 3 */
{
	"_id" : 3,
	"reading" : 24,
	"isNumber" : true,
	"type" : "int"
},

/* 4 */
{
	"_id" : 4,
	"reading" : 24,
	"isNumber" : true,
	"type" : "int"
},

/* 5 */
{
	"_id" : 5,
	"reading" : "24",
	"isNumber" : false,
	"type" : "string"
},

/* 6 */
{
	"_id" : 6,
	"reading" : [ Decimal128("26") ],
	"isNumber" : false,
	"type" : "array"
}

依据$isNumber的结果修改返回字段值

学生成绩表grade中包含了学生成绩信息。 其中grade字段保存了两种数据类型,一种是字符类型的ABCDF,另一种数字类型的绩点成绩。

复制代码
db.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
}])

现在需要计算两个学生的平均绩点。

  • 为查询结果添加新字段points表示学生的绩点

  • 使用$isNumber判断grade字段是否为数字类型,当为数字类型是,直接返回给points字段,如果不是数字类型, 按照等级对应关系返回给points字段。 A-4.0,B-3.0,C-2.0,D-1.0,F-0.0.

    db.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"
    }
    }
    }])

运行聚合查询,返回结果

复制代码
/* 1 */
{
	"_id" : 978451637,
	"GPA" : Double("3")
},

/* 2 */
{
	"_id" : 457864153,
	"GPA" : 3.5
}
相关推荐
tzhou644521 小时前
MySQL备份与恢复
数据库·mysql·adb
一过菜只因1 小时前
MySql Jdbc
android·数据库·mysql
思成不止于此2 小时前
MySQL 查询实战(三):排序与综合练习
数据库·笔记·学习·mysql
茅坑的小石头2 小时前
数据库表设计,概念模型、逻辑模型、物理模型的区别,目标、主要内容、所处阶段、面向人群,数据库无关性
数据库
tebukaopu1482 小时前
mysql数据备份还原
数据库·mysql
zyxqyy&∞2 小时前
mysql代码小练-2
数据库·mysql
JIngJaneIL2 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
+VX:Fegn08952 小时前
计算机毕业设计|基于springboot + vue心理健康管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
only-qi4 小时前
Redis如何应对 Redis 大 Key 问题
数据库·redis·缓存
muxin-始终如一4 小时前
消息丢失场景和解决方案
数据库·中间件·消息丢失