---
title: "Create or Modify Counters"
description: "Create new counters or increment existing counter values on RavenDB documents using the session API in C#, Java, Python, PHP, and Node.js."
url: "https://docs.ravendb.net/7.2/document-extensions/counters/create-or-modify"
version: "7.2"
language: "python"
---

# Create or Modify Counters

> Client language: Python. Also available for [C#](https://docs.ravendb.net/7.2/document-extensions/counters/create-or-modify.md), [Java](https://docs.ravendb.net/7.2/document-extensions/counters/create-or-modify.java.md), [PHP](https://docs.ravendb.net/7.2/document-extensions/counters/create-or-modify.php.md), [Node.js](https://docs.ravendb.net/7.2/document-extensions/counters/create-or-modify.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."

> **Note**
>
> - Use the `counters_for .increment` method to **create** a new Counter or **modify** an existing Counter's value.
>
> - If the Counter exists, `increment` will add the specified number to the Counter's current value.  
>   If the Counter doesn't exist, `increment` will create it and set its initial value.
>
> - For all other `counters_for ` methods see this [Overview](https://docs.ravendb.net/7.2/document-extensions/counters/overview.python.md#counter-methods-and-the--object).
>
> - In this page:
>   - [`increment` usage](#increment-usage)
>   - [Example](#example)
>   - [Syntax](#syntax)

## `increment` usage

**Flow**:

- Open a session.
- Create an instance of `counters_for `.
  - Either pass `counters_for ` an explicit document ID, -or-
  - Pass it an [entity tracked by the session](https://docs.ravendb.net/7.2/client-api/session/loading-entities.python.md),
    e.g. a document object returned from [session.Query](https://docs.ravendb.net/7.2/querying/overview.python.md) or from [session.Load](https://docs.ravendb.net/7.2/client-api/session/loading-entities.python.md#load).
- Call `counters_for .increment`.
- Call `session.save_changes` for the changes to take effect.

**Note**:

- Modifying a Counter using `increment` only takes effect when `session.aave_changes()` is executed.
- To **decrease** a Counter's value, pass the method a negative number to the `increment` method.

## Example

```python
# Open a session
with store.open_session() as session:
  # Pass CountersFor's constructor a document ID
  document_counters = session.counters_for("products/1-A")

  # Use 'CountersFor.increment'
  # ===========================

  # Increase "ProductLikes" by 1, or create it if doesn't exist with a value of 1
  document_counters.increment("ProductLikes")

  # Increase "ProductPageViews" by 15, or create it if doesn't exist with a value of 15
  document_counters.increment("ProductPageViews", 15)

  # Decrease "DaysLeftForSale" by 10, or create it if doesn't exist with a value of -10
  document_counters.increment("DaysLeftForSale", -10)

  # Execute all changes by calling save_changes
  session.save_changes()
```

## Syntax

```python
def increment(self, counter: str, delta: int = 1) -> None: ...
```

| Parameter | Type | Description |
| - | - | - |
| **counter\_name** | `str` | Counter's name |
| **delta** | `int` | Increase Counter by this value.<br />Default value is 1.<br />For a new Counter, this number will be its initial value. |

## See also

- [Studio Counters Management](https://docs.ravendb.net/7.2/studio/database/document-extensions/counters.md#counters)
- [Counters Overview](https://docs.ravendb.net/7.2/document-extensions/counters/overview.python.md)
- [Deleting a Counter](https://docs.ravendb.net/7.2/document-extensions/counters/delete.python.md)
- [Retrieving Counter Values](https://docs.ravendb.net/7.2/document-extensions/counters/retrieve-counter-values.python.md)
- [Counters and other features](https://docs.ravendb.net/7.2/document-extensions/counters/counters-and-other-features.python.md)
- [Counters In Clusters](https://docs.ravendb.net/7.2/document-extensions/counters/counters-in-clusters.md)
- [Counters Operations](https://docs.ravendb.net/7.2/client-api/operations/counters/get-counters.md#operations--counters--how-to-get-counters)
