Package com.google.cloud.firestore
Class DocumentReference
java.lang.Object
com.google.cloud.firestore.DocumentReference
A DocumentReference refers to a document location in a Firestore database and can be used to
write, read, or listen to the location. There may or may not exist a document at the referenced
location. A DocumentReference can also be used to create a CollectionReference to a
subcollection.
Subclassing Note: Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.
-
Method Summary
Modifier and TypeMethodDescriptionaddSnapshotListener(EventListener<DocumentSnapshot> listener) Starts listening to the document referenced by this DocumentReference.addSnapshotListener(Executor executor, EventListener<DocumentSnapshot> listener) Starts listening to the document referenced by this DocumentReference.collection(String collectionPath) Gets a CollectionReference instance that refers to the collection that is a child of this document.com.google.api.core.ApiFuture<WriteResult>Creates a new Document at the DocumentReference location.com.google.api.core.ApiFuture<WriteResult>Creates a new Document at the DocumentReference's Location.com.google.api.core.ApiFuture<WriteResult>delete()Deletes the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>delete(Precondition options) Deletes the document referred to by this DocumentReference.booleanReturns true if this DocumentReference is equal to the provided object.com.google.api.core.ApiFuture<DocumentSnapshot>get()Reads the document referenced by this DocumentReference.com.google.api.core.ApiFuture<DocumentSnapshot>Reads the document referenced by this DocumentReference.getId()The id of a document refers to the last component of path pointing to a document, for example "document-id" in "projects/project-id/databases/database-id/document-id".A reference to the Collection to which this DocumentReference belongs to.getPath()A string representing the path of the referenced document (relative to the root of the database).inthashCode()Fetches the subcollections that are direct children of this document.com.google.api.core.ApiFuture<WriteResult>Overwrites the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>set(Object pojo, SetOptions options) Writes to the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>Overwrites the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>set(Map<String, Object> fields, SetOptions options) Writes to the document referred to by this DocumentReference.toString()com.google.api.core.ApiFuture<WriteResult>Updates the fields in the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>update(Precondition options, FieldPath fieldPath, Object value, Object... moreFieldsAndValues) Updates the fields in the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>update(Precondition options, String field, Object value, Object... moreFieldsAndValues) Updates the fields in the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>Updates the fields in the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>Updates fields in the document referred to by this DocumentReference.com.google.api.core.ApiFuture<WriteResult>update(Map<String, Object> fields, Precondition options) Updates fields in the document referred to by this DocumentReference.
-
Method Details
-
getFirestore
-
getId
The id of a document refers to the last component of path pointing to a document, for example "document-id" in "projects/project-id/databases/database-id/document-id".- Returns:
- The ID of the document.
-
getPath
A string representing the path of the referenced document (relative to the root of the database).- Returns:
- The slash-separated path relative to the root of the database.
-
getParent
A reference to the Collection to which this DocumentReference belongs to.- Returns:
- The parent Collection.
-
collection
Gets a CollectionReference instance that refers to the collection that is a child of this document.- Parameters:
collectionPath- A relative and slash-separated path to a collection.- Returns:
- The CollectionReference instance.
-
create
@Nonnull public com.google.api.core.ApiFuture<WriteResult> create(@Nonnull Map<String, Object> fields) Creates a new Document at the DocumentReference's Location. It fails the write if the document exists.- Parameters:
fields- A map of the fields and values for the document.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
create
Creates a new Document at the DocumentReference location. It fails the write if the document exists.- Parameters:
pojo- The POJO that will be used to populate the document contents.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
set
Overwrites the document referred to by this DocumentReference. If no document exists yet, it will be created. If a document already exists, it will be overwritten.- Parameters:
fields- A map of the fields and values for the document.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
set
@Nonnull public com.google.api.core.ApiFuture<WriteResult> set(@Nonnull Map<String, Object> fields, @Nonnull SetOptions options) Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you passSetOptions, the provided data can be merged into an existing document.- Parameters:
fields- A map of the fields and values for the document.options- An object to configure the set behavior.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
set
Overwrites the document referred to by this DocumentReference. If no document exists yet, it will be created. If a document already exists, it will be overwritten.- Parameters:
pojo- The POJO that will be used to populate the document contents.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
set
@Nonnull public com.google.api.core.ApiFuture<WriteResult> set(@Nonnull Object pojo, @Nonnull SetOptions options) Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you passSetOptions, the provided data can be merged into an existing document.- Parameters:
pojo- The POJO that will be used to populate the document contents.options- An object to configure the set behavior.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
update
@Nonnull public com.google.api.core.ApiFuture<WriteResult> update(@Nonnull Map<String, Object> fields) Updates fields in the document referred to by this DocumentReference. If the document doesn't exist yet, the update will fail.- Parameters:
fields- A Map containing the fields and values with which to update the document.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
update
@Nonnull public com.google.api.core.ApiFuture<WriteResult> update(@Nonnull Map<String, Object> fields, Precondition options) Updates fields in the document referred to by this DocumentReference. If the document doesn't exist yet, the update will fail.- Parameters:
fields- A map containing the fields and values with which to update the document.options- Preconditions to enforce on this update.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
update
@Nonnull public com.google.api.core.ApiFuture<WriteResult> update(@Nonnull String field, @Nullable Object value, Object... moreFieldsAndValues) Updates the fields in the document referred to by this DocumentReference. If the document doesn't exist yet, the update will fail.- Parameters:
field- The first field to setvalue- The first value to setmoreFieldsAndValues- String and Object pairs with more fields to be set.- Returns:
- An ApiFuture that will be resolved when the write finishes.
-
update
@Nonnull public com.google.api.core.ApiFuture<WriteResult> update(@Nonnull FieldPath fieldPath, @Nullable Object value, Object... moreFieldsAndValues) Updates the fields in the document referred to by this DocumentReference. If the document doesn't exist yet, the update will fail.- Parameters:
fieldPath- The first field to setvalue- The first value to setmoreFieldsAndValues- String and Object pairs with more fields to be set.- Returns:
- A ApiFuture that will be resolved when the write finishes.
-
update
@Nonnull public com.google.api.core.ApiFuture<WriteResult> update(@Nonnull Precondition options, @Nonnull String field, @Nullable Object value, Object... moreFieldsAndValues) Updates the fields in the document referred to by this DocumentReference. If the document doesn't exist yet, the update will fail.- Parameters:
options- Preconditions to enforce on this update.field- The first field to setvalue- The first value to setmoreFieldsAndValues- String and Object pairs with more fields to be set.- Returns:
- A ApiFuture that will be resolved when the write finishes.
-
update
@Nonnull public com.google.api.core.ApiFuture<WriteResult> update(@Nonnull Precondition options, @Nonnull FieldPath fieldPath, @Nullable Object value, Object... moreFieldsAndValues) Updates the fields in the document referred to by this DocumentReference. If the document doesn't exist yet, the update will fail.- Parameters:
options- Preconditions to enforce on this update.fieldPath- The first field to setvalue- The first value to setmoreFieldsAndValues- String and Object pairs with more fields to be set.- Returns:
- A ApiFuture that will be resolved when the write finishes.
-
delete
Deletes the document referred to by this DocumentReference.- Parameters:
options- Preconditions to enforce for this delete.- Returns:
- An ApiFuture that will be resolved when the delete completes.
-
delete
Deletes the document referred to by this DocumentReference.- Returns:
- An ApiFuture that will be resolved when the delete completes.
-
get
Reads the document referenced by this DocumentReference. If the document doesn't exist, the get() will return an an empty DocumentSnapshot.- Returns:
- An ApiFuture that will be resolved with the contents of the Document at this DocumentReference, or a failure if the document does not exist.
-
get
Reads the document referenced by this DocumentReference. If the document doesn't exist, the get(FieldMask fieldMask) will return an empty DocumentSnapshot.- Parameters:
fieldMask- A FieldMask object to retrieve the field value- Returns:
- An ApiFuture that will be resolved with the contents of the Document at this DocumentReference, or a failure if the document does not exist
-
listCollections
Fetches the subcollections that are direct children of this document.- Returns:
- An Iterable that can be used to fetch all subcollections.
- Throws:
FirestoreException- if the Iterable could not be initialized.
-
addSnapshotListener
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull Executor executor, @Nonnull EventListener<DocumentSnapshot> listener) Starts listening to the document referenced by this DocumentReference.- Parameters:
executor- The executor to use to call the listener.listener- The event listener that will be called with the snapshots.- Returns:
- A registration object that can be used to remove the listener.
-
addSnapshotListener
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull EventListener<DocumentSnapshot> listener) Starts listening to the document referenced by this DocumentReference.- Parameters:
listener- The event listener that will be called with the snapshots.- Returns:
- A registration object that can be used to remove the listener.
-
toString
-
equals
Returns true if this DocumentReference is equal to the provided object. -
hashCode
public int hashCode()
-