# Cloud Firestore Client - Class DocumentSnapshot (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 DocumentSnapshot.

Represents the data of a document at the time of retrieval.

A snapshot is immutable and may point to a non-existing document.

Fields may be read in array-style syntax. Note that writing using array-style
syntax is NOT supported and will result in a `\BadMethodCallException`.

Example:

    use Google\Cloud\Firestore\FirestoreClient;

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

    // Fields are exposed via array-style accessors:
    $bitcoinWalletValue = $snapshot['wallet']['cryptoCurrency']['bitcoin'];

## Namespace

Google \\ Cloud \\ Firestore

## Methods

### __construct

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `reference` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference` The document which created the snapshot. |
| `valueMapper` | `ValueMapper` A Firestore Value Mapper. |
| `info` | `array` Document information, such as create and update timestamps. |
| `data` | `array` Document field data. |
| `exists` | `bool` Whether the document exists in the Firestore database. |

### reference

Get the reference of the document which created the snapshot.

Example:

    $reference = $snapshot->reference();

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

### 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:

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

Example:

    $name = $snapshot->name();

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

Example:

    $path = $snapshot->path();

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

### id

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

IDs are the path element which identifies a resource. To retrieve the
full path to a resource (the resource name), use
[DocumentSnapshot::name()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentSnapshot#_Google_Cloud_Firestore_DocumentSnapshot__name__).

Example:

    $id = $snapshot->id();

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

### updateTime

Get the Document Update Timestamp.

Example:

    $updateTime = $snapshot->updateTime();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-core/latest/Timestamp.html|null` |   |

### readTime

Get the Document Read Timestamp.

Example:

    $readTime = $snapshot->readTime();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-core/latest/Timestamp.html|null` |   |

### createTime

Get the Document Create Timestamp.

Example:

    $createTime = $snapshot->createTime();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-core/latest/Timestamp.html|null` |   |

### data

Returns document data as an array, or null if the document does not exist.

Example:

    $data = $snapshot->data();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array|null` |   |

### exists

Returns true if the document exists in the database.

Example:

    if ($snapshot->exists()) {
        echo "The document exists!";
    }

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `bool` |   |

### get

Get a field by field path.

A field path is a string containing the path to a specific field, at the
top level or nested, delimited by `.`. For instance, the value `hello` in
the structured field `{ "foo" : { "bar" : "hello" }}` would be accessible
using a field path of `foo.bar`.

Example:

    $value = $snapshot->get('wallet.cryptoCurrency.bitcoin');

    // Field names containing dots or symbols can be targeted using a FieldPath instance:
    use Google\Cloud\Firestore\FieldPath;

    $value = $snapshot->get(new FieldPath(['wallet', 'cryptoCurrency', 'my.coin']));

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `fieldPath` | `string|https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/FieldPath` The field path to return. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `mixed` |   |

### offsetSet

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `offset` | `mixed` |
| `value` | `mixed` |

### offsetExists

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `offset` | `mixed` |

### offsetUnset

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `offset` | `mixed` |

### offsetGet

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `offset` | `mixed` |