QueryDocumentSnapshot

QueryDocumentSnapshot

A QueryDocumentSnapshot contains data read from a document in your Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted with data() or get() to get a specific field.

A QueryDocumentSnapshot offers the same API surface as a DocumentSnapshot. Since query results contain only existing documents, the exists property will always be true and data() will never return 'undefined'.

Constructor

new QueryDocumentSnapshot(ref, fieldsProto, readTime, createTime, updateTime)

Parameters:
Name Type Description
ref

The reference to the document.

fieldsProto

The fields of the Firestore Document Protobuf backing this document.

readTime

The time when this snapshot was read.

createTime

The time when the document was created.

updateTime

The time when the document was last updated.

Source:

Extends

Members

(readonly) createTime :Timestamp

The time the document was created.

Overrides:
Source:
Example
let query = firestore.collection('col');

query.get().forEach(snapshot => {
  console.log(`Document created at '${snapshot.createTime.toDate()}'`);
});

(readonly) exists :boolean

True if the document exists.

Overrides:
Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    console.log(`Data: ${JSON.stringify(documentSnapshot.data())}`);
  }
});

(readonly) id :string

The ID of the document for which this DocumentSnapshot contains data.

Overrides:
Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    console.log(`Document found with name '${documentSnapshot.id}'`);
  }
});

(readonly) readTime :Timestamp

The time this snapshot was read.

Overrides:
Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then(documentSnapshot => {
  let readTime = documentSnapshot.readTime;
  console.log(`Document read at '${readTime.toDate()}'`);
});

(readonly) ref :DocumentReference

A DocumentReference for the document stored in this snapshot.

Overrides:
Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    console.log(`Found document at '${documentSnapshot.ref.path}'`);
  }
});

(readonly) updateTime :Timestamp

The time the document was last updated (at the time the snapshot was generated).

Overrides:
Source:
Example
let query = firestore.collection('col');

query.get().forEach(snapshot => {
  console.log(`Document updated at '${snapshot.updateTime.toDate()}'`);
});

Methods

data() → {DocumentData}

Retrieves all fields in the document as an object.

Overrides:
Source:
Example
let query = firestore.collection('col');

query.get().forEach(documentSnapshot => {
  let data = documentSnapshot.data();
  console.log(`Retrieved data: ${JSON.stringify(data)}`);
});

get(field) → {*}

Retrieves the field specified by field.

Parameters:
Name Type Description
field string | FieldPath

The field path (e.g. 'foo' or 'foo.bar') to a specific field.

Overrides:
Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.set({ a: { b: 'c' }}).then(() => {
  return documentRef.get();
}).then(documentSnapshot => {
  let field = documentSnapshot.get('a.b');
  console.log(`Retrieved field value: ${field}`);
});

isEqual(other) → {boolean}

Returns true if the document's data and path in this DocumentSnapshot is equal to the provided value.

Parameters:
Name Type Description
other *

The value to compare against.

Overrides:
Source: