Query

public classQuery extends Object
Known Direct Subclasses
DatabaseReferenceA Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. 

The Query class (and its subclass,DatabaseReference) are used for reading data. Listeners are attached, and they will be triggered when the corresponding data changes.

Instances of Query are obtained by calling startAt(), endAt(), or limit() on a DatabaseReference.

Public Method Summary

ChildEventListener
addChildEventListener(ChildEventListener listener)
Add a listener for child events occurring at this location.
void
addListenerForSingleValueEvent(ValueEventListener listener)
Add a listener for a single change in the data at this location.
ValueEventListener
addValueEventListener(ValueEventListener listener)
Add a listener for changes in the data at this location.
Query
endAt(String value, String key)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key key less than or equal to the given key.
Query
endAt(String value)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.
Query
endAt(double value, String key)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.
Query
endAt(double value)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.
Query
endAt(boolean value)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.
Query
endAt(boolean value, String key)
Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.
Query
equalTo(String value, String key)
Create a query constrained to only return the child node with the given key and value.
Query
equalTo(double value)
Create a query constrained to only return child nodes with the given value.
Query
equalTo(double value, String key)
Create a query constrained to only return the child node with the given key and value.
Query
equalTo(String value)
Create a query constrained to only return child nodes with the given value.
Query
equalTo(boolean value, String key)
Create a query constrained to only return the child node with the given key and value.
Query
equalTo(boolean value)
Create a query constrained to only return child nodes with the given value.
DatabaseReference
void
keepSynced(boolean keepSynced)
By callingkeepSynced(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.
Query
limitToFirst(int limit)
Create a query with limit and anchor it to the start of the window.
Query
limitToLast(int limit)
Create a query with limit and anchor it to the end of the window.
Query
orderByChild(String path)
Create a query in which child nodes are ordered by the values of the specified path.
Query
orderByKey()
Create a query in which child nodes are ordered by their keys.
Query
orderByPriority()
Create a query in which child nodes are ordered by their priorities.
Query
orderByValue()
Create a query in which nodes are ordered by their value
void
removeEventListener(ChildEventListener listener)
Remove the specified listener from this location.
void
removeEventListener(ValueEventListener listener)
Remove the specified listener from this location.
Query
startAt(double value)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.
Query
startAt(String value, String key)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.
Query
startAt(String value)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.
Query
startAt(double value, String key)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.
Query
startAt(boolean value)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.
Query
startAt(boolean value, String key)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Inherited Method Summary

From class java.lang.Object
Object
clone()
boolean
equals(Object arg0)
void
finalize()
final Class<?>
getClass()
int
hashCode()
final void
notify()
final void
notifyAll()
String
toString()
final void
wait(long arg0, int arg1)
final void
wait(long arg0)
final void
wait()

Public Methods

publicChildEventListeneraddChildEventListener(ChildEventListener listener)

Add a listener for child events occurring at this location. When child locations are added, removed, changed, or moved, the listener will be triggered for the appropriate event.

Parameters
listenerThe listener to be called with changes
Returns
  • A reference to the listener provided. Save this to remove the listener later.

public voidaddListenerForSingleValueEvent(ValueEventListener listener)

Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.

Parameters
listenerThe listener to be called with the data

publicValueEventListeneraddValueEventListener(ValueEventListener listener)

Add a listener for changes in the data at this location. Each time time the data changes, your listener will be called with an immutable snapshot of the data.

Parameters
listenerThe listener to be called with changes
Returns
  • A reference to the listener provided. Save this to remove the listener later.

publicQueryendAt(String value, String key)

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key key less than or equal to the given key.

Parameters
valueThe value to end at, inclusive
keyThe key to end at, inclusive
Returns
  • A Query with the new constraint

publicQueryendAt(String value)

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
valueThe value to end at, inclusive
Returns
  • A Query with the new constraint

publicQueryendAt(double value, String key)

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

Parameters
valueThe value to end at, inclusive
keyThe key to end at, inclusive
Returns
  • A Query with the new constraint

publicQueryendAt(double value)

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
valueThe value to end at, inclusive
Returns
  • A Query with the new constraint

publicQueryendAt(boolean value)

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
valueThe value to end at, inclusive
Returns
  • A Query with the new constraint

publicQueryendAt(boolean value, String key)

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

Parameters
valueThe value to end at, inclusive
keyThe key to end at, inclusive
Returns
  • A Query with the new constraint

publicQueryequalTo(String value, String key)

Create a query constrained to only return the child node with the given key and value. Note that there is at most one such child as names are unique.

Parameters
valueThe value to query for
keyThe key of the child
Returns
  • A query with the new constraint

publicQueryequalTo(double value)

Create a query constrained to only return child nodes with the given value.

Parameters
valueThe value to query for
Returns
  • A query with the new constraint

publicQueryequalTo(double value, String key)

Create a query constrained to only return the child node with the given key and value. Note that there is at most one such child as keys are unique.

Parameters
valueThe value to query for
keyThe key of the child
Returns
  • A query with the new constraint

publicQueryequalTo(String value)

Create a query constrained to only return child nodes with the given value.

Parameters
valueThe value to query for
Returns
  • A query with the new constraint

publicQueryequalTo(boolean value, String key)

Create a query constrained to only return the child node with the given key and value. Note that there is at most one such child as keys are unique.

Parameters
valueThe value to query for
keyThe name of the child
Returns
  • A query with the new constraint

publicQueryequalTo(boolean value)

Create a query constrained to only return child nodes with the given value.

Parameters
valueThe value to query for
Returns
  • A query with the new constraint

publicDatabaseReferencegetRef()

Returns
  • A DatabaseReference to this location

public voidkeepSynced(boolean keepSynced)

By callingkeepSynced(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. Additionally, while a location is kept synced, it will not be evicted from the persistent disk cache.

Parameters
keepSyncedPasstrue to keep this location synchronized, passfalse to stop synchronization.

publicQuerylimitToFirst(int limit)

Create a query with limit and anchor it to the start of the window.

Parameters
limitThe maximum number of child nodes to return
Returns
  • A Query with the new constraint

publicQuerylimitToLast(int limit)

Create a query with limit and anchor it to the end of the window.

Parameters
limitThe maximum number of child nodes to return
Returns
  • A Query with the new constraint

publicQueryorderByChild(String path)

Create a query in which child nodes are ordered by the values of the specified path.

Parameters
pathThe path to the child node to use for sorting
Returns
  • A Query with the new constraint

publicQueryorderByKey()

Create a query in which child nodes are ordered by their keys.

Returns
  • A Query with the new constraint

publicQueryorderByPriority()

Create a query in which child nodes are ordered by their priorities.

Returns
  • A Query with the new constraint

publicQueryorderByValue()

Create a query in which nodes are ordered by their value

Returns
  • A Query with the new constraint

public voidremoveEventListener(ChildEventListener listener)

Remove the specified listener from this location.

Parameters
listenerThe listener to remove

public voidremoveEventListener(ValueEventListener listener)

Remove the specified listener from this location.

Parameters
listenerThe listener to remove

publicQuerystartAt(double value)

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
valueThe value to start at, inclusive
Returns
  • A Query with the new constraint

publicQuerystartAt(String value, String key)

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Parameters
valueThe priority to start at, inclusive
keyThe key to start at, inclusive
Returns
  • A Query with the new constraint

publicQuerystartAt(String value)

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
valueThe value to start at, inclusive
Returns
  • A Query with the new constraint

publicQuerystartAt(double value, String key)

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Parameters
valueThe priority to start at, inclusive
keyThe key name to start at, inclusive
Returns
  • A Query with the new constraint

publicQuerystartAt(boolean value)

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
valueThe value to start at, inclusive
Returns
  • A Query with the new constraint

publicQuerystartAt(boolean value, String key)

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Parameters
valueThe priority to start at, inclusive
keyThe key to start at, inclusive
Returns
  • A Query with the new constraint

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2021-04-28 UTC.