|
| 1 | +# Counters Batch Operation |
| 2 | + |
| 3 | +*CounterBatchOperation* allows you to operate on multiple counters (`Increment`, `Get`, `Delete`) of different documents in a **single request**. |
| 4 | + |
| 5 | +{PANEL: Syntax} |
| 6 | + |
| 7 | +{CODE:php counter_batch_op@ClientApi\Operations\Counters\Counters.php /} |
| 8 | + |
| 9 | +| Parameter | | | |
| 10 | +|------------------|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 11 | +| **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 | |
| 12 | + |
| 13 | +{CODE:php counter_batch@ClientApi\Operations\Counters\Counters.php /} |
| 14 | + |
| 15 | +#### DocumentCountersOperation |
| 16 | + |
| 17 | +{CODE:php document_counters_op@ClientApi\Operations\Counters\Counters.php /} |
| 18 | + |
| 19 | +#### CounterOperation |
| 20 | + |
| 21 | +{CODE:php counter_operation@ClientApi\Operations\Counters\Counters.php /} |
| 22 | + |
| 23 | +#### CounterOperationType |
| 24 | + |
| 25 | +{CODE:php counter_operation_type@ClientApi\Operations\Counters\Counters.php /} |
| 26 | + |
| 27 | +{INFO: Document updates as a result of a counter operation } |
| 28 | +A document that has counters holds all its counter names in the `metadata`. |
| 29 | +Therefore, when creating a new counter, the parent document is modified, as the counter's name needs to be added to the metadata. |
| 30 | +Deleting a counter also modifies the parent document, as the counter's name needs to be removed from the metadata. |
| 31 | +Incrementing an existing counter will not modify the parent document. |
| 32 | + |
| 33 | +Even if a `DocumentCountersOperation` contains several `CounterOperation` items that affect the document's metadata (create, delete), |
| 34 | +the parent document will be modified **only once**, after all the `CounterOperation` items in this `DocumentCountersOperation` have been processed. |
| 35 | +If `DocumentCountersOperation` doesn't contain any `CounterOperation` that affects the metadata, the parent document won't be modified. |
| 36 | + |
| 37 | +{INFO/} |
| 38 | + |
| 39 | +{PANEL/} |
| 40 | + |
| 41 | +{PANEL: Return Value} |
| 42 | + |
| 43 | +* *CounterBatchOperation* returns a `CountersDetail` object, which holds a list of `CounterDetail` objects. |
| 44 | + |
| 45 | +* If a `CounterOperationType` is `Increment` or `Get`, a `CounterDetail` object will be added to the result. |
| 46 | + `Delete` operations will not be included in the result. |
| 47 | + |
| 48 | +{CODE:php counters_detail@ClientApi\Operations\Counters\Counters.php /} |
| 49 | + |
| 50 | +{CODE:php counter_detail@ClientApi\Operations\Counters\Counters.php /} |
| 51 | + |
| 52 | +{PANEL/} |
| 53 | + |
| 54 | +{PANEL: Examples} |
| 55 | + |
| 56 | +Assume we have two documents, `users/1` and `users/2`, that hold 3 counters each: |
| 57 | +`likes`, `dislikes` and `downloads` - with values 10, 20 and 30 (respectively) |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +### Example #1 : Increment Multiple Counters in a Batch |
| 62 | + |
| 63 | +{CODE:php counter_batch_exmpl1@ClientApi\Operations\Counters\Counters.php /} |
| 64 | + |
| 65 | +#### Result: |
| 66 | +{CODE-BLOCK:json} |
| 67 | +{ |
| 68 | + "Counters": |
| 69 | + [ |
| 70 | + { |
| 71 | + "DocumentId" : "users/1", |
| 72 | + "CounterName" : "likes", |
| 73 | + "TotalValue" : 15, |
| 74 | + "CounterValues" : null |
| 75 | + }, |
| 76 | + { |
| 77 | + "DocumentId" : "users/1", |
| 78 | + "CounterName" : "dislikes", |
| 79 | + "TotalValue" : 20, |
| 80 | + "CounterValues" : null |
| 81 | + }, |
| 82 | + { |
| 83 | + "DocumentId" : "users/2", |
| 84 | + "CounterName" : "likes", |
| 85 | + "TotalValue" : 110, |
| 86 | + "CounterValues" : null |
| 87 | + }, |
| 88 | + { |
| 89 | + "DocumentId" : "users/2", |
| 90 | + "CounterName" : "score", |
| 91 | + "TotalValue" : 50, |
| 92 | + "CounterValues" : null |
| 93 | + } |
| 94 | + ] |
| 95 | +} |
| 96 | +{CODE-BLOCK/} |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### Example #2 : Get Multiple Counters in a Batch |
| 101 | + |
| 102 | +{CODE:php counter_batch_exmpl2@ClientApi\Operations\Counters\Counters.php /} |
| 103 | + |
| 104 | +#### Result: |
| 105 | + |
| 106 | +{CODE-BLOCK:json} |
| 107 | +{ |
| 108 | + "Counters": |
| 109 | + [ |
| 110 | + { |
| 111 | + "DocumentId" : "users/1", |
| 112 | + "CounterName" : "likes", |
| 113 | + "TotalValue" : 15, |
| 114 | + "CounterValues" : null |
| 115 | + }, |
| 116 | + { |
| 117 | + "DocumentId" : "users/1", |
| 118 | + "CounterName" : "downloads", |
| 119 | + "TotalValue" : 30, |
| 120 | + "CounterValues" : null |
| 121 | + }, |
| 122 | + { |
| 123 | + "DocumentId" : "users/2", |
| 124 | + "CounterName" : "likes", |
| 125 | + "TotalValue" : 110, |
| 126 | + "CounterValues" : null |
| 127 | + }, |
| 128 | + { |
| 129 | + "DocumentId" : "users/2", |
| 130 | + "CounterName" : "score", |
| 131 | + "TotalValue" : 50, |
| 132 | + "CounterValues" : null |
| 133 | + } |
| 134 | + ] |
| 135 | +} |
| 136 | +{CODE-BLOCK/} |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +### Example #3 : Delete Multiple Counters in a Batch |
| 141 | + |
| 142 | +{CODE:php counter_batch_exmpl3@ClientApi\Operations\Counters\Counters.php /} |
| 143 | + |
| 144 | +#### Result: |
| 145 | + |
| 146 | +{CODE-BLOCK:json} |
| 147 | +{ |
| 148 | + "Counters": [] |
| 149 | +} |
| 150 | +{CODE-BLOCK/} |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +### Example #4 : Mix Different Types of CounterOperations in a Batch |
| 155 | + |
| 156 | +{CODE:php counter_batch_exmpl4@ClientApi\Operations\Counters\Counters.php /} |
| 157 | + |
| 158 | +#### Result: |
| 159 | + |
| 160 | +* Note: The `Delete` operations are Not included in the results. |
| 161 | + |
| 162 | +{CODE-BLOCK:json} |
| 163 | +{ |
| 164 | + "Counters": |
| 165 | + [ |
| 166 | + { |
| 167 | + "DocumentId" : "users/1", |
| 168 | + "CounterName" : "likes", |
| 169 | + "TotalValue" : 30, |
| 170 | + "CounterValues" : null |
| 171 | + }, |
| 172 | + null, |
| 173 | + { |
| 174 | + "DocumentId" : "users/2", |
| 175 | + "CounterName" : "likes", |
| 176 | + "TotalValue" : 110, |
| 177 | + "CounterValues" : null |
| 178 | + } |
| 179 | + ] |
| 180 | +} |
| 181 | +{CODE-BLOCK/} |
| 182 | + |
| 183 | +{PANEL/} |
| 184 | + |
| 185 | +## Related Articles |
| 186 | + |
| 187 | +### Operations |
| 188 | + |
| 189 | +- [What are Operations](../../../client-api/operations/what-are-operations) |
| 190 | +- [Get Counters Operation](../../../client-api/operations/counters/get-counters) |
| 191 | + |
| 192 | + |
0 commit comments