Skip to content

Query Constraints

GogoVega edited this page Oct 23, 2023 · 3 revisions

Query Constraints is used to sort and/or order your data.

Order your Data

Before ordering your data, you must first create indexing in your rules! (see example)

Below is the list and description of the methods to order your data:

Order by Child

Data that contains the specified child key is ordered as follows:

  1. Children with a null value for the specified child key come first.
  2. Children with a value of false for the specified child key come next. If multiple children have a value of false, they are sorted lexicographically by key.
  3. Children with a value of true for the specified child key come next. If multiple children have a value of true, they are sorted lexicographically by key.
  4. Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified child node, they are sorted by key.
  5. Strings come after numbers, and are sorted lexicographically in ascending order. If multiple children have the same value for the specified child node, they are ordered lexicographically by key.
  6. Objects come last, and sorted lexicographically by key in ascending order.

Order by Key

Data is returned in ascending order by key as follows. Keep in mind that keys can only be strings.

  1. Children with a key that can be parsed as a 32-bit integer come first, sorted in ascending order.
  2. Children with a string value as their key come next, sorted lexicographically in ascending order.

Order by Priority

Data is returned in ascending order by priority. The priority is set with the methods Set Priority and Set with Priority in Firebase OUT node.

Order by Value

Children are ordered by their value. The ordering criteria is the same as in Order by Child, except the value of the node is used instead of the value of a specified child key.

Sort your Data

Below is the list and description of the methods to sort your data:

Limit Queries

The limit queries below are used to set a maximum number of children to be synced for a given callback:

  • Limit to First
  • Limit to Last

Range Queries

Using range queries below allows you to choose arbitrary starting and ending points for your queries:

  • Start At
  • Start After
  • End At
  • End After
  • Equal To

The Equal To method allows you to filter based on exact matches. As is the case with the other range queries, it will fire for each matching child node.

Example

Below is an example that uses the Order by Child and Equal To constraints:

Your database contains:

{
  "users": {
    "alanisawesome": {
      "full_name": "Alan Turing",
      "nickname": "Alan The Machine",
      "hobby": "Computer"
    },
    "steveisapple": {
      "full_name": "Steve Jobs",
      "nickname": "Steve The King",
      "hobby": "Computer"
    }
}

To order your data according to the hobby child key you need to insert the following rule:

{
  "rules": {
    "users": {
      // Can also be an array
      ".indexOn": "hobby"
    }
  }
}

Note: The value of .indexOn can be an array of multiple child keys.

The Query Constraints below allow you to retrieve only data with "Computer" as a hobby:

{
  orderByChild: 'hobby',
  equalTo: {
    value: 'Computer',
    // If the key is specified, then children that have exactly the specified value must also have exactly the specified key as their key name.
    // key: 'alanisawesome'
  }
}