Cloud Firestore Client - Class DocumentReference (1.40.0)

Reference documentation and code samples for the Cloud Firestore Client class DocumentReference.

Represents a reference to a Firestore document.

Example:

use Google\Cloud\Firestore\FirestoreClient;

$firestore = new FirestoreClient();
$document = $firestore->document('users/john');

Namespace

Google \ Cloud \ Firestore

Methods

__construct

Parameters
NameDescription
connection Google\Cloud\Firestore\Connection\ConnectionInterface

A Connection to Cloud Firestore. This object is created by FirestoreClient, and should not be instantiated outside of this client.

valueMapper Google\Cloud\Firestore\ValueMapper

A Firestore Value Mapper.

parent Google\Cloud\Firestore\CollectionReference

The collection in which this document is contained.

name string

The fully-qualified document name.

parent

Returns the parent collection.

Example:

$parent = $document->parent();
Returns
TypeDescription
Google\Cloud\Firestore\CollectionReference

name

Get the document name.

Names are absolute. The result of this call would be of the form projects/<project-id>/databases/<database-id>/documents/<relative-path>.

Other methods are available to retrieve different parts of a collection name:

Example:

$name = $document->name();
Returns
TypeDescription
string

path

Get the document path.

Paths identify the location of a document, relative to the database name.

To retrieve the document ID (the last element of the path), use Google\Cloud\Firestore\DocumentReference::id().

Example:

$path = $document->path();
Returns
TypeDescription
string

id

Get the document identifier (i.e. the last path element).

IDs are the path element which identifies a resource. To retrieve the path of a resource, relative to the database name, use Google\Cloud\Firestore\DocumentReference::path().

Example:

$id = $document->id();
Returns
TypeDescription
string

create

See also:

Parameters
NameDescription
fields array

An array containing fields, where keys are the field names, and values are field values. Nested arrays are allowed. Note that unlike Google\Cloud\Firestore\DocumentReference::update(), field paths are NOT supported by this method.

options array

Configuration Options.

Returns
TypeDescription
array[WriteResult](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.WriteResult)

set

See also:

Parameters
NameDescription
fields array

An array containing fields, where keys are the field names, and values are field values. Nested arrays are allowed. Note that unlike Google\Cloud\Firestore\DocumentReference::update(), field paths are NOT supported by this method.

options array

Configuration Options

↳ merge bool

If true, unwritten fields will be preserved. Otherwise, they will be overwritten (removed). Defaults to false.

Returns
TypeDescription
array[WriteResult](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.WriteResult)

update

See also:

Parameters
NameDescription
data array[]

A list of arrays of form [FieldPath|string $path, mixed $value].

options array

Configuration options

Returns
TypeDescription
array[WriteResult](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.WriteResult)

delete

See also:

Parameter
NameDescription
options array

Configuration Options

Returns
TypeDescription
array[WriteResult](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.WriteResult)

snapshot

Parameter
NameDescription
options array

Configuration Options

Returns
TypeDescription
Google\Cloud\Firestore\DocumentSnapshot

collection

Get a reference to a collection which is a child of the current document.

Example:

$child = $document->collection('wallet');
Parameter
NameDescription
collectionId string

The ID of the child collection.

Returns
TypeDescription
Google\Cloud\Firestore\CollectionReference

collections

List all collections which are children of the current document.

Example:

$collections = $document->collections();
Parameter
NameDescription
options array

Configuration options.

Returns
TypeDescription
Google\Cloud\Core\Iterator\ItemIterator<\google\cloud\firestore\collectionreference>