$type¶
Definition¶
-
$type¶ $typeselects documents where the value of thefieldis 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
$typeexpression for a single BSON type has the following syntax:Changed in version 3.2.
You can specify either the number or alias for the BSON type
The
$typeexpression can also accept an array of BSON types and has the following syntax:The above query will match documents where the
fieldvalue 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
$isNumber- checks if the argument is a number. New in MongoDB 4.4$type (Aggregation)- returns the BSON type of the argument.
Behavior¶
$type returns documents where the BSON type of the field
matches the BSON type passed to $type.
Arrays¶
For documents where field is an array, $type returns
documents in which at least one array element matches a type passed to
$type.
Querying for the Array BSON Type¶
With MongoDB 3.6 and later, querying for $type: "array" returns
documents where the field itself is an array. Prior to MongoDB 3.6,
$type: "array" returned documents where the field is an array
containing at least one element of type array. For example, given
the following documents:
With MongoDB 3.6 and later, the query
find( {"data" : { $type : "array" } } ) returns both documents.
Prior to MongoDB 3.6, the query returns only the first document.
Available Types¶
Starting in MongoDB 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. [1]
| 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 code with scope | 15 | “javascriptWithScope” | Deprecated in MongoDB 4.4. |
| 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:
For examples, see Examples.
| [1] | Starting in MongoDB 4.2, users can no longer use the query filter
$type: 0 as a synonym for
$exists:false. To query for null or missing fields, see
Query for Null or Missing Fields. |
See also
$isNumber New in MongoDB 4.4
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:
The following query will return the document with _id: 1:
The following query will return the document with _id: 2:
Examples¶
Querying by Data Type¶
The addressBook contains addresses and zipcodes, where
zipCode has string, int, double, and long
values:
The following queries return all documents where zipCode is the
BSON type string or is an array containing an element of
the specified type:
These queries return:
The following queries return all documents where zipCode is the
BSON type double or is an array containing an element of
the specified type:
These queries return:
The following query uses the number alias to return documents where
zipCode is the BSON type double, int, or long
or is an array containing an element of the specified types:
These queries return:
Querying by Multiple Data Type¶
The grades collection contains names and averages, where
classAverage has string, int, and double values:
The following queries return all documents where classAverage is the
BSON type string or double or is an array containing
an element of the specified types. The first query uses numeric aliases
while the second query uses string aliases.
These queries return the following documents:
Querying by MinKey and MaxKey¶
The restaurants collection uses minKey for any grade that is a
failing grade:
And maxKey for any grade that is the highest passing grade:
The following query returns any restaurant whose grades.grade field
contains minKey or is an array containing an element of
the specified type:
This returns
The following query returns any restaurant whose grades.grade field
contains maxKey or is an array containing an element of
the specified type:
This returns
Querying by Array Type¶
A collection named SensorReading contains the following documents:
The following query returns any document in which the readings
field is an array, empty or non-empty.
The above query returns the following documents:
In the documents with _id : 1, _id : 2, _id : 3, and
_id : 4, the readings field is an array.