Document

Document reference

the document reference identifies a document within a collection, and embeds information about the parent collection.

parent is the the collection reference, id is the document's id. path is the document's absolute path as a string.

docRef.parent
docRef.id
docRed.path

use reference for CRUD operations

We provide the document reference for CRUD operations:

  • create or override (upsert) the document - setDoc(ref, data)

  • read the document - getDoc(ref)

  • update an existing document (it errors if the document is not found) - updateDoc(ref, data)

  • delete the document - deleteDoc(ref)

build a document reference

The document's path identifies it uniquely. We provide it either as a single string or build it from multiple strings.

Alternatively, we may provide the collectionRef and the document ID.

We may also let Firestore creates a reference for us. In that case, we only need the parent collection's reference.

  • compute a reference out of a path or ID.
const docRef = doc(collectionRef, id)
const docRef = doc(collectionRef) // ID is generated by firestore

const docRef = doc(db, "users", id) // collectionRef-less alternative
const docRef = doc(db, "users", "Nk....WQ")

const docRef = doc(db, "users/Nk....WQ") // path as single argument
const docRef = doc(db, "users/" + id)

const docRef = collectionRef.doc("John") // admin sdk
const docRef = collectionRef.doc("NkJz11WQ")

read single document

at ref, or at id

getDoc(docRef)
db.collection("messages").doc(id).get()

Document snapshot

The document snapshot is a wrapper that does not guarantee the document existence.

When we request a document, we receive a snapshot, which may be empty in the sense of not containing data, but it still includes metadata.

The data, if any, is accessible at snapshot.data(options).

We may provide a config, though it's unneeded most of the time.

We may subscribe to receive snapshots in realtime on document change.

work with the snapshot and the underlying document.

check document existence

docSnapshot.exists()

get the underlying document if it exists.

docSnapshot.data() // undefined if document doesn't exist
// typescript infered type: DocumentData | undefined
if (!data) { .. }

guard against document inexistence (redundant but needed by typescript).

other props and methods

requested id, provided ref, resulting metadata.

docSnapshot.id
docSnapshot.ref
docSnapshot.metadata

get a single property directly through the document snapshot.

docSnapshot.get("phoneNumber")
earlymorning logo

© 2025 - All rights reserved

Document

Document reference

the document reference identifies a document within a collection, and embeds information about the parent collection.

parent is the the collection reference, id is the document's id. path is the document's absolute path as a string.

docRef.parent
docRef.id
docRed.path

use reference for CRUD operations

We provide the document reference for CRUD operations:

  • create or override (upsert) the document - setDoc(ref, data)

  • read the document - getDoc(ref)

  • update an existing document (it errors if the document is not found) - updateDoc(ref, data)

  • delete the document - deleteDoc(ref)

build a document reference

The document's path identifies it uniquely. We provide it either as a single string or build it from multiple strings.

Alternatively, we may provide the collectionRef and the document ID.

We may also let Firestore creates a reference for us. In that case, we only need the parent collection's reference.

  • compute a reference out of a path or ID.
const docRef = doc(collectionRef, id)
const docRef = doc(collectionRef) // ID is generated by firestore

const docRef = doc(db, "users", id) // collectionRef-less alternative
const docRef = doc(db, "users", "Nk....WQ")

const docRef = doc(db, "users/Nk....WQ") // path as single argument
const docRef = doc(db, "users/" + id)

const docRef = collectionRef.doc("John") // admin sdk
const docRef = collectionRef.doc("NkJz11WQ")

read single document

at ref, or at id

getDoc(docRef)
db.collection("messages").doc(id).get()

Document snapshot

The document snapshot is a wrapper that does not guarantee the document existence.

When we request a document, we receive a snapshot, which may be empty in the sense of not containing data, but it still includes metadata.

The data, if any, is accessible at snapshot.data(options).

We may provide a config, though it's unneeded most of the time.

We may subscribe to receive snapshots in realtime on document change.

work with the snapshot and the underlying document.

check document existence

docSnapshot.exists()

get the underlying document if it exists.

docSnapshot.data() // undefined if document doesn't exist
// typescript infered type: DocumentData | undefined
if (!data) { .. }

guard against document inexistence (redundant but needed by typescript).

other props and methods

requested id, provided ref, resulting metadata.

docSnapshot.id
docSnapshot.ref
docSnapshot.metadata

get a single property directly through the document snapshot.

docSnapshot.get("phoneNumber")