$isNumber (aggregation)
On this page本页内容
Definition定义
$isNumberNew in version 4.4.4.4版新增。$isNumberchecks if the specified expression resolves to one of the following numeric BSON types:检查指定的表达式是否解析为以下数值BSON类型之一:$isNumberreturns:退货:如果表达式解析为数字,则为trueif the expression resolves to a number.true。如果表达式解析为任何其他BSON类型、falseif the expression resolves to any other BSON type,null, or a missing field.null或缺少字段,则为false。
$isNumberhas the following operator expression syntax:具有以下运算符表达式语法:{ $isNumber: <expression> }The argument can be any valid expression.参数可以是任何有效的表达式。
See also: 另请参阅:
$type (Aggregation)-returns the BSON type of the argument.返回参数的BSON类型。$type (Query)-filters fields based on BSON type.根据BSON类型筛选字段。
Example实例
Use $isNumber to Check if a Field is Numeric使用$isNumber检查字段是否为数字字段
Issue the following operation against the 针对examples.sensors collection to populate test data:examples.sensors集合发出以下操作以填充测试数据:
db.getSiblingDB("examples").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) ]}
])
The following aggregation uses the 以下聚合使用$addFields aggregation stage to add the following fields to each document:$addFields聚合阶段将以下字段添加到每个文档:
isNumber-Indicates whether the value of指示读数的值是整数、十进制、双精度还是长。readingis an integer, decimal, double, or long.type-Indicates the BSON type of指示reading.reading的BSON类型。
db.sensors.aggregate([{
$addFields : {
"isNumber" : { $isNumber : "$reading" },
"hasType" : {$type : "$reading"}
}
}])
The aggregation operation returns the following results:聚合操作返回以下结果:
{ "_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" }
Conditionally Modify Fields using $isNumber使用$isNumber有条件地修改字段
The 成绩集合包含有关学生成绩的数据。grades collection contains data on student grades. The grade field may either store a string letter grade or a numeric point value.grade字段可以存储字符串字母等级或数字点值。
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
}
])
The following aggregation uses the 下面的聚合使用$addFields stage to add a points field containing the numeric grade value for that course. $addFields阶段添加一个包含该课程的数字分数值的points字段。The stage uses the 该阶段使用$cond operator to set the value of points based on the output of $isNumber:$cond运算符根据$isNumber的输出设置points的值:
If如果为true,gradesalready contains the numeric point value. Setpointsequal togrades.true,则grades已包含数值点值。设定points等于grades。If如果为false,gradescontains a string letter value.false,则grades包含字符串字母值。Use使用$switchto convert the letter grade to its equivalent point value and assign topoints.$switch将字母等级转换为其等效的点值并指定给points。
The aggregation pipeline then uses the 然后,聚合管道使用$group stage to group on the student_id and calculate the student's average GPA.$group阶段对student_id进行分组,并计算学生的averageGPA。
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"
}
}
}
])
The aggregation pipeline outputs one document per unique 聚合管道为每个唯一的student_id with that student's GPA grade point average:student_id输出一个文档,该文档具有该学生的平均GPA分数:
{ "_id" : 457864153, "GPA" : 3.5 }
{ "_id" : 978451637, "GPA" : 3 }