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:

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