QuerySnapshot

QuerySnapshot

new QuerySnapshot()

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

Members

(readonly) docs :Array.<QueryDocumentSnapshot>

An array of all the documents in this QuerySnapshot.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  let docs = querySnapshot.docs;
  for (let doc of docs) {
    console.log(`Document found at path: ${doc.ref.path}`);
  }
});

(readonly) empty :boolean

True if there are no documents in the QuerySnapshot.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  if (querySnapshot.empty) {
    console.log('No documents found.');
  }
});

(readonly) query :Query

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

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.limit(10).get().then(querySnapshot => {
  console.log(`Returned first batch of results`);
  let query = querySnapshot.query;
  return query.offset(10).get();
}).then(() => {
  console.log(`Returned second batch of results`);
});

readTime :Timestamp

The time this query snapshot was obtained.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then((querySnapshot) => {
  let readTime = querySnapshot.readTime;
  console.log(`Query results returned at '${readTime.toDate()}'`);
});

(readonly) size :number

The number of documents in the QuerySnapshot.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  console.log(`Found ${querySnapshot.size} documents.`);
});

Methods

docChanges() → {Array.<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.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.onSnapshot(querySnapshot => {
  let changes = querySnapshot.docChanges();
  for (let change of changes) {
    console.log(`A document was ${change.type}.`);
  }
});

forEach(callback, thisArgopt)

Enumerates all of the documents in the QuerySnapshot. This is a convenience method for running the same callback on each QueryDocumentSnapshot that is returned.

Parameters:
Name Type Attributes Description
callback function

A callback to be called with a QueryDocumentSnapshot for each document in the snapshot.

thisArg * <optional>

The this binding for the callback..

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  querySnapshot.forEach(documentSnapshot => {
    console.log(`Document found at path: ${documentSnapshot.ref.path}`);
  });
});

isEqual(other) → {boolean}

Returns true if the document data in this QuerySnapshot is equal to the provided value.

Parameters:
Name Type Description
other *

The value to compare against.

QuerySnapshot

new QuerySnapshot(_query, _readTime, _size, docs, changes)

Parameters:
Name Type Description
_query

The originating query.

_readTime

The time when this query snapshot was obtained.

_size

The number of documents in the result set.

docs

A callback returning a sorted array of documents matching this query

changes

A callback returning a sorted array of document change events for this snapshot.

Members

(readonly) docs :Array.<QueryDocumentSnapshot>

An array of all the documents in this QuerySnapshot.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  let docs = querySnapshot.docs;
  for (let doc of docs) {
    console.log(`Document found at path: ${doc.ref.path}`);
  }
});

(readonly) empty :boolean

True if there are no documents in the QuerySnapshot.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  if (querySnapshot.empty) {
    console.log('No documents found.');
  }
});

(readonly) query :Query

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

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.limit(10).get().then(querySnapshot => {
  console.log(`Returned first batch of results`);
  let query = querySnapshot.query;
  return query.offset(10).get();
}).then(() => {
  console.log(`Returned second batch of results`);
});

readTime :Timestamp

The time this query snapshot was obtained.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then((querySnapshot) => {
  let readTime = querySnapshot.readTime;
  console.log(`Query results returned at '${readTime.toDate()}'`);
});

(readonly) size :number

The number of documents in the QuerySnapshot.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  console.log(`Found ${querySnapshot.size} documents.`);
});

Methods

docChanges() → {Array.<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.

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.onSnapshot(querySnapshot => {
  let changes = querySnapshot.docChanges();
  for (let change of changes) {
    console.log(`A document was ${change.type}.`);
  }
});

forEach(callback, thisArgopt)

Enumerates all of the documents in the QuerySnapshot. This is a convenience method for running the same callback on each QueryDocumentSnapshot that is returned.

Parameters:
Name Type Attributes Description
callback function

A callback to be called with a QueryDocumentSnapshot for each document in the snapshot.

thisArg * <optional>

The this binding for the callback..

Example
let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  querySnapshot.forEach(documentSnapshot => {
    console.log(`Document found at path: ${documentSnapshot.ref.path}`);
  });
});

isEqual(other) → {boolean}

Returns true if the document data in this QuerySnapshot is equal to the provided value.

Parameters:
Name Type Description
other *

The value to compare against.