message  Send Feedback

QuerySnapshot

A QuerySnapshot contains zero or more DocumentSnapshot objects representing the results of a query. The documents can be accessed as an array via the docs property or enumerated using the forEach method. The number of documents can be determined via the empty and size properties.

Properties

Methods


Properties

docs

docs: DocumentSnapshot[];

An array of all the documents in the QuerySnapshot.


empty

empty: boolean;

True if there are no documents in the QuerySnapshot.


metadata

metadata: SnapshotMetadata;

Metadata about this snapshot, concerning its source and if it has local modifications.


query

query: Query;

The query on which you called get or onSnapshot in order to get this QuerySnapshot.


size

size: number;

The number of documents in the QuerySnapshot.


Methods

docChanges

docChanges(
  options?: SnapshotListenOptions
): DocumentChange[];

Returns an array of the documents changes since the last snapshot. If this is the first snapshot, all documents will be in the list as added changes.

To include metadata changes, ensure that the onSnapshot() method includes metadata changes.

Example

firebase.firestore().collection('users')
.onSnapshot((querySnapshot) => {
console.log('Metadata Changes', querySnapshot.docChanges());
});

Example - With metadata changes

firebase.firestore().collection('users')
.onSnapshot({ includeMetadataChanges: true }, (querySnapshot) => {
console.log('Metadata Changes', querySnapshot.docChanges({
includeMetadataChanges: true,
}));
});

Parameters

  • options: (optional) SnapshotListenOptions
    `SnapshotListenOptions` that control whether metadata-only changes (i.e. only `DocumentSnapshot.metadata` changed) should trigger snapshot events.

forEach

forEach(
  callback: Function,
  thisArg?: any
): void;

Enumerates all of the documents in the QuerySnapshot.

Example

const querySnapshot = await firebase.firestore().collection('users').get();
querySnapshot.forEach((documentSnapshot) => {
console.log('User', documentSnapshot.data());
})

Parameters

  • callback: Function
    A callback to be called with a `DocumentSnapshot` for each document in the snapshot.
  • thisArg: (optional) any
    The `this` binding for the callback.

isEqual

isEqual(
  other: QuerySnapshot
): boolean;

Returns true if this QuerySnapshot is equal to the provided one.

Example

const querySnapshot1 = await firebase.firestore().collection('users').limit(5).get();
const querySnapshot2 = await firebase.firestore().collection('users').limit(10).get();
// false
querySnapshot1.isEqual(querySnapshot2);

This operation can be resource intensive when dealing with large datasets.

Parameters


React Native Market

Looking for pre-built React Native apps to kick start your next project? Check out the React Native Market by Invertase.

Your purchases help support our open-source projects such as React Native Firebase. All items are currently 50% off to celebrate the release of React Native Firebase version 6.
shopping_cartVisit React Native Market