element
Element Operators List
Name
Description
$exists
Matches documents that have the specified field.
$type
Selects documents if a field is of the specified type.
$exists
Definition
Syntax: { field: { $exists: } }
When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field. $exists does not correspond to SQL operator exists. For SQL exists, refer to the $in operator.
Example of Exists and Not Equal To
Consider the following example:
This query will select all documents in the inventory collection where the qty field exists and its value does not equal 5 or 15.
Null Values
The following examples uses a collection named records with the following documents:
$exists: true
The following query specifies the query predicate a: { $exists: true }:
tim.X("inventory").Get( { a: { $exists: true } } ) The results consist of those documents that contain the field a, including the document whose field a contains a null value:
$exists: false
The following query specifies the query predicate b: { $exists: false }:
tim.X("inventory").Get( { b: { $exists: false } } ) The results consist of those documents that do not contain the field b:
$type
Definition
$type selects the documents where the value of the field is an instance of the specified JSON 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 JSON type has the following syntax:
You can specify either the number or alias for the JSON type
The $type expression can also accept an array of JSON types and has the following syntax:
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 JSON types and their corresponding numeric and string aliases.
If you wish to obtain the JSON type returned by an operator expression rather than filtering documents by their JSON type, use the $type aggregation operator.
Behavior
$type returns documents where the JSON type of the field matches the JSON type passed to $type.
$type now works with arrays in the same way it works with other JSON types. Previous versions only matched documents where the field contained a nested array.
Available Types
$type operator accepts string aliases for the JSON types in addition to the numbers corresponding to the JSON types. Previous versions only accepted the numbers corresponding to the JSON 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”
Min key
-1
“minKey”
Max key
127
“maxKey”
$type supports the number alias, which will match against the following JSON 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 JSON 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:
Example of 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 JSON type string:
These queries return:
The following queries return all documents where zipCode is the JSON type double:
These queries return:
The following query uses the number alias to return documents where zipCode is the JSON type double, int, or long:
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 JSON type string or double. 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:
This returns
The following query returns any restaurant whose grades.grade field contains maxKey:
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.
Last updated