ADocumentReference refers to a document location in a Firestore databaseand can be used to write, read, or listen to the location. The document atthe referenced location may or may not exist. ADocumentReference canalso be used to create aCollectionReference to a subcollection.
Type parameters
T
Index
Constructors
Properties
Methods
Constructors
Private constructor
- new
Document Reference():DocumentReference ReturnsDocumentReference
Properties
firestore
Thefirebase.firestore.Firestore the document is in.This is useful for performing transactions, for example.
id
The document's identifier within its collection.
parent
The Collection thisDocumentReference belongs to.
path
A string representing the path of the referenced document (relativeto the root of the database).
Methods
collection
- collection(collectionPath: string):CollectionReference<DocumentData>
Gets a
CollectionReferenceinstance that refers to the collection atthe specified path.Parameters
collectionPath:string
A slash-separated path to a collection.
ReturnsCollectionReference<DocumentData>
The
CollectionReferenceinstance.
delete
- delete():Promise<void>
Deletes the document referred to by this
DocumentReference.ReturnsPromise<void>
A Promise resolved once the document has been successfullydeleted from the backend (Note that it won't resolve while you'reoffline).
get
- get(options?: GetOptions):Promise<DocumentSnapshot<T>>
Reads the document referred to by this
DocumentReference.Note: By default, get() attempts to provide up-to-date data when possibleby waiting for data from the server, but it may return cached data or failif you are offline and the server cannot be reached. This behavior can bealtered via the
GetOptionsparameter.Parameters
Optional options:GetOptions
An object to configure the get behavior.
ReturnsPromise<DocumentSnapshot<T>>
A Promise resolved with a DocumentSnapshot containing thecurrent document contents.
isEqual
- is
Equal(other: DocumentReference<T>):boolean Returns true if this
DocumentReferenceis equal to the provided one.Parameters
other:DocumentReference<T>
The
DocumentReferenceto compare against.
Returnsboolean
true if this
DocumentReferenceis equal to the provided one.
onSnapshot
- on
Snapshot(observer: {complete?:() =>void;error?:(error: FirestoreError) =>void;next?:(snapshot: DocumentSnapshot<T>) =>void }):() =>void Attaches a listener for DocumentSnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
observer:{complete?:() =>void;error?:(error:FirestoreError) =>void;next?:(snapshot:DocumentSnapshot<T>) =>void }
A single object containing
nextanderrorcallbacks.Optional complete?:() =>void
- ():void
Returnsvoid
Optional error?:(error:FirestoreError) =>void
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional next?:(snapshot:DocumentSnapshot<T>) =>void
- (snapshot:DocumentSnapshot<T>):void
Parameters
snapshot:DocumentSnapshot<T>
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
- on
Snapshot(options: SnapshotListenOptions, observer: {complete?:() =>void;error?:(error: FirestoreError) =>void;next?:(snapshot: DocumentSnapshot<T>) =>void }):() =>void Attaches a listener for DocumentSnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
options:SnapshotListenOptions
Options controlling the listen behavior.
observer:{complete?:() =>void;error?:(error:FirestoreError) =>void;next?:(snapshot:DocumentSnapshot<T>) =>void }
A single object containing
nextanderrorcallbacks.Optional complete?:() =>void
- ():void
Returnsvoid
Optional error?:(error:FirestoreError) =>void
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional next?:(snapshot:DocumentSnapshot<T>) =>void
- (snapshot:DocumentSnapshot<T>):void
Parameters
snapshot:DocumentSnapshot<T>
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
- on
Snapshot(onNext: (snapshot: DocumentSnapshot<T>) =>void, onError?: (error: FirestoreError) =>void, onCompletion?: () =>void):() =>void Attaches a listener for DocumentSnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
onNext:(snapshot:DocumentSnapshot<T>) =>void
A callback to be called every time a new
DocumentSnapshotis available.- (snapshot:DocumentSnapshot<T>):void
Parameters
snapshot:DocumentSnapshot<T>
Returnsvoid
Optional onError:(error:FirestoreError) =>void
A callback to be called if the listen fails or iscancelled. No further callbacks will occur.
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional onCompletion:() =>void
- ():void
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
- on
Snapshot(options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot<T>) =>void, onError?: (error: FirestoreError) =>void, onCompletion?: () =>void):() =>void Attaches a listener for DocumentSnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
options:SnapshotListenOptions
Options controlling the listen behavior.
onNext:(snapshot:DocumentSnapshot<T>) =>void
A callback to be called every time a new
DocumentSnapshotis available.- (snapshot:DocumentSnapshot<T>):void
Parameters
snapshot:DocumentSnapshot<T>
Returnsvoid
Optional onError:(error:FirestoreError) =>void
A callback to be called if the listen fails or iscancelled. No further callbacks will occur.
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional onCompletion:() =>void
- ():void
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
set
- set(data: Partial<T>, options: SetOptions):Promise<void>
Writes to the document referred to by this
DocumentReference. If thedocument does not yet exist, it will be created. If you passSetOptions, the provided data can be merged into an existing document.Parameters
data:Partial<T>
A map of the fields and values for the document.
options:SetOptions
An object to configure the set behavior.
ReturnsPromise<void>
A Promise resolved once the data has been successfully writtento the backend (Note that it won't resolve while you're offline).
- set(data: T):Promise<void>
Writes to the document referred to by this
DocumentReference. If thedocument does not yet exist, it will be created. If you passSetOptions, the provided data can be merged into an existing document.Parameters
data:T
A map of the fields and values for the document.
ReturnsPromise<void>
A Promise resolved once the data has been successfully writtento the backend (Note that it won't resolve while you're offline).
update
- update(data: UpdateData):Promise<void>
Updates fields in the document referred to by this
DocumentReference.The update will fail if applied to a document that does not exist.Parameters
data:UpdateData
An object containing the fields and values with which toupdate the document. Fields can contain dots to reference nested fieldswithin the document.
ReturnsPromise<void>
A Promise resolved once the data has been successfully writtento the backend (Note that it won't resolve while you're offline).
- update(field: string |FieldPath, value: any, ...moreFieldsAndValues: any[]):Promise<void>
Updates fields in the document referred to by this
DocumentReference.The update will fail if applied to a document that does not exist.Nested fields can be updated by providing dot-separated field pathstrings or by providing FieldPath objects.
Parameters
field:string |FieldPath
The first field to update.
value:any
The first value.
Rest...moreFieldsAndValues:any[]
Additional key value pairs.
ReturnsPromise<void>
A Promise resolved once the data has been successfully writtento the backend (Note that it won't resolve while you're offline).
withConverter
- with
Converter(converter: null):DocumentReference<DocumentData> Applies a custom data converter to this DocumentReference, allowing youto use your own custom model objects with Firestore. When you callset(), get(), etc. on the returned DocumentReference instance, theprovided converter will convert between Firestore data and your customtype U.
Passing in
nullas the converter parameter removes the currentconverter.Parameters
converter:null
Converts objects to and from Firestore. Passing in
nullremoves the current converter.
ReturnsDocumentReference<DocumentData>
A DocumentReference that uses the provided converter.
- with
Converter<U>(converter: FirestoreDataConverter<U>):DocumentReference<U> Applies a custom data converter to this DocumentReference, allowing youto use your own custom model objects with Firestore. When you callset(), get(), etc. on the returned DocumentReference instance, theprovided converter will convert between Firestore data and your customtype U.
Passing in
nullas the converter parameter removes the currentconverter.Type parameters
U
Parameters
converter:FirestoreDataConverter<U>
Converts objects to and from Firestore. Passing in
nullremoves the current converter.
ReturnsDocumentReference<U>
A DocumentReference that uses the provided converter.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-07-27 UTC.