On this page本页内容
$isNumber
¶New in version 4.4.版本4.4中的新功能。
$isNumber
checks if the specified expression resolves to one of the following numeric BSON types:检查指定的表达式是否解析为以下数字BSON类型之一:
$isNumber
returns:返回
true
if the expression resolves to a number.true
。false
if the expression resolves to any other BSON type, null
, or a missing field.null
或缺少字段,则为false
。$isNumber
has the following operator expression syntax:具有以下运算符表达式语法:
The argument can be any valid expression.参数可以是任何有效的表达式。
See also参阅
$type (Aggregation)
- $type (Query)
- $isNumber
to Check If A Field Is Numeric$isNumber
检查字段是否为数字¶Issue the following operation against the 对examples.sensors
collection to populate test data:examples.sensors
集合执行以下操作以填充测试数据:
The following aggregation uses the 以下聚合使用$addFields
aggregation stage to add the following fields to each document:$addFields
聚合阶段向每个文档添加以下字段:
isNumber
- reading
is an integer, decimal, double, or long.type
- reading
.The aggregation operation returns the following results:聚合操作返回以下结果:
$isNumber
$isNumber
有条件地修改字段¶The grades
collection contains data on student grades. grades
集合包含有关学生成绩的数据。The grade
field may either store a string letter grade or a numeric point value.grades
字段可以存储字符串字母等级或数字点值。
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
的值:
true
, grades
already contains the numeric point value. true
,则grades
已包含该数值。points
equal to grades
.points
等于grades
。false
, grades
contains a string letter value. false
,则grades
包含一个字符串字母值。$switch
to convert the letter grade to its equivalent point value and assign to points
.$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
进行分组,并计算学生的平均GPA。
The aggregation pipeline outputs one document per unique 聚合管道为每个唯一的student_id
with that student’s GPA
grade point average:student_id
输出一个文档,该学生的GPA
平均分为: