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
using (var session = docStore.OpenSession())
{
    // 2. pass CountersFor's constructor a document ID  
    var documentCounters = session.CountersFor("products/1-C");

    // 3. Use `CountersFor.Get` to retrieve a Counter's value
    var DaysLeft = documentCounters.Get("DaysLeftForSale");
    Console.WriteLine("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.

Dictionary<string, long?> GetAll();
Return Type Description
Dictionary<string, long> Map of Counter names and values

GetAll Usage Flow:

GetAll Code Sample:

// 1. Open a session
using (var session = docStore.OpenSession())
{
    // 2. pass CountersFor's constructor a document ID  
    var documentCounters = session.CountersFor("products/1-C");

    // 3. Use GetAll to retrieve all of the document's Counters' names and values.
    var counters = documentCounters.GetAll();

    // list counters' names and values
    foreach (var counter in counters)
    {
        Console.WriteLine("counter name: " + counter.Key + ", counter value: " + counter.Value);
    }
}