Overview of Filter Comparison Operators
A comparison operator tests whether a given JSON object field satisfies some conditions.
One of the simplest and most useful filter specifications tests a field for equality to a specific value. For example, this filter specification matches any document that has a field name whose value is "Jason". It uses the operator $eq, which tests field-value equality.
{ "name" : { "$eq" : "Jason" } }
For convenience, for such a scalar-equality filter you can generally omit operator $eq. This scalar-equality filter specification is thus equivalent to the preceding one, which uses $eq:
{ "name" : "Jason" }
Both of the preceding filter specifications match Example 2-1.
The comparison operators are the following:
-
$all— whether an array field value contains all of a set of values -
$between— whether a field value is between two string or number values (inclusive) -
$eq— whether a field value is equal to a given scalar -
$exists— whether a given field exists -
$gt— whether a field value is greater than a given scalar value -
$gte— whether a field value is greater than or equal to a given scalar -
$hasSubstring— whether a string field value has a given substring (same as$instr) -
$in— whether a field value is a member of a given set of scalar values -
$instr— whether a string field value has a given substring (same as$hasSubstring) -
$like— whether a field value matches a given SQLLIKEpattern -
$lt— whether a field value is less than a given scalar value -
$lte— whether a field value is less than or equal to a given scalar value -
$ne— whether a field valueis different from a given scalar value -
$nin— whether a field value is not a member of a given set of scalar values -
$regex— whether a string field value matches a given regular expression -
$startsWith— whether a string field value starts with a given substring
You can combine multiple comparison operators in the object that is the value of a single filter field. The operators are implicitly ANDed. For example, the following filter uses comparison operators $gt and $lt. It matches Example 2-2, because that document contains an age field with a value (50), which is both greater than 45 and less than 55.
{ "age" : { "$gt" : 45, "$lt" : 55 } }
Both the operand of a SODA operator and the data matched in your documents by a filter are JSON data. But a comparison operator can in some cases interpret such JSON values specially before comparing them. The use of item-method operators can specify that a comparison should first interpret JSON string data as, for example, uppercase or as a date or a time stamp (date with time). This is explained in the sections about item-method operators.
Related Topics