QueryDocumentSnapshot
Source: document.
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'.
- Extends
- DocumentSnapshot
Properties
Methods
Properties
createTime unknown
The time the document was created.
Example
let query = firestore.collection('col');
query.get().forEach(snapshot => {
console.log(`Document created at '${snapshot.createTime.toDate()}'`);
});
- Inherited from
- QueryDocumentSnapshot#createTime
exists unknown
True if the document exists.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then((documentSnapshot) => {
if (documentSnapshot.exists) {
console.log(`Data: ${JSON.stringify(documentSnapshot.data())}`);
}
});
- Inherited from
- DocumentSnapshot#exists
id unknown
The ID of the document for which this DocumentSnapshot contains data.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then((documentSnapshot) => {
if (documentSnapshot.exists) {
console.log(`Document found with name '${documentSnapshot.id}'`);
}
});
- Inherited from
- DocumentSnapshot#id
readTime unknown
The time this snapshot was read.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then(documentSnapshot => {
let readTime = documentSnapshot.readTime;
console.log(`Document read at '${readTime.toDate()}'`);
});
- Inherited from
- DocumentSnapshot#readTime
ref unknown
A DocumentReference for the document stored in this snapshot.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then((documentSnapshot) => {
if (documentSnapshot.exists) {
console.log(`Found document at '${documentSnapshot.ref.path}'`);
}
});
- Inherited from
- DocumentSnapshot#ref
updateTime unknown
The time the document was last updated (at the time the snapshot was generated).
Example
let query = firestore.collection('col');
query.get().forEach(snapshot => {
console.log(`Document updated at '${snapshot.updateTime.toDate()}'`);
});
- Inherited from
- QueryDocumentSnapshot#updateTime
Methods
data() → DocumentData
Retrieves all fields in the document as an object.
Example
let query = firestore.collection('col');
query.get().forEach(documentSnapshot => {
let data = documentSnapshot.data();
console.log(`Retrieved data: ${JSON.stringify(data)}`);
});
- Inherited from
- QueryDocumentSnapshot#data
- Returns
-
An object containing all fields in the document.
get(field) → any type
Retrieves the field specified by field.
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}`);
});
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
field |
|
|
The field path (e.g. 'foo' or 'foo.bar') to a specific field. |
- Inherited from
- DocumentSnapshot#get
- Returns
-
The data at the specified field location or undefined if no such field exists.
isEqual(other) → boolean
Returns true if the document's data and path in this DocumentSnapshot is
equal to the provided value.
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
other |
|
|
The value to compare against. |
- Inherited from
- DocumentSnapshot#isEqual
- Returns
-
true if this
DocumentSnapshotis equal to the provided value.