Cloud Firestore Client - Class CollectionReference (1.38.0)

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

Represents a Cloud Firestore Collection.

Collections are implicit namespaces for Firestore Documents. They are created when the first document is inserted, and cease to exist when the last document is removed.

Example:

use Google\Cloud\Firestore\FirestoreClient;

$firestore = new FirestoreClient();
$collection = $firestore->collection('users');

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.

name string

The absolute name of the collection.

name

Get the collection 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 = $collection->name();
Returns
TypeDescription
string

path

Get the collection path.

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

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

Example:

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

id

Get the collection ID.

IDs are the path element which identifies a resource. To retrieve the full path to a resource (the resource name), use Google\Cloud\Firestore\CollectionReference::name().

Example:

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

document

Get a reference to a document which is a direct child of this collection.

Example:

$newUser = $collection->document('john');
Parameter
NameDescription
documentId string

The document ID.

Returns
TypeDescription
Google\Cloud\Firestore\DocumentReference

newDocument

Get a document reference with a randomly generated document ID.

If you wish to create a document reference with a specified name, use Google\Cloud\Firestore\CollectionReference::document().

This method does NOT insert the document until you call Google\Cloud\Firestore\DocumentReference::create().

Example:

$newUser = $collection->newDocument();
Returns
TypeDescription
Google\Cloud\Firestore\DocumentReference

add

Generate a new document reference, and insert it with the given field data.

This method will generate a random document name. If you wish to create a document with a specified name, create a reference with Google\Cloud\Firestore\CollectionReference::document(), then call Google\Cloud\Firestore\DocumentReference::create() to insert the document.

This method immediately inserts the document. If you wish for lazy creation of a Document instance, refer to Google\Cloud\Firestore\CollectionReference::document() or Google\Cloud\Firestore\CollectionReference::newDocument().

Example:

$newUser = $collection->add([
    'name' => 'Kate'
]);
Parameters
NameDescription
fields array

An array containing field names paired with their value. Accepts a nested array, or a simple array of field paths.

options array

Configuration Options.

Returns
TypeDescription
Google\Cloud\Firestore\DocumentReference

listDocuments

Parameters
NameDescription
options array

Configuration options

↳ pageSize int

The maximum number of results to return per request.

↳ resultLimit int

Limit the number of results returned in total. Defaults to 0 (return all results).

↳ pageToken string

A previously-returned page token used to resume the loading of results from a specific point.

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

parent

Get the parent document reference for a subcollection, or null if root.

Example:

$parent = $collection->parent();
Returns
TypeDescription
Google\Cloud\Firestore\DocumentReference|null