$type¶
Definition¶
-
$type¶ $typeselects the documents where the value of thefieldis an instance of the specified BSON type. Querying by data type is useful when dealing with highly unstructured data where data types are not predictable.A
$typeexpression has the following syntax:Changed in version 3.2.
Available Types describes the BSON types and their corresponding numeric and string aliases.
Behavior¶
$type returns documents where the BSON type of the field
matches the BSON type passed to $type.
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” | |
| 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
Arrays¶
When applied to arrays, $type matches any inner element that is
of the specified BSON type. For example, when matching for
$type : 'array', the document will match if the field has a nested array.
It will not return results where the field itself is an array.
See Querying by Array Type for an example.
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:
These queries return:
The following queries return all documents where zipCode is the
BSON type double:
These queries return:
The following query uses the number alias to return documents where
zipCode is the BSON type double, int, or long:
These queries return:
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:
This returns
The following query returns any restaurant whose grades.grade field
contains maxKey:
This returns
Querying by Array Type¶
The SensorReading collection contains the following documents:
The following query returns any document where readings has an
element of BSON type array; i.e. the $type does
not check if readings itself is an array:
This returns the following doucment:
The document with _id : 1 has at least one element in readings
that is an array, whereas the document with _id : 2 does not.