コンソールへ移動

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.

Read our documentation on Sorting and filtering data for more information.

Index

Properties

ref

Returns a Reference to the Query's location.

Methods

endAt

  • endAt(value: number | string | boolean | null, key?: string): 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 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.

    example
    // Find all dinosaurs whose names come before Pterodactyl lexicographically.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByKey().endAt("pterodactyl").on("child_added", function(snapshot) {
      console.log(snapshot.key);
    });

    Parameters

    • value: number | string | 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.

    • Optional key: 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 Query

equalTo

  • equalTo(value: number | string | boolean | null, key?: string): Query
  • Creates a Query that includes children that match the specified value.

    Using startAt(), endAt(), and equalTo() allows us to choose arbitrary starting and ending points for our 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.

    example
    // Find all dinosaurs whose height is exactly 25 meters.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
      console.log(snapshot.key);
    });

    Parameters

    • value: number | string | 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.

    • Optional key: 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 Query

isEqual

  • isEqual(other: Query | null): boolean
  • Returns whether or not the current and provided queries represent the same location, have the same query parameters, and are from the same instance of firebase.app.App.

    Two Reference objects are equivalent if they represent the same location and are from the same instance of firebase.app.App.

    Two Query objects are equivalent if they represent the same location, have the same query parameters, and are from the same instance of firebase.app.App. Equivalent queries share the same sort order, limits, and starting and ending points.

    example
    var rootRef = firebase.database.ref();
    var usersRef = rootRef.child("users");
    
    usersRef.isEqual(rootRef);  // false
    usersRef.isEqual(rootRef.child("users"));  // true
    usersRef.parent.isEqual(rootRef);  // true
    example
    var rootRef = firebase.database.ref();
    var usersRef = rootRef.child("users");
    var usersQuery = usersRef.limitToLast(10);
    
    usersQuery.isEqual(usersRef);  // false
    usersQuery.isEqual(usersRef.limitToLast(10));  // true
    usersQuery.isEqual(rootRef.limitToLast(10));  // false
    usersQuery.isEqual(usersRef.orderByKey().limitToLast(10));  // false

    Parameters

    • other: Query | null

      The query to compare against.

    Returns boolean

    Whether or not the current and provided queries are equivalent.

limitToFirst

  • limitToFirst(limit: number): Query
  • 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.

    example
    // Find the two shortest dinosaurs.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) {
      // This will be called exactly two times (unless there are less than two
      // dinosaurs in the Database).
    
      // It will also get fired again if one of the first two dinosaurs is
      // removed from the data set, as a new dinosaur will now be the second
      // shortest.
      console.log(snapshot.key);
    });

    Parameters

    • limit: number

      The maximum number of nodes to include in this query.

    Returns Query

limitToLast

  • limitToLast(limit: number): 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.

    example
    // Find the two heaviest dinosaurs.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByChild("weight").limitToLast(2).on("child_added", function(snapshot) {
      // This callback will be triggered exactly two times, unless there are
      // fewer than two dinosaurs stored in the Database. It will also get fired
      // for every new, heavier dinosaur that gets added to the data set.
      console.log(snapshot.key);
    });

    Parameters

    • limit: number

      The maximum number of nodes to include in this query.

    Returns Query

off

  • off(eventType?: EventType, callback?: function, context?: Object | null): void
  • 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.

    example
    var onValueChange = function(dataSnapshot) {  ... };
    ref.on('value', onValueChange);
    ref.child('meta-data').on('child_added', onChildAdded);
    // Sometime later...
    ref.off('value', onValueChange);
    
    // You must also call off() for any child listeners on ref
    // to cancel those callbacks
    ref.child('meta-data').off('child_added', onValueAdded);
    example
    // Or you can save a line of code by using an inline function
    // and on()'s return value.
    var onValueChange = ref.on('value', function(dataSnapshot) { ... });
    // Sometime later...
    ref.off('value', onValueChange);

    Parameters

    • Optional eventType: 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.

    • Optional callback: function

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

    • Optional context: Object | null

      The context that was passed to on().

    Returns void

on

  • on(eventType: EventType, callback: function, cancelCallbackOrContext?: Object | null, context?: Object | null): function
  • 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. See Retrieve Data on the Web for more details.

    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.

    example

    Handle a new value:

    ref.on('value', function(dataSnapshot) {
      ...
    });
    example

    Handle a new child:

    ref.on('child_added', function(childSnapshot, prevChildKey) {
      ...
    });
    example

    Handle child removal:

    ref.on('child_removed', function(oldChildSnapshot) {
      ...
    });
    example

    Handle child data changes:

    ref.on('chil