Class AsyncDocumentReference (2.3.4)

AsyncDocumentReference(*path, **kwargs)

A reference to a document in a Firestore database.

The document may already exist or can be created by this class.

Parameters

NameDescription
path Tuple[str, ...]

The components in the document path. This is a series of strings representing each collection and sub-collection ID, as well as the document IDs for any documents that contain a sub-collection (as well as the base document).

kwargs dict

The keyword arguments for the constructor. The only supported keyword is client and it must be a Client. It represents the client that created this document reference.

Methods

collections

collections(page_size: Optional[int] = None, retry: google.api_core.retry.Retry = <_MethodDefault._DEFAULT_VALUE: <object object>>, timeout: Optional[float] = None)

List subcollections of the current document.

Parameters
NameDescription
page_size Optional[int]]

The maximum number of collections in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API.

retry google.api_core.retry.Retry

Designation of what errors, if any, should be retried. Defaults to a system-specified policy.

timeout float

The timeout for this request. Defaults to a system-specified value.

Returns
TypeDescription
Sequence[AsyncCollectionReference]iterator of subcollections of the current document. If the document does not exist at the time of snapshot, the iterator will be empty

create

create(document_data: dict, retry: google.api_core.retry.Retry = <_MethodDefault._DEFAULT_VALUE: <object object>>, timeout: Optional[float] = None)

Create the current document in the Firestore database.

Parameters
NameDescription
document_data dict

Property names and values to use for creating a document.

retry google.api_core.retry.Retry

Designation of what errors, if any, should be retried. Defaults to a system-specified policy.

timeout float

The timeout for this request. Defaults to a system-specified value.

Exceptions
TypeDescription
google.cloud.exceptions.ConflictIf the document already exists.
Returns
TypeDescription
WriteResultThe write result corresponding to the committed document. A write result contains an update_time field.

delete

delete(option: Optional[google.cloud.firestore_v1._helpers.WriteOption] = None, retry: google.api_core.retry.Retry = <_MethodDefault._DEFAULT_VALUE: <object object>>, timeout: Optional[float] = None)

Delete the current document in the Firestore database.

Parameters
NameDescription
option Optional[WriteOption]

A write option to make assertions / preconditions on the server state of the document before applying changes.

retry google.api_core.retry.Retry

Designation of what errors, if any, should be retried. Defaults to a system-specified policy.

timeout float

The timeout for this request. Defaults to a system-specified value.

Returns
TypeDescription
google.protobuf.timestamp_pb2.TimestampThe time that the delete request was received by the server. If the document did not exist when the delete was sent (i.e. nothing was deleted), this method will still succeed and will still return the time that the request was received by the server.

get

get(field_paths: Optional[Iterable[str]] = None, transaction=None, retry: google.api_core.retry.Retry = <_MethodDefault._DEFAULT_VALUE: <object object>>, timeout: Optional[float] = None)

Retrieve a snapshot of the current document.

See xref_field_path for more information on field paths.

If a transaction is used and it already has write operations added, this method cannot be used (i.e. read-after-write is not allowed).

Parameters
NameDescription
field_paths Optional[Iterable[str, ...]]

An iterable of field paths (.-delimited list of field names) to use as a projection of document fields in the returned results. If no value is provided, all fields will be returned.

retry google.api_core.retry.Retry

Designation of what errors, if any, should be retried. Defaults to a system-specified policy.

timeout float

The timeout for this request. Defaults to a system-specified value.

transaction Optional[AsyncTransaction]

An existing transaction that this reference will be retrieved in.

Returns
TypeDescription
DocumentSnapshotA snapshot of the current document. If the document does not exist at the time of the snapshot is taken, the snapshot's reference, data, update_time, and create_time attributes will all be None and its exists attribute will be False.

set

set(document_data: dict, merge: bool = False, retry: google.api_core.retry.Retry = <_MethodDefault._DEFAULT_VALUE: <object object>>, timeout: Optional[float] = None)

Replace the current document in the Firestore database.

A write option can be specified to indicate preconditions of the "set" operation. If no option is specified and this document doesn't exist yet, this method will create it.

Overwrites all content for the document with the fields in document_data. This method performs almost the same functionality as create. The only difference is that this method doesn't make any requirements on the existence of the document (unless option is used), whereas as create will fail if the document already exists.

Parameters
NameDescription
document_data dict

Property names and values to use for replacing a document.

merge Optional[bool] or Optional[List

If True, apply merging instead of overwriting the state of the document.

retry google.api_core.retry.Retry

Designation of what errors, if any, should be retried. Defaults to a system-specified policy.

timeout float

The timeout for this request. Defaults to a system-specified value.

Returns
TypeDescription
WriteResultThe write result corresponding to the committed document. A write result contains an update_time field.

update

update(field_updates: dict, option: Optional[google.cloud.firestore_v1._helpers.WriteOption] = None, retry: google.api_core.retry.Retry = <_MethodDefault._DEFAULT_VALUE: <object object>>, timeout: Optional[float] = None)

Update an existing document in the Firestore database.

By default, this method verifies that the document exists on the server before making updates. A write option can be specified to override these preconditions.

Each key in field_updates can either be a field name or a field path (For more information on field paths, see xref_field_path.) To illustrate this, consider a document with

>>> snapshot = await document.get()
>>> snapshot.to_dict()
{
    'foo': {
        'bar': 'baz',
    },
    'other': True,
}

stored on the server. If the field name is used in the update:

>>> field_updates = {
...     'foo': {
...         'quux': 800,
...     },
... }
>>> await document.update(field_updates)

then all of foo will be overwritten on the server and the new value will be

>>> snapshot = await document.get()
>>> snapshot.to_dict()
{
    'foo': {
        'quux': 800,
    },
    'other': True,
}

On the other hand, if a .-delimited field path is used in the update:

>>> field_updates = {
...     'foo.quux': 800,
... }
>>> await document.update(field_updates)

then only foo.quux will be updated on the server and the field foo.bar will remain intact:

>>> snapshot = await document.get()
>>> snapshot.to_dict()
{
    'foo': {
        'bar': 'baz',
        'quux': 800,
    },
    'other': True,
}

>>> field_updates = {
...     'other': firestore.DELETE_FIELD,
... }
>>> await document.update(field_updates)

would update the value on the server to:

>>> snapshot = await document.get()
>>> snapshot.to_dict()
{
    'foo': {
        'bar': 'baz',
    },
}

To set a field to the current time on the server when the update is received, use the xref_SERVER_TIMESTAMP sentinel. Sending

>>> field_updates = {
...     'foo.now': firestore.SERVER_TIMESTAMP,
... }
>>> await document.update(field_updates)

would update the value on the server to:

>>> snapshot = await document.get()
>>> snapshot.to_dict()
{
    'foo': {
        'bar': 'baz',
        'now': datetime.datetime(2012, ...),
    },
    'other': True,
}
Parameters
NameDescription
field_updates dict

Field names or paths to update and values to update with.

option Optional[WriteOption]

A write option to make assertions / preconditions on the server state of the document before applying changes.

retry google.api_core.retry.Retry

Designation of what errors, if any, should be retried. Defaults to a system-specified policy.

timeout float

The timeout for this request. Defaults to a system-specified value.

Exceptions
TypeDescription
google.cloud.exceptions.NotFoundIf the document does not exist.
Returns
TypeDescription
WriteResultThe write result corresponding to the updated document. A write result contains an update_time field.