You are currently browsing legacy 4.1 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Retrieving Counter Values



Get Method - Retrieve a single Counter's value

Get Syntax:

  • Use get to retrieve the current value of a single Counter.

long get(String counterName);

Parameters Type Description
counterName String Counter's name
Return Type Description
long Counter's current value

Get Usage Flow:

Get Code Sample:

// 1. Open a session
try (IDocumentSession session = docStore.openSession()) {
    // 2. pass CountersFor's constructor a document ID
    ISessionDocumentCounters documentCounters = session.countersFor("products/1-C");

    // 3. Use `countersFor.get` to retrieve a Counter's value
    Long daysLeft = documentCounters.get("daysLeftForSale");
    System.out.println("Days Left For Sale: " + daysLeft);
}


getAll Method - Retrieve ALL Counters of a document

getAll Syntax:

  • Use getAll to retrieve all names and values of a document's Counters.

Map<String, Long> getAll();

Return Type Description
Map<String, Long> Map of Counter names and values

getAll Usage Flow:

getAll Code Sample:

// 1. Open a session
try (IDocumentSession session = docStore.openSession()) {
    // 2. pass countersFor's constructor a document ID
    ISessionDocumentCounters documentCounters = session.countersFor("products/1-C");

    // 3. Use GetAll to retrieve all of the document's Counters' names and values.
    Map<String, Long> counters = documentCounters.getAll();

    // list counters' names and values
    for (Map.Entry<String, Long> kvp : counters.entrySet()) {
        System.out.println("counter name: " + kvp.getKey() + ", counter value: " + kvp.getValue());
    }
}