# Cloud Firestore Client - Class CollectionReference (2.2.0-RC1)

2.2.0-RC1 2.1.0 (latest) 2.0.2-RC1 1.55.0 1.54.4 1.53.1 1.52.0 1.51.0 1.50.0 1.48.1 1.47.3 1.46.0 1.45.2 1.44.1 1.43.3 1.40.0 1.39.0 1.38.0 1.37.7 1.36.1 1.35.0 1.34.0 1.33.0 1.32.0 1.31.0 1.30.0 1.28.0 1.27.3 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** ||
|---|---|
| **Name** | **Description** |
| `firestoreClient` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/V1.Client.FirestoreClient` A Connection to Cloud Firestore. This object is created by FirestoreClient, and should not be instantiated outside of this client. |
| `valueMapper` | `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:

- [CollectionReference::id()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__id__) Returns the last element.
- [CollectionReference::path()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__path__) Returns the path, relative to the database.

Example:

    $name = $collection->name();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `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
[CollectionReference::id()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__id__).

Example:

    $path = $collection->path();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `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
[CollectionReference::name()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__name__).

Example:

    $id = $collection->id();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### document

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

Example:

    $newUser = $collection->document('john');

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `documentId` | `string` The document ID. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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
[CollectionReference::document()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__document__).

This method does NOT insert the document until you call
[DocumentReference::create()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference#_Google_Cloud_Firestore_DocumentReference__create__).

Example:

    $newUser = $collection->newDocument();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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
[CollectionReference::document()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__document__), then call
[DocumentReference::create()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference#_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
[CollectionReference::document()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__document__) or
[CollectionReference::newDocument()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/CollectionReference#_Google_Cloud_Firestore_CollectionReference__newDocument__).

Example:

    $newUser = $collection->add([
        'name' => 'Kate'
    ]);

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `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** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference` |   |

### listDocuments

See also:

- [ListDocumentsRequest](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.ListDocumentsRequest)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `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** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-core/latest/Iterator.ItemIterator.html<https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference>` |   |

### parent

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

Example:

    $parent = $collection->parent();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference|null` |   |