Docs HomeMongoDB Manual

$type (aggregation)

On this page

Definition

$type

Returns a string that specifies the BSON type of the argument.

$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.4

  • $type (Query) - filters fields based on BSON type.

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. Instead, when passed an array as its argument, the $type aggregation operator returns the type of the argument, i.e. "array".

If the argument is a field that is missing in the input document, $type returns the string "missing".

The following table shows the $type output for several common types of expressions:

ExampleResults
{ $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). Wrapping the array [ 1, 2, 3 ] in a $literal expression achieves the same result.

See operator expression syntax forms for more information.

Available Types

TypeNumberAliasNotes
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"
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".

Example

This example uses a collection named coll with the following documents:

{ _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.

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" }