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
- 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.
// 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 all Counters of a document
- 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.
// 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);
}
}