Get Counter Values
-
Use
CountersFor.Get
to retrieve the value of a single Counter,
orCountersFor.GetAll
to retrieve the names and values of all Counters associated with a document. -
For all other
CountersFor
methods see this Overview. -
In this page:
Get a single Counter's value
Get usage:
- Open a session
-
Create an instance of
CountersFor
.- Either pass
CountersFor
an explicit document ID, -or- - Pass it an entity tracked by the session, e.g. a document object returned from session.Query or from session.Load.
- Either pass
- 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
.- Either pass
CountersFor
an explicit document ID, -or- - Pass it an entity tracked by the session, e.g. a document object returned from session.Query or from session.Load.
- Either pass
- 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 |