$type

Definition

$type

$type selects the documents where the value of the field is an instance of the specified BSON type(s). Querying by data type is useful when dealing with highly unstructured data where data types are not predictable.

A $type expression for a single BSON type has the following syntax:

Changed in version 3.2.

{ field: { $type: <BSON type> } }

You can specify either the number or alias for the BSON type

The $type expression can also accept an array of BSON types and has the following syntax:

{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }

The above query will match documents where the field value is any of the listed types. The types specified in the array can be either numeric or string aliases.

See Querying by Multiple Data Type for an example.

Available Types describes the BSON types and their corresponding numeric and string aliases.

See also

If you wish to obtain the BSON type returned by an operator expression rather than filtering documents by their BSON type, use the $type aggregation operator.

Behavior

$type returns documents where the BSON type of the field matches the BSON type passed to $type.

Changed in version 3.6.

$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.

Available Types

Changed in version 3.2: $type operator accepts string aliases for the BSON types in addition to the numbers corresponding to the BSON types. Previous versions only accepted the numbers corresponding to the BSON type.

Type Number Alias Notes
Double 1 “double”  
String 2 “string”  
Object 3 “object”  
Array 4 “array”  
Binary data 5 “binData”  
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”  
Boolean 8 “bool”  
Date 9 “date”  
Null 10 “null”  
Regular Expression 11 “regex”  
DBPointer 12 “dbPointer” Deprecated.
JavaScript 13 “javascript”  
Symbol 14 “symbol” Deprecated.
JavaScript (with scope) 15 “javascriptWithScope”  
32-bit integer 16 “int”  
Timestamp 17 “timestamp”  
64-bit integer 18 “long”  
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”  
Max key 127 “maxKey”  

$type supports the number alias, which will match against the following BSON types:

  • double
  • 32-bit integer
  • 64-bit integer
  • decimal

See Querying by Data Type

MinKey and MaxKey

MinKey and MaxKey are used in comparison operations and exist primarily for internal use. For all possible BSON element values, MinKey will always be the smallest value while MaxKey will always be the greatest value.

Querying for minKey or maxKey with $type will only return fields that match the special MinKey or MaxKey values.

Suppose that the data collection has two documents with MinKey and MaxKey:

{ "_id" : 1, x : { "$minKey" : 1 } }
{ "_id" : 2, y : { "$maxKey" : 1 } }

The following query will return the document with _id: 1:

db.data.find( { x: { $type: "minKey" } } )

The following query will return the document with _id: 2:

db.data.find( { y: { $type: "maxKey" } } )

Examples

Querying by Data Type

The addressBook contains addresses and zipcodes, where zipCode has string, int, double, and long values:

db.addressBook.insertMany(
   [
      { "_id" : 1, address : "2030 Martian Way", zipCode : "90698345" },
      { "_id" : 2, address: "156 Lunar Place", zipCode : 43339374 },
      { "_id" : 3, address : "2324 Pluto Place", zipCode: NumberLong(3921412) },
      { "_id" : 4, address : "55 Saturn Ring" , zipCode : NumberInt(88602117) }
   ]
)

The following queries return all documents where zipCode is the BSON type string:

db.addressBook.find( { "zipCode" : { $type : 2 } } );
db.addressBook.find( { "zipCode" : { $type : "string" } } );

These queries return:

{ "_id" : 1, "address" : "2030 Martian Way", "zipCode" : "90698345" }

The following queries return all documents where zipCode is the BSON type double:

db.addressBook.find( { "zipCode" : { $type : 1 } } )
db.addressBook.find( { "zipCode" : { $type : "double" } } )

These queries return:

{ "_id" : 2, "address" : "156 Lunar Place", "zipCode" : 43339374 }

The following query uses the number alias to return documents where zipCode is the BSON type double, int, or long:

db.addressBook.find( { "zipCode" : { $type : "number" } } )

These queries return:

{ "_id" : 2, "address" : "156 Lunar Place", "zipCode" : 43339374 }
{ "_id" : 3, "address" : "2324 Pluto Place", "zipCode" : NumberLong(3921412) }
{ "_id" : 4, "address" : "55 Saturn Ring", "zipCode" : 88602117 }

Querying by Multiple Data Type

The grades collection contains names and averages, where classAverage has string, int, and double values:

db.grades.insertMany(
   [
      { "_id" : 1, name : "Alice King" , classAverage : 87.333333333333333 },
      { "_id" :