$type (aggregation)

On this page本页内容

Definition定义

$type

Returns a string that specifies the BSON type of the argument.返回指定参数的BSON类型的字符串。

$type has the following operator expression syntax:具有以下运算符表达式语法

{ $type: <expression> }

The argument can be any valid expression.参数可以是任何有效的表达式

Tip提示
See also: 参阅:
  • $isNumber - checks if the argument is a number. 检查参数是否为数字。New in MongoDB 4.4MongoDB 4.4中的新增功能
  • $type (Query) - filters fields based on BSON type.根据BSON类型筛选字段。

Behavior行为

$type

Unlike the $type query operator, which matches array elements based on their BSON type, the $type aggregation operator does not examine array elements. $type查询运算符根据BSON类型匹配数组元素,与此不同,$type聚合运算符不检查数组元素。Instead, when passed an array as its argument, the $type aggregation operator returns the type of the argument, i.e. "array".相反,当传递数组作为参数时,$type聚合运算符返回参数的类型,即"array"

If the argument is a field that is missing in the input document, $type returns the string "missing".如果参数是输入文档中缺少的字段,$type将返回字符串"missing"

The following table shows the $type output for several common types of expressions:下表显示了几种常见表达式类型的$type输出:

Example示例Results结果
{ $type: "a" }"string"
{ $type: /a/ }"regex"
{ $type: 1 }"double"
{ $type: NumberLong(627) }"long"
{ $type: { x: 1 } }"object"
{ $type: [ [ 1, 2, 3 ] ] }"array"
Note注意

In the case of a literal array such as [ 1, 2, 3 ], enclose the expression in an outer set of array brackets to prevent MongoDB from parsing [ 1, 2, 3 ] as an argument list with three arguments (1, 2, 3). 对于像[ 1, 2, 3 ]这样的文字数组,将表达式括在一组外部的数组括号中,以防止MongoDB将[ 1, 2, 3 ]解析为具有三个参数(1, 2, 3)的参数列表Wrapping the array [ 1, 2, 3 ] in a $literal expression achieves the same result.$literal表达式中包装数组[ 1, 2, 3 ]可以获得相同的结果。

See operator expression syntax forms for more information.有关详细信息,请参阅运算符表达式语法形式

Available Types可用类型

Type类型NumberAliasNotes
Double1"double"
String2"string"
Object3"object"
Array4"array"
Binary data5"binData"
Undefined6"undefined"Deprecated.
ObjectId7"objectId"
Boolean8"bool"
Date9"date"
Null10"null"
Regular Expression11"regex"
DBPointer12"dbPointer"Deprecated.
JavaScript13"javascript"
Symbol14"symbol"Deprecated.
JavaScript code with scope15"javascriptWithScope"Deprecated in MongoDB 4.4.
32-bit integer16"int"
Timestamp17"timestamp"
64-bit integer18"long"
Decimal12819"decimal"New in version 3.4.
Min key-1"minKey"
Max key127"maxKey"

If the argument is a field that is missing in the input document, $type returns the string "missing".如果参数是输入文档中缺少的字段,$type将返回字符串"missing"

Example示例

This example uses a collection named coll with the following documents:此示例将名为coll集合与以下文档一起使用:

{ _id: 0, a : 8 }
{ _id: 1, a : [ 41.63, 88.19 ] }
{ _id: 2, a : { a : "apple", b : "banana", c: "carrot" } }
{ _id: 3, a :  "caribou" }
{ _id: 4, a : NumberLong(71) }
{ _id: 5 }

The following aggregation operation uses the $type operator to display the type of field a for all documents as part of the $project stage.以下聚合操作使用$type运算符显示作为$project阶段一部分的所有文档的字段a的类型。

db.coll.aggregate([{
    $project: {
       a : { $type: "$a" }
    }
}])

The operation returns the following:运算结果如下:

{ _id: 0, "a" : "double" }
{ _id: 1, "a" : "array" }
{ _id: 2, "a" : "object" }
{ _id: 3, "a" : "string" }
{ _id: 4, "a" : "long" }
{ _id: 5, "a" : "missing" }
←  $trunc (aggregation)$unsetField (aggregation) →