---
title: "Get Counters Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/counters/get-counters"
version: "6.2"
language: "php"
---

# Get Counters Operation

> Client language: PHP. Also available for [C#](https://docs.ravendb.net/6.2/client-api/operations/counters/get-counters.md), [Java](https://docs.ravendb.net/6.2/client-api/operations/counters/get-counters.java.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/counters/get-counters.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

This operation is used to get counters' values for a specific document.  
It can be used to get the value of a single counter, multiple counters' values, or all counters' values.

## Syntax

#### `GetCountersOperation`

Use `GetCountersOperation` to get counters.  
Find usage examples below for getting a single counter, multiple counters, or all document counters.

```php
class GetCountersOperation {
  public function __construct(?string $docId, string|StringArray|array|null $counters = 
      null, bool $returnFullResults = false) { ... }
}
```

| Parameters | | |
| - | - | - |
| **docId** | string | The ID of the document that holds the counters |
| **counter** | `string` or `StringArray` or `array` or `null` | The name or names array of the counter/s to get, <br />or `null` for all document counters |
| **returnFullResults** | bool | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |

#### Get Single Counter

```php
$docId = "users/1";
$counter = "likes";
$returnFullResults = false;

$operation = new GetCountersOperation($docId, $counter, $returnFullResults);
```

| Parameters | | |
| - | - | - |
| **docId** | string | The ID of the document that holds the counters |
| **counter** | string | The name of the counter to get |
| **returnFullResults** | bool | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |

> **Info**
>
> **Return Full Results flag:**
>
> If RavenDB is running in a distributed cluster, and the database resides on several nodes,
> a counter can have a different *local* value on each database node, and the total counter value is the
> sum of all the local values of this counter from each node.  
> In order to get the counter values per database node, set the `returnFullResults` flag to `true`

#### Get Multiple Counters

```php
$docId = "users/1";
$counters = [ "likes", "score"];
$returnFullResults = false;

$operation = new GetCountersOperation($docId, $counters, $returnFullResults);
```

| Parameters | | |
| - | - | - |
| **docId** | string | The ID of the document that holds the counters |
| **counters** | `StringArray` or `array` | The names of the counters to get |
| **returnFullResults** | bool | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |

#### Get All Counters of a Document

```php
$docId = "users/1";
$returnFullResults = false;

$operation = new GetCountersOperation($docId, null, $returnFullResults);
```

| Parameters | | |
| - | - | - |
| **docId** | string | The ID of the document that holds the counters |
| **returnFullResults** | bool | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |

## Return Value

The operation returns a `CountersDetail` object, which holds a list of `CounterDetail` objects

```php
class CountersDetail
{
  private ?CounterDetailList $counters = null;
}
```

```php
class CounterDetail
{
  private ?string $documentId = null;     // ID of the document that holds the counter
  private ?string $counterName = null;    // The counter name
  private ?int $totalValue = null;        // Total counter value
  private ?int $etag = null;              // Counter Etag
  private ?array $counterValues = [];     // A dictionary of counter values per database node

  private ?string $changeVector = null;   // Change vector of the counter

  // ... getters and setters
}

class CounterDetailList extends TypedList
{
  public function __construct()
  {
      parent::__construct(CounterDetail::class);
      $this->setNullAllowed(true);
  }
}
```

## Examples

Assume we have a `users/1` document that holds 3 counters:  
`likes`, `dislikes` and `downloads` -  with values 10, 20 and 30 (respectively)

### Example #1 : Get single counter

```php
/** @var CountersDetail  $operationResult */
$operationResult = $store
  ->operations()
  ->send(new GetCountersOperation("users/1", "likes"));
```

#### Result:

```json
{
  "Counters": 
  [
  	{
  		"DocumentId" : "users/1",
  		"CounterName" : "likes",
  		"TotalValue" : 10,
  		"CounterValues" : null
  	}
  ]
}
```

### Example #2 : Get multiple counters

```php
/** @var CountersDetail $operationResult */
$operationResult = $store
  ->operations()
  ->send(new GetCountersOperation("users/1", [ "likes", "dislikes" ]));
```

#### Result:

```json
{
  "Counters": 
  [
  	{
  		"DocumentId" : "users/1",
  		"CounterName" : "likes",
  		"TotalValue" : 10,
  		"CounterValues" : null
  	},
      {
  		"DocumentId" : "users/1",
  		"CounterName" : "dislikes",
  		"TotalValue" : 20,
  		"CounterValues" : null
  	}
  ]
}
```

### Example #3 : Get all counters

```php
/** @var CountersDetail $operationResult */
$operationResult = $store->operations()
  ->send(new GetCountersOperation("users/1"));
```

#### Result:

```json
{
  "Counters": 
  [
  	{
  		"DocumentId" : "users/1",
  		"CounterName" : "likes",
  		"TotalValue" : 10,
  		"CounterValues" : null
  	},
      {
  		"DocumentId" : "users/1",
  		"CounterName" : "dislikes",
  		"TotalValue" : 20,
  		"CounterValues" : null
  	},
      {
  		"DocumentId" : "users/1",
  		"CounterName" : "downloads",
  		"TotalValue" : 30,
  		"CounterValues" : null
  	}
  ]
}
```

### Example #4 : Include full values in the result

```php
/** @var CountersDetail $operationResult */
$operationResult = $store
  ->operations()
  ->send(new GetCountersOperation("users/1", "likes", true));
```

#### Result:

Assuming a 3-node cluster, the distribution of the counter's value to nodes A, B, and C could be as follows:

```json
{
  "Counters": 
  [
  	{
  		"DocumentId" : "users/1",
  		"CounterName" : "likes",
  		"TotalValue" : 10,
  		"CounterValues" : 
          {
              "A:35-UuCp420vs0u+URADcGVURA" : 5,
              "B:83-SeCFU29daUOxfjUcAlLiJw" : 3,
              "C:27-7i7GP8bOOkGYLNflO/rSeg" : 2,
          }
  	}
  ]
}
```

## See also

- [What are Operations](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.php.md)
- [Counter Batch Operation](https://docs.ravendb.net/6.2/client-api/operations/counters/counter-batch.php.md)
