Query API

Field equality, comparison operators (at field level) and logical operators (at top level).

The Query API is very similar to MongoDB, You can select documents based on field equality or use comparison operators ($lt, $lte, $gt, $gte, $in, $nin, $ne, $eq). You can also use logical operators $or, $and, $not and $where.

Field equality

To specify equality conditions, use { <FieldName> : <Value> } expressions in the query filter document. This is the most basic and straight forward query.

An example of this query would go like this:

// select all documents where the name is "ozzy"
db.find({ filter: {name: "ozzy"} });

// select all documents where
// the age is exactly 27
// and height is exactly 180
db.find({
    filter: {
        age: 27,
        height: 180
    }
});

Field level operators

Syntax: { <fieldName>: { <operator>:<specification> } }

Comparison operators

Equal $eq

Applies to: any field type.

Syntax: { <fieldName> : { $eq: <value> } }.

Explanation: Specifies equality condition. The $eq operator matches documents where the value of a field equals the specified value. It is equivalent to { <FieldName> : <Value> }.

Not equal $ne

Applies to: any field type.

Syntax: { <fieldName> : { $ne: <value> } }.

Explanation: $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.

Greater than $gt

Applies to: number & Date fields.

Syntax: { <fieldName> : { $gt: <value> } }.

Explanation: $gt selects those documents where the value of the field is greater than (i.e. >) the specified value.

Less than $lt

Applies to: number & Date fields.

Syntax: { <fieldName> : { $lt: <value> } }.

Explanation: $lt selects those documents where the value of the field is less than (i.e. <) the specified value.

Greater than or equal $gte

Applies to: number & Date fields.

Syntax: { <fieldName> : { $gte: <value> } }.

Explanation: $gte selects those documents where the value of the field is greater than or equal to (i.e. >=) the specified value.

Less than or equal $lte

Applies to: number & Date fields.

Syntax: { <fieldName> : { $lte: <value> } }.

Explanation: $lte selects those documents where the value of the field is less than or equal to (i.e. <=) the specified value.

In $in

Applies to: any field type.

Syntax: { <fieldName> : { $in: [<value1>, <value2>, ... etc] } }.

Explanation: The $in operator selects the documents where the value of a field equals any value in the specified array.

Not in $nin

Applies to: any field type.

Syntax: { <fieldName> : { $nin: [<value1>, <value2>, ... etc] } }.

Explanation: $nin selects the documents where: the field value is not in the specified array or the field does not exist.

Element operators

Exists $exists

Applies to: any field type.

Syntax: { <fieldName> : { $exists: <boolean> } }.

Explanation: When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null. If <boolean> is false, the query returns only the documents that do not contain the field.

Type $type

Applies to: any field type.

Syntax: { <fieldName> : { $type: <specification> } }.

Explanation: $type selects documents where the value of the field is an instance of the specified type. Type specification can be one of the following:

  • "string"

  • "number"

  • "boolean"

  • "undefined"

  • "array"

  • "null"

  • "date"

  • "object"

Although rare, but this is useful for when a field can have different type.

Evaluation operators

Modulo $mod

Applies to: number & Date fields.

Syntax: { <fieldName> : { $mod: [divisor, remainder] } }.

Explanation: Select documents where the value of a field divided by a divisor has the specified remainder (i.e. perform a modulo operation to select documents).

Regular expression testing $regex

Applies to: string fields only.

Syntax: { <fieldName> : { $regex: <RegExp> } }.

Explanation: Selects documents which tests true for a given regular expression.

Array operators

All contained $all

Applies to: array fields only.

Syntax: { <fieldName> : { $all: [<value1>, <value2>,...etc] } }.

Explanation: The $all operator selects the documents where the value of a field is an array that contains all the specified elements.

Element match $elemMatch

Applies to: array fields only.

Syntax: {<fieldName>:{$elemMatch:{<query1>,<query2>,...etc}}}.

Explanation: The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

Size $size

Applies to: array fields only.

Syntax: { <fieldName> : { $size: number } }.

Explanation: The $size operator matches any array with the number of elements (length of the array) specified by the argument.

Field level operators on the array element

The array fields has the operators $all, $elemMatch and $size specific for them, nonetheless all the other aforementioned operators can be applied on the array, and would return true if any element in the array matches them.

  • $eq: matches an array that has an element equal to the value specified by the operator.

  • $ne: matches an array that has an element other than the value specified by the operator.

  • $gt: matches an array of numbers that has a number greater than the value specified by the operator.

  • $lt: matches an array of numbers that has a number less than the value specified by the operator.

  • $gte: matches an array of numbers that has a number greater than or equals to the value specified by the operator.

  • $lte: matches an array of numbers that has a number less than or equal to the value specified by the operator.

  • $in: matches an array that has any of the values specified by the operator.

  • $nin: matches an array that has none of the values specified by the operator.

  • $exists: will match any given array.

  • $type: will match the array if the specification for the operator is "array".

  • $mod: matches an array if it has a number that when divided by the divider would given the remainder specified by the operator.

  • $regex: matches an array of strings that has a string that would return true when tested by the regex given by this operator.

Negation

All the above operators can be negated using the $not operator.

// find all documents
// where they have "tags" that is not of length 10
db.find({ filter: { tags: { $not: { $size: 10 } } } });

// similar to $ne
db.find({ filter: { name: { $not: { $eq: "ozzy" } } } });

// find documents where the "name" field
// is a not a string
db.find({ filter: { name: { $not: { $type: "string" } } } });

// select documents where the "tags"
// field is an array that doesn't have "music" & "art"
db.find({ filter: { tags: { $not: { $all: [ "music", "art" ] } } } });

// select documents where the "years" field
// is an even number
db.find({ filter: {
    years: {
        $not: {
            $mod: [2,1]
        }
    }
}});

// ...etc 

Top level operators

$and

Syntax:

{
    $and: [
        <query1>,
        <query2>,
        <query3>,
        ...etc
    ]
}

Explanation: $and performs a logical AND operation on an array of two or more expressions (e.g. <field level query 1>, <field level query 2> , etc.) and selects the documents that satisfy all the expressions in the array. The $and operator uses short-circuit evaluation. If the first expression (e.g. <field level query 1>) evaluates to false, TyDB will not evaluate the remaining expressions.

$nor

Syntax:

{
    $nor: [
        <query1>,
        <query2>,
        <query3>,
        ...etc
    ]
}

Explanation: $nor performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array.

$or

Syntax:

{
    $or: [
        <query1>,
        <query2>,
        <query3>,
        ...etc
    ]
}

Explanation: The $or operator performs a logical OR operation on an array of two or more expressions and selects the documents that satisfy at least one of the expressions.

$where

Syntax:

{
    $where: (this: Model) => boolean
}

Explanation: Matches the documents that when evaluated by the given function, would return true. **The $where provides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. Reference the document in the JavaScript expression or function using this.

$deep

Syntax:

{
    $deep: {
        "an.embedded.document.field": <query>
    }
}

Explanation: This is the only operator that TyDB offers while MongoDB doesn't. Use this operator when trying to apply a query on a deeply nested properties (i.e. embedded documents), like: "employee.address.street".

This operator is not strongly typed, as the typescript compiler is unable to decide what would be the type of a dot noted deeply nested field. In fact, this limitation is the reason for placing dot notation access to field in a separate operator, since placing them at the top level would disable all type checks performed by typescript.

Due to this limitation, although it is perfectly supported, It is strongly advised to avoid embedded documents when possible.

Last updated