Class DocumentReference (3.7.9)

public class 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.

Inheritance

Object > DocumentReference

Methods

addSnapshotListener(EventListener<DocumentSnapshot> listener)

public ListenerRegistration addSnapshotListener(EventListener<DocumentSnapshot> listener)

Starts listening to the document referenced by this DocumentReference.

Parameter
NameDescription
listenerEventListener<DocumentSnapshot>

The event listener that will be called with the snapshots.

Returns
TypeDescription
ListenerRegistration

A registration object that can be used to remove the listener.

addSnapshotListener(Executor executor, EventListener<DocumentSnapshot> listener)

public ListenerRegistration addSnapshotListener(Executor executor, EventListener<DocumentSnapshot> listener)

Starts listening to the document referenced by this DocumentReference.

Parameters
NameDescription
executorExecutor

The executor to use to call the listener.

listenerEventListener<DocumentSnapshot>

The event listener that will be called with the snapshots.

Returns
TypeDescription
ListenerRegistration

A registration object that can be used to remove the listener.

collection(String collectionPath)

public CollectionReference collection(String collectionPath)

Gets a CollectionReference instance that refers to the collection that is a child of this document.

Parameter
NameDescription
collectionPathString

A relative and slash-separated path to a collection.

Returns
TypeDescription
CollectionReference

The CollectionReference instance.

create(Object pojo)

public ApiFuture<WriteResult> create(Object pojo)

Creates a new Document at the DocumentReference location. It fails the write if the document exists.

Parameter
NameDescription
pojoObject

The POJO that will be used to populate the document contents.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

create(Map<String,Object> fields)

public ApiFuture<WriteResult> create(Map<String,Object> fields)

Creates a new Document at the DocumentReference's Location. It fails the write if the document exists.

Parameter
NameDescription
fieldsMap<String,Object>

A map of the fields and values for the document.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

delete()

public ApiFuture<WriteResult> delete()

Deletes the document referred to by this DocumentReference.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the delete completes.

delete(Precondition options)

public ApiFuture<WriteResult> delete(Precondition options)

Deletes the document referred to by this DocumentReference.

Parameter
NameDescription
optionsPrecondition

Preconditions to enforce for this delete.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the delete completes.

equals(Object obj)

public boolean equals(Object obj)

Returns true if this DocumentReference is equal to the provided object.

Parameter
NameDescription
objObject

The object to compare against.

Returns
TypeDescription
boolean

Whether this DocumentReference is equal to the provided object.

Overrides

get()

public ApiFuture<DocumentSnapshot> get()

Reads the document referenced by this DocumentReference. If the document doesn't exist, the get() will return an an empty DocumentSnapshot.

Returns
TypeDescription
ApiFuture<DocumentSnapshot>

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(FieldMask fieldMask)

public ApiFuture<DocumentSnapshot> get(FieldMask fieldMask)

Reads the document referenced by this DocumentReference. If the document doesn't exist, the get(FieldMask fieldMask) will return an empty DocumentSnapshot.

Parameter
NameDescription
fieldMaskFieldMask

A FieldMask object to retrieve the field value

Returns
TypeDescription
ApiFuture<DocumentSnapshot>

An ApiFuture that will be resolved with the contents of the Document at this DocumentReference, or a failure if the document does not exist

getFirestore()

public Firestore getFirestore()
Returns
TypeDescription
Firestore

getId()

public String getId()

The id of a document refers to the last component of path pointing to a document, e.g. "document-id" in "projects/project-id/databases/database-id/document-id".

Returns
TypeDescription
String

The ID of the document.

getParent()

public CollectionReference getParent()

A reference to the Collection to which this DocumentReference belongs to.

Returns
TypeDescription
CollectionReference

The parent Collection.

getPath()

public String getPath()

A string representing the path of the referenced document (relative to the root of the database).

Returns
TypeDescription
String

The slash-separated path relative to the root of the database.

hashCode()

public int hashCode()
Returns
TypeDescription
int
Overrides

listCollections()

public Iterable<CollectionReference> listCollections()

Fetches the subcollections that are direct children of this document.

Returns
TypeDescription
Iterable<CollectionReference>

An Iterable that can be used to fetch all subcollections.

set(Object pojo)

public ApiFuture<WriteResult> set(Object pojo)

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.

Parameter
NameDescription
pojoObject

The POJO that will be used to populate the document contents.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

set(Object pojo, SetOptions options)

public ApiFuture<WriteResult> set(Object pojo, SetOptions options)

Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you pass SetOptions, the provided data can be merged into an existing document.

Parameters
NameDescription
pojoObject

The POJO that will be used to populate the document contents.

optionsSetOptions

An object to configure the set behavior.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

set(Map<String,Object> fields)

public ApiFuture<WriteResult> set(Map<String,Object> fields)

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.

Parameter
NameDescription
fieldsMap<String,Object>

A map of the fields and values for the document.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

set(Map<String,Object> fields, SetOptions options)

public ApiFuture<WriteResult> set(Map<String,Object> fields, SetOptions options)

Writes to the document referred to by this DocumentReference. If the document does not yet exist, it will be created. If you pass SetOptions, the provided data can be merged into an existing document.

Parameters
NameDescription
fieldsMap<String,Object>

A map of the fields and values for the document.

optionsSetOptions

An object to configure the set behavior.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

toString()

public String toString()
Returns
TypeDescription
String
Overrides

update(FieldPath fieldPath, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(FieldPath fieldPath, 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
NameDescription
fieldPathFieldPath

The first field to set

valueObject

The first value to set

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

A ApiFuture that will be resolved when the write finishes.

update(Precondition options, FieldPath fieldPath, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(Precondition options, FieldPath fieldPath, 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
NameDescription
optionsPrecondition

Preconditions to enforce on this update.

fieldPathFieldPath

The first field to set

valueObject

The first value to set

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

A ApiFuture that will be resolved when the write finishes.

update(Precondition options, String field, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(Precondition options, String field, 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
NameDescription
optionsPrecondition

Preconditions to enforce on this update.

fieldString

The first field to set

valueObject

The first value to set

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

A ApiFuture that will be resolved when the write finishes.

update(String field, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(String field, 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
NameDescription
fieldString

The first field to set

valueObject

The first value to set

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

update(Map<String,Object> fields)

public ApiFuture<WriteResult> update(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.

Parameter
NameDescription
fieldsMap<String,Object>

A Map containing the fields and values with which to update the document.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.

update(Map<String,Object> fields, Precondition options)

public ApiFuture<WriteResult> update(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
NameDescription
fieldsMap<String,Object>

A map containing the fields and values with which to update the document.

optionsPrecondition

Preconditions to enforce on this update.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture that will be resolved when the write finishes.