DocumentSnapshot
Source: document.
A DocumentSnapshot is an immutable representation for a document in a Firestore database. The data can be extracted with data() or get(fieldPath) to get a specific field.
For a DocumentSnapshot that points to a non-existing document, any data access will return 'undefined'. You can use the exists property to explicitly verify a document's existence.
Properties
Methods
Properties
createTime (Timestamp or undefined)
The time the document was created. Undefined for documents that don't exist.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then(documentSnapshot => {
if (documentSnapshot.exists) {
let createTime = documentSnapshot.createTime;
console.log(`Document created at '${createTime.toDate()}'`);
}
});
exists boolean
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())}`);
}
});
id string
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}'`);
}
});
readTime Timestamp
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()}'`);
});
ref DocumentReference
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}'`);
}
});
updateTime (Timestamp or undefined)
The time the document was last updated (at the time the snapshot was generated). Undefined for documents that don't exist.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then(documentSnapshot => {
if (documentSnapshot.exists) {
let updateTime = documentSnapshot.updateTime;
console.log(`Document updated at '${updateTime.toDate()}'`);
}
});
Methods
data() → (DocumentData or undefined)
Retrieves all fields in the document as an object. Returns 'undefined' if the document doesn't exist.
Example
let documentRef = firestore.doc('col/doc');
documentRef.get().then(documentSnapshot => {
let data = documentSnapshot.data();
console.log(`Retrieved data: ${JSON.stringify(data)}`);
});
- Returns
-
(DocumentData or undefined)An object containing all fields in the document or 'undefined' if the document doesn't exist.
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 |
(string or FieldPath) |
|
The field path (e.g. 'foo' or 'foo.bar') to a specific field. |
- Returns
-
any typeThe 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 |
any type |
|
The value to compare against. |
- Returns
-
booleantrue if this
DocumentSnapshotis equal to the provided value.