React Native Firebase
    Preparing search index...

    A Query sorts and filters the data at a Database location so only a subset of the child data is included. This can be used to order a collection of data by some attribute (for example, height of dinosaurs) as well as to restrict a large list of items (for example, chat messages) down to a number suitable for synchronizing to the client. Queries are created by chaining together one or more of the filter methods defined here.

    Just as with a Reference, you can receive data from a Query by using the on() method. You will only receive events and DataSnapshots for the subset of the data that matches your query.

    interface Query {
        ref: FirebaseDatabaseTypes.Reference;
        endAt(
            value: string | number | boolean | null,
            key?: string,
        ): FirebaseDatabaseTypes.Query;
        equalTo(
            value: string | number | boolean | null,
            key?: string,
        ): FirebaseDatabaseTypes.Query;
        isEqual(other: FirebaseDatabaseTypes.Query): boolean;
        keepSynced(bool: boolean): Promise<void>;
        limitToFirst(limit: number): FirebaseDatabaseTypes.Query;
        limitToLast(limit: number): FirebaseDatabaseTypes.Query;
        off(
            eventType?: EventType,
            callback?: (
                a: FirebaseDatabaseTypes.DataSnapshot,
                b?: string | null,
            ) => void,
            context?: Record<string, any>,
        ): void;
        on(
            eventType: EventType,
            callback: (
                data: FirebaseDatabaseTypes.DataSnapshot,
                previousChildKey?: string | null,
            ) => void,
            cancelCallbackOrContext?:
                | Record<string, any>
                | ((a: Error) => void)
                | null,
            context?: Record<string, any> | null,
        ): (
            a: FirebaseDatabaseTypes.DataSnapshot | null,
            b?: string | null,
        ) => void;
        once(
            eventType: EventType,
            successCallback?: (
                a: FirebaseDatabaseTypes.DataSnapshot,
                b?: string | null,
            ) => any,
            failureCallbackContext?: Record<string, any> | ((a: Error) => void) | null,
        ): Promise<FirebaseDatabaseTypes.DataSnapshot>;
        orderByChild(path: string): FirebaseDatabaseTypes.Query;
        orderByKey(): FirebaseDatabaseTypes.Query;
        orderByPriority(): FirebaseDatabaseTypes.Query;
        orderByValue(): FirebaseDatabaseTypes.Query;
        startAt(
            value: string | number | boolean | null,
            key?: string,
        ): FirebaseDatabaseTypes.Query;
        toJSON(): object;
        toString(): string;
    }

    Hierarchy (View Summary)

    Index

    Methods

    • Creates a Query with the specified ending point.

      Using startAt(), endAt(), and equalTo() allows you to choose arbitrary starting and ending points for your queries.

      The ending point is inclusive, so children with exactly the specified value will be included in the query. The optional key argument can be used to further limit the range of the query. If it is specified, then children that have exactly the specified value must also have a key name less than or equal to the specified key.

      You can read more about endAt() in Filtering data.

      const ref = firebase.database().ref('users');
      const snapshot = await ref.orderByKey().endAt('Ada Lovelace').once('value');

      Parameters

      • value: string | number | boolean | null

        The value to end at. The argument type depends on which orderBy*() function was used in this query. Specify a value that matches the orderBy*() type. When used in combination with orderByKey(), the value must be a string.

      • Optionalkey: string

        The child key to end at, among the children with the previously specified priority. This argument is only allowed if ordering by child, value, or priority.

      Returns FirebaseDatabaseTypes.Query

    • Creates a Query with the specified ending point.

      Using startAt(), endAt(), and equalTo() allows you to choose arbitrary starting and ending points for your queries.

      The optional key argument can be used to further limit the range of the query. If it is specified, then children that have exactly the specified value must also have exactly the specified key as their key name. This can be used to filter result sets with many matches for the same value.

      You can read more about equalTo() in Filtering data.

      const ref = firebase.database().ref('users');
      const snapshot = await ref.orderByChild('age').equalTo(30).once('value');

      Parameters

      • value: string | number | boolean | null

        The value to match for. The argument type depends on which orderBy*() function was used in this query. Specify a value that matches the orderBy*() type. When used in combination with orderByKey(), the value must be a string.

      • Optionalkey: string

        The child key to start at, among the children with the previously specified priority. This argument is only allowed if ordering by child, value, or priority.

      Returns FirebaseDatabaseTypes.Query

    • Returns whether or not the current and provided queries represent the same location, have the same query parameters.

      Two Reference objects are equivalent if they represent the same location and are from the same instance of @firebase/app!FirebaseApp. Equivalent queries share the same sort order, limits, and starting and ending points.

      const ref1 = firebase.database().ref('users').orderByKey().endAt('Ada Lovelace');
      const ref2 = firebase.database().ref('users').orderByKey();

      console.log(ref1.isEqual(ref2)); // false
      const ref1 = firebase.database().ref('users').orderByKey().endAt('Ada Lovelace');
      const ref2 = firebase.database().ref('users').endAt('Ada Lovelace').orderByKey();

      console.log(ref1.isEqual(ref2)); // true

      Parameters

      Returns boolean

    • By calling keepSynced(true) on a location, the data for that location will automatically be downloaded and kept in sync, even when no listeners are attached for that location.

      const ref = firebase.database().ref('users');
      await ref.keepSynced(true);

      Parameters

      • bool: boolean

        Pass true to keep this location synchronized, pass false to stop synchronization.

      Returns Promise<void>

    • Generates a new Query limited to the first specific number of children.

      The limitToFirst() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have fewer than 100 messages stored in our Database, a child_added event will fire for each message. However, if we have over 100 messages, we will only receive a child_added event for the first 100 ordered messages. As items change, we will receive child_removed events for each item that drops out of the active list so that the total number stays at 100.

      You can read more about limitToFirst() in Filtering data.

      const snapshot = firebase.database().ref('users').orderByKey().limitToFirst(2).once('value');
      console.log(snapshot.numChildren()); // 2

      Parameters

      • limit: number

        The maximum number of nodes to include in this query.

      Returns FirebaseDatabaseTypes.Query

    • Generates a new Query object limited to the last specific number of children.

      The limitToLast() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have fewer than 100 messages stored in our Database, a child_added event will fire for each message. However, if we have over 100 messages, we will only receive a child_added event for the last 100 ordered messages. As items change, we will receive child_removed events for each item that drops out of the active list so that the total number stays at 100.

      You can read more about limitToLast() in Filtering data.

      const snapshot = firebase.database().ref('users').orderByKey().limitToLast(2).once('value');
      console.log(snapshot.numChildren()); // 2

      Parameters

      • limit: number

        The maximum number of nodes to include in this query.

      Returns FirebaseDatabaseTypes.Query

    • Detaches a callback previously attached with on().

      Detach a callback previously attached with on(). Note that if on() was called multiple times with the same eventType and callback, the callback will be called multiple times for each event, and off() must be called multiple times to remove the callback. Calling off() on a parent listener will not automatically remove listeners registered on child nodes, off() must also be called on any child listeners to remove the callback.

      If a callback is not specified, all callbacks for the specified eventType will be removed. Similarly, if no eventType is specified, all callbacks for the Reference will be removed.

      const ref = firebase.database().ref('settings');
      const onValueChange = function(snapshot) { ... };
      const onChildAdded = function(snapshot) { ... };

      ref.on('value', onValueChange);
      ref.child('meta-data').on('child_added', onChildAdded);
      // Sometime later...
      ref.off('value', onValueChange);
      ref.child('meta-data').off('child_added', onChildAdded);

      Parameters

      • OptionaleventType: EventType

        One of the following strings: "value", "child_added", "child_changed", "child_removed", or "child_moved." If omitted, all callbacks for the Reference will be removed.

      • Optionalcallback: (a: FirebaseDatabaseTypes.DataSnapshot, b?: string | null) => void

        The callback function that was passed to on() or undefined to remove all callbacks.

      • Optionalcontext: Record<string, any>

        The context that was passed to on().

      Returns void

    • Listens for data changes at a particular location.

      This is the primary way to read data from a Database. Your callback will be triggered for the initial data and again whenever the data changes. Use off() to stop receiving updates..

      value event

      This event will trigger once with the initial data stored at this location, and then trigger again each time the data changes. The DataSnapshot passed to the callback will be for the location at which on() was called. It won't trigger until the entire contents has been synchronized. If the location has no data, it will be triggered with an empty DataSnapshot (val() will return null).

      child_added event

      This event will be triggered once for each initial child at this location, and it will be triggered again every time a new child is added. The DataSnapshot passed into the callback will reflect the data for the relevant child. For ordering purposes, it is passed a second argument which is a string containing the key of the previous sibling child by sort order, or null if it is the first child.

      child_removed event

      This event will be triggered once every time a child is removed. The DataSnapshot passed into the callback will be the old data for the child that was removed. A child will get removed when either:

      • a client explicitly calls remove() on that child or one of its ancestors
      • a client calls set(null) on that child or one of its ancestors
      • that child has all of its children removed
      • there is a query in effect which now filters out the child (because it's sort order changed or the max limit was hit)

      child_changed event

      This event will be triggered when the data stored in a child (or any of its descendants) changes. Note that a single child_changed event may represent multiple changes to the child. The DataSnapshot passed to the callback will contain the new child contents. For ordering purposes, the callback is also passed a second argument which is a string containing the key of the previous sibling child by sort order, or null if it is the first child.

      child_moved event

      This event will be triggered when a child's sort order changes such that its position relative to its siblings changes. The DataSnapshot passed to the callback will be for the data of the child that has moved. It is also passed a second argument which is a string containing the key of the previous sibling child by sort order, or null if it is the first child.

      Parameters

      • eventType: EventType

        One of the following strings: "value", "child_added", "child_changed", "child_removed", or "child_moved."

      • callback: (
            data: FirebaseDatabaseTypes.DataSnapshot,
            previousChildKey?: string | null,
        ) => void

        A callback that fires when the specified event occurs. The callback will be passed a DataSnapshot. For ordering purposes, "child_added", "child_changed", and "child_moved" will also be passed a string containing the key of the previous child, by sort order, or null if it is the first child.

      • OptionalcancelCallbackOrContext: Record<string, any> | ((a: Error) => void) | null

        An optional callback that will be notified if your event subscription is ever canceled because your client does not have permission to read this data (or it had permission but has now lost it). This callback will be passed an Error object indicating why the failure occurred.

      • Optionalcontext: Record<string, any> | null

        If provided, this object will be used as this when calling your callback(s).

      Returns (a: FirebaseDatabaseTypes.DataSnapshot | null, b?: string | null) => void

    • Listens for exactly one event of the specified event type, and then stops listening.

      This is equivalent to calling on(), and then calling off() inside the callback function. See on() for details on the event types.

      // Promise
      const snapshot = await firebase.database().ref('users').once('value');
      // Callback
      firebase.database().ref('users).once('value', (snapshot) => {
      console.log(snapshot.val());
      });

      Parameters

      • eventType: EventType

        One of the following strings: "value", "child_added", "child_changed", "child_removed", or "child_moved."

      • OptionalsuccessCallback: (a: FirebaseDatabaseTypes.DataSnapshot, b?: string | null) => any

        A callback that fires when the specified event occurs. The callback will be passed a DataSnapshot. For ordering purposes, "child_added", "child_changed", and "child_moved" will also be passed a string containing the key of the previous child by sort order, or null if it is the first child.

      • OptionalfailureCallbackContext: Record<string, any> | ((a: Error) => void) | null

        An optional callback that will be notified if your client does not have permission to read the data. This callback will be passed an Error object indicating why the failure occurred.

      Returns Promise<FirebaseDatabaseTypes.DataSnapshot>

    • Generates a new Query object ordered by the specified child key.

      Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query is an error.

      Firebase queries allow you to order your data by any child key on the fly. However, if you know in advance what your indexes will be, you can define them via the .indexOn rule in your Security Rules for better performance.

      You can read more about orderByChild() in Sort data.

      const snapshot = await firebase.database().ref('users').orderByChild('age').once('value');
      snapshot.forEach((snapshot) => {
      console.log('Users age:', snapshot.val().age);
      });

      Parameters

      • path: string

        The child path node to order by.

      Returns FirebaseDatabaseTypes.Query

    • Generates a new Query object ordered by key.

      Sorts the results of a query by their (ascending) key values.

      You can read more about orderByKey() in Sort data.

      const snapshot = await firebase.database().ref('users').orderByKey().once('value');
      snapshot.forEach((snapshot) => {
      console.log('User:', snapshot.val());
      });

      Returns FirebaseDatabaseTypes.Query

    • Generates a new Query object ordered by value.

      If the children of a query are all scalar values (string, number, or boolean), you can order the results by their (ascending) values.

      You can read more about orderByValue() in Sort data.

      await firebase.database().ref('scores').orderByValue().once('value');
      

      Returns FirebaseDatabaseTypes.Query

    • Creates a Query with the specified starting point.

      Using startAt(), endAt(), and equalTo() allows you to choose arbitrary starting and ending points for your queries.

      The starting point is inclusive, so children with exactly the specified value will be included in the query. The optional key argument can be used to further limit the range of the query. If it is specified, then children that have exactly the specified value must also have a key name greater than or equal to the specified key.

      You can read more about startAt() in Filtering data.

      await firebase.database().ref('users').orderByChild('age').startAt(21).once('value');
      

      Parameters

      • value: string | number | boolean | null

        The value to start at. The argument type depends on which orderBy*() function was used in this query. Specify a value that matches the orderBy*() type. When used in combination with orderByKey(), the value must be a string.

      • Optionalkey: string

        The child key to start at. This argument is only allowed if ordering by child, value, or priority.

      Returns FirebaseDatabaseTypes.Query

    • Returns a JSON-serializable representation of this object.

      Returns object

    • Gets the absolute URL for this location.

      The toString() method returns a URL that is ready to be put into a browser, curl command, or a firebase.database().refFromURL() call. Since all of those expect the URL to be url-encoded, toString() returns an encoded URL.

      Append '.json' to the returned URL when typed into a browser to download JSON-formatted data. If the location is secured (that is, not publicly readable), you will get a permission-denied error.

      const ref1 = firebase.database().ref();
      const ref2 = firebase.database().ref('users').orderByValue();

      ref1.toString(); // https://sample-app.firebaseio.com/
      ref2.toString(); // https://sample-app.firebaseio.com/users

      Returns string

    Properties

    Returns a Reference to the Query's location.