Get Counter Values



Get a single Counter's value

Get usage:

  • Open a session
  • Create an instance of CountersFor.
  • Call CountersFor.Get to retrieve the current value of a single Counter.

Get example:

// 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);
}

Get syntax:

long Get(string counterName);
Parameter Type Description
counterName string Counter's name
Return Type Description
long Counter's current value

Get all Counters of a document


GetAll usage:

  • Open a session.
  • Create an instance of CountersFor.
  • Call CountersFor.GetAll to retrieve the names and values of all counters associated with the document.

GetAll example:

// 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);
    }
}

GetAll syntax:

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