Skip to content

$all, $not, and $and

Compare
Choose a tag to compare
@matthewp matthewp released this 14 Aug 12:43
· 18 commits to master since this release

This adds support for 3 new comparison operators taken from Mongo's operators.

$all

The $all comparison matches collections which contain all of the provided values. For example given the dataset:

[
  {
    "id": "Canada",
    "colors": [ "red", "white" ]
  },
  {
    "id": "Mexico",
    "colors": [ "red", "white", "green" ]
  },
  {
    "id": "USA",
    "colors": [ "red", "white", "blue" ]
  }
]

The query { colors: { $all: ["red", "white"] } } would match all 3 entries. The query { colors: { $all: ["blue"] } } would match only USA.

$not

The $not comparison can be used to negate any other comparison. For example given the dataset:

[
  {
    "name": "Joe",
    "age": 45
  },
  {
    "name": "Zoey",
    "age": 22
  }
]

The query { age: { $not: { $lt: 40 } } } matches Joe. It is exactly equivalent to { age: { $gte: 40 } }.

$and

The $and comparison combines multiple other comparisons into a single grouping. It can be used in combination with the two above comparisons like so. Given the dataset:

[
  {
    "id": "Canada",
    "colors": [ "red", "white" ]
  },
  {
    "id": "Mexico",
    "colors": [ "red", "white", "green" ]
  },
  {
    "id": "USA",
    "colors": [ "red", "white", "blue" ]
  }
]

The query { $and: [ { colors: { $all: ["red, "white"] } }, { colors: { $not: { $all: ["blue"] } } } ] } matches Canada and Mexico.