Retrieving Counter Values
-
Use CountersFor.
Get
to retrieve the value of a single Counter,
or CountersFor.GetAll
to retrieve the values of all the Counters of a document. -
In this page:
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:
- 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
- Execute
CountersFor.Get
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:
- 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
- Execute
CountersFor.GetAll
.
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);
}
}