Logical Query Operators

Fuse.js supports logical query operators. These operators are used for filtering the data and getting precise results based on the given conditions. The following table contains the logical query operators:

NameDescription
$andReturns all documents that match the conditions of all clauses.
$orReturns all documents that match the conditions of any clause.

$and

{ $and: [ { <expression_1> }, { <expression_2> } , ... , { <expression_N> } ] }

The $and operator performs a logical AND operation on an array of expressions and selects the entries that satisfy all the expressions. The $and operator uses short-circuit evaluation (i.e, if the first expression evaluates to false, Fuse.js will not evaluate the remaining expressions).

TIP

Fuse.js 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.

Example

const result = fuse.search({
  $and: [{ author: 'abc' }, { title: 'xyz' }]
})

$or

The $or operator performs a logical OR operation on an array expressions and selects the entries that satisfy at least one of the expressions. The $or operator uses short-circuit evaluation (i.e, if the first expression evaluates to true, Fuse.js will not evaluate the remaining expressions).

Example

const result = fuse.search({
  $or: [{ author: 'abc' }, { author: 'def' }]
})

Logical search with dotted keys

To handle keys that contain dots, you can use the $path and $val properties when building the query.

Example

Use with Extended Searching

Logical query operations pair quite nicely with extended searching.

const result = fuse.search({
  $and: [
    { title: 'old war' }, // Fuzzy "old war"
    { color: "'blue" }, // Exact match for blue
    {
      $or: [
        { title: '^lock' }, // Starts with "lock"
        { title: '!arts' } // Does not have "arts"
      ]
    }
  ]
})
Last Updated: