Skip to content

Commit

Permalink
added client-api\operations\counters\counter-batch.php.markdown and c…
Browse files Browse the repository at this point in the history
…lient-api\operations\counters\get-counters.php.markdown and a samples file for them both,

added the "get counters operation" page the constructor syntax & explanation,
added CountersFor synatx to the "create or modify counters" page
  • Loading branch information
reebhub committed Mar 7, 2025
1 parent bfc5a0b commit 8569b0b
Show file tree
Hide file tree
Showing 15 changed files with 1,166 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Counters Batch Operation

*CounterBatchOperation* allows you to operate on multiple counters (`Increment`, `Get`, `Delete`) of different documents in a **single request**.

{PANEL: Syntax}

{CODE:php counter_batch_op@ClientApi\Operations\Counters\Counters.php /}

| Parameter | | |
|------------------|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **counterBatch** | `CounterBatch` | An object that holds a list of `DocumentCountersOperation`.<br>Each element in the list describes the counter operations to perform for a specific document |

{CODE:php counter_batch@ClientApi\Operations\Counters\Counters.php /}

#### DocumentCountersOperation

{CODE:php document_counters_op@ClientApi\Operations\Counters\Counters.php /}

#### CounterOperation

{CODE:php counter_operation@ClientApi\Operations\Counters\Counters.php /}

#### CounterOperationType

{CODE:php counter_operation_type@ClientApi\Operations\Counters\Counters.php /}

{INFO: Document updates as a result of a counter operation }
A document that has counters holds all its counter names in the `metadata`.
Therefore, when creating a new counter, the parent document is modified, as the counter's name needs to be added to the metadata.
Deleting a counter also modifies the parent document, as the counter's name needs to be removed from the metadata.
Incrementing an existing counter will not modify the parent document.

Even if a `DocumentCountersOperation` contains several `CounterOperation` items that affect the document's metadata (create, delete),
the parent document will be modified **only once**, after all the `CounterOperation` items in this `DocumentCountersOperation` have been processed.
If `DocumentCountersOperation` doesn't contain any `CounterOperation` that affects the metadata, the parent document won't be modified.

{INFO/}

{PANEL/}

{PANEL: Return Value}

* *CounterBatchOperation* returns a `CountersDetail` object, which holds a list of `CounterDetail` objects.

* If a `CounterOperationType` is `Increment` or `Get`, a `CounterDetail` object will be added to the result.
`Delete` operations will not be included in the result.

{CODE:php counters_detail@ClientApi\Operations\Counters\Counters.php /}

{CODE:php counter_detail@ClientApi\Operations\Counters\Counters.php /}

{PANEL/}

{PANEL: Examples}

Assume we have two documents, `users/1` and `users/2`, that hold 3 counters each:
`likes`, `dislikes` and `downloads` - with values 10, 20 and 30 (respectively)

---

### Example #1 : Increment Multiple Counters in a Batch

{CODE:php counter_batch_exmpl1@ClientApi\Operations\Counters\Counters.php /}

#### Result:
{CODE-BLOCK:json}
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 15,
"CounterValues" : null
},
{
"DocumentId" : "users/1",
"CounterName" : "dislikes",
"TotalValue" : 20,
"CounterValues" : null
},
{
"DocumentId" : "users/2",
"CounterName" : "likes",
"TotalValue" : 110,
"CounterValues" : null
},
{
"DocumentId" : "users/2",
"CounterName" : "score",
"TotalValue" : 50,
"CounterValues" : null
}
]
}
{CODE-BLOCK/}

---

### Example #2 : Get Multiple Counters in a Batch

{CODE:php counter_batch_exmpl2@ClientApi\Operations\Counters\Counters.php /}

#### Result:

{CODE-BLOCK:json}
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 15,
"CounterValues" : null
},
{
"DocumentId" : "users/1",
"CounterName" : "downloads",
"TotalValue" : 30,
"CounterValues" : null
},
{
"DocumentId" : "users/2",
"CounterName" : "likes",
"TotalValue" : 110,
"CounterValues" : null
},
{
"DocumentId" : "users/2",
"CounterName" : "score",
"TotalValue" : 50,
"CounterValues" : null
}
]
}
{CODE-BLOCK/}

---

### Example #3 : Delete Multiple Counters in a Batch

{CODE:php counter_batch_exmpl3@ClientApi\Operations\Counters\Counters.php /}

#### Result:

{CODE-BLOCK:json}
{
"Counters": []
}
{CODE-BLOCK/}

---

### Example #4 : Mix Different Types of CounterOperations in a Batch

{CODE:php counter_batch_exmpl4@ClientApi\Operations\Counters\Counters.php /}

#### Result:

* Note: The `Delete` operations are Not included in the results.

{CODE-BLOCK:json}
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 30,
"CounterValues" : null
},
null,
{
"DocumentId" : "users/2",
"CounterName" : "likes",
"TotalValue" : 110,
"CounterValues" : null
}
]
}
{CODE-BLOCK/}

{PANEL/}

## Related Articles

### Operations

- [What are Operations](../../../client-api/operations/what-are-operations)
- [Get Counters Operation](../../../client-api/operations/counters/get-counters)


Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It can be used to get the value of a single counter, multiple counters' values,

{INFO: }

__Return Full Results flag:__
**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
Expand Down Expand Up @@ -63,8 +63,8 @@ The operation returns a `CountersDetail` object, which holds a list of `CounterD

{PANEL: Examples}

Assume we have a document "users/1" that holds 3 counters -
_"likes"_, _"dislikes"_ and _"downloads"_ - with values 10, 20 and 30 (respectively)
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It can be used to get the value of a single counter, multiple counters' values,

{INFO: }

__Return Full Results flag__:
**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
Expand Down Expand Up @@ -63,8 +63,8 @@ The operation returns a `CountersDetail` object, which holds a list of `CounterD

{PANEL: Examples}

Assume we have a document "users/1" that holds 3 counters -
_"likes"_, _"dislikes"_ and _"downloads"_ - with values 10, 20 and 30 (respectively)
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ It can be used to get the value of a single counter, multiple counters' values,

| Parameter | Type | Description |
|-----------------------|----------|-----------------------------------------------------------------------------------------------------------------------|
| __docId__ | string | The ID of the document that holds the counters |
| __counter__ | string | The name of the counter to get |
| __counters__ | string[] | The list of counter names to get |
| __returnFullResults__ | boolean | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |
| **docId** | string | The ID of the document that holds the counters |
| **counter** | string | The name of the counter to get |
| **counters** | string[] | The list of counter names to get |
| **returnFullResults** | boolean | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |

{INFO: }

__The full results flag:__
**The full results flag:**

If RavenDB is running in a distributed cluster, and the database resides on several nodes,
then a counter can have a different *local* value on each database node.
Expand All @@ -43,8 +43,8 @@ The operation returns a `CountersDetail` object, which holds a list of `CounterD

{PANEL: Examples}

Assume we have a document `users/1` that holds 3 counters -
_"Likes"_, _"Dislikes"_ and _"Downloads"_ - with values 10, 20 and 30 (respectively)
Assume we have a `users/1` document that holds 3 counters:
`Likes`, `Dislikes` and `Downloads` - with values 10, 20 and 30 (respectively)

---

Expand Down
Loading

0 comments on commit 8569b0b

Please sign in to comment.