results matching ""

    No results matching ""

     2.2.Logical Query Operators

    ¶ 2.2.1. Logical Operators List

    NameDescription
    $andJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.
    $notInverts the effect of a query expression and returns documents that do not match the query expression.
    $norJoins query clauses with a logical NOR returns all documents that fail to match both clauses.
    $orJoins query clauses with a logical OR returns all documents that match the conditions of either clause.

    ¶ 2.2.2. $and

    Syntax: { $and: [ {}, {} , ... , {} ] }

    $and performs a logical AND operation on an array of two or more expressions (e.g.,, 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.) evaluates to false, X-Server will not evaluate the remaining expressions.

    NOTE

    X-Server provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.

    § 2.2.2.1. AND Queries With Multiple Expressions Specifying the Same Field

    Consider the following example:

    tim.X("inventory").Get( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
    

    This query will select all documents in the inventory collection where:

    the price field value is not equal to 1.99 and the price field exists.

    This query can be also be constructed with an implicit AND operation by combining the operator expressions for the price field. For example, this query can be written as:

    tim.X("inventory").Get( { price: { $ne: 1.99, $exists: true } } )
    

    § 2.2.2.2. AND Queries With Multiple Expressions Specifying the Same Operator

    Consider the following example:

    tim.X("inventory").Get( {
        $and : [
            { $or : [ { price : 0.99 }, { price : 1.99 } ] },
            { $or : [ { sale : true }, { qty : { $lt : 20 } } ] }
        ]
    } )
    

    This query will select all documents where:

    the price field value equals 0.99 or 1.99, and the sale field value is equal to true or the qty field value is less than 20. This query cannot be constructed using an implicit AND operation, because it uses the $or operator more than once.

    ¶ 2.2.3. $not

    Syntax: { field: { $not: {} } }

    $not performs a logical NOT operation on the specifiedand selects the documents that do not match the. This includes documents that do not contain the field.

    Consider the following query:

    tim.X("inventory").Get( { price: { $not: { $gt: 1.99 } } } )
    

    This query will select all documents in the inventory collection where:

    the price field value is less than or equal to 1.99 or the price field does not exist

    { $not: { $gt: 1.99 } } is different from the $lte operator. { $lte: 1.99 } returns only the documents where price field exists and its value is less than or equal to 1.99.

    Remember that the $not operator only affects other operators and cannot check fields and documents independently. So, use the $not operator for logical disjunctions and the $ne operator to test the contents of fields directly.

    Consider the following behaviors when using the $not operator:

    The operation of the $not operator is consistent with the behavior of other operators but may yield unexpected results with some data types like arrays.

    The $not operator does not support operations with the $regex operator. Instead use // or in your driver interfaces, use your language’s regular expression capability to create regular expression objects.

    Consider the following example which uses the pattern match expression //:

    tim.X("inventory").Get( { item: { $not: /^p.*/ } } )
    

    The query will select all documents in the inventory collection where the item field value does not start with the letter p.

    ¶ 2.2.4. $nor

    $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. The $nor has the following Syntax:

    { $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }
    

    § 2.2.4.1. $nor Query with Two Expressions

    Consider the following query which uses only the $nor operator:

    tim.X("inventory").Get( { $nor: [ { price: 1.99 }, { sale: true } ]  } )
    

    This query will return all documents that:

    contain the price field whose value is not equal to 1.99 and contain the sale field whose value is not equal to true or contain the price field whose value is not equal to 1.99 but do not contain the sale field or do not contain the price field but contain the sale field whose value is not equal to true or do not contain the price field and do not contain the sale field.

    § 2.2.4.2. $nor and Additional Comparisons

    Consider the following query:

    tim.X("inventory").Get( { $nor: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )
    

    This query will select all documents in the inventory collection where:

    the price field value does not equal 1.99 and the qty field value is not less than 20 and the sale field value is not equal to true including those documents that do not contain these field(s).

    The exception in returning documents that do not contain the field in the $nor expression is when the $nor operator is used with the $exists operator.

    § 2.2.4.3. $nor and $exists

    Compare that with the following query which uses the $nor operator with the $exists operator:

    tim.X("inventory").Get( { $nor: [ { price: 1.99 }, { price: { $exists: false } },
                                 { sale: true }, { sale: { $exists: false } } ] } )
    

    This query will return all documents that:

    contain the price field whose value is not equal to 1.99 and contain the sale field whose value is not equal to true

    ¶ 2.2.5. $or

    The $or operator performs a logical OR operation on an array of two or moreand selects the documents that satisfy at least one of the. The $or has the following Syntax:

    { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
    

    Consider the following example:

    tim.X("inventory").Get( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
    

    This query will select all documents in the inventory collection where either the quantity field value is less than 20 or the price field value equals 10.

    § 2.2.5.1. $or Clauses and Indexes

    When evaluating the clauses in the $or expression, X-Server either performs a collection scan or, if all the clauses are supported by indexes, X-Server performs index scans. That is, for X-Server to use indexes to evaluate an $or expression, all the clauses in the $or expression must be supported by indexes. Otherwise, X-Server will perform a collection scan.

    When using indexes with $or queries, each clause of an $or can use its own index. Consider the following query:

    tim.X("inventory").Get( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
    

    § 2.2.5.2. $or and text Queries

    If $or includes a $text query, all clauses in the $or array must be supported by an index. This is because a $text query must use an index, and $or can only use indexes if all its clauses are supported by indexes. If the $text query cannot use an index, the query will return an error.

    § 2.2.5.3. $or and GeoSpatial Queries

    $or supports geospatial clauses with the following exception for the near clause (near clause includes $nearSphere and $near). $or cannot contain a near clause with any other clause.

    § 2.2.5.4. $or and Sort Operations

    When executing $or queries with a sort(), X-Server can now use indexes that support the $or clauses. Previous versions did not use the indexes.

    § 2.2.5.5. $or versus $in

    When using $or withthat are equality checks for the value of the same field, use the $in operator instead of the $or operator.

    For example, to select all documents in the inventory collection where the quantity field value equals either 20 or 50, use the $in operator:

    tim.X("inventory").Get ( { quantity: { $in: [20, 50] } } )
    

    § 2.2.5.6. Nested $or Clauses

    You may nest $or operations.

    Read Like

    Question & Feedback