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 fromsession.load
.
- Either pass
- Call
countersFor.get
to retrieve the current value of a single Counter.
Get example:
// 1. Open a session
try (IDocumentSession session = docStore.openSession()) {
// 2. pass CountersFor's constructor a document ID
ISessionDocumentCounters documentCounters = session.countersFor("products/1-C");
// 3. Use `countersFor.get` to retrieve a Counter's value
Long daysLeft = documentCounters.get("daysLeftForSale");
System.out.println("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
try (IDocumentSession session = docStore.openSession()) {
// 2. pass countersFor's constructor a document ID
ISessionDocumentCounters documentCounters = session.countersFor("products/1-C");
// 3. Use GetAll to retrieve all of the document's Counters' names and values.
Map<String, Long> counters = documentCounters.getAll();
// list counters' names and values
for (Map.Entry<String, Long> kvp : counters.entrySet()) {
System.out.println("counter name: " + kvp.getKey() + ", counter value: " + kvp.getValue());
}
}
GetAll syntax:
Map<String, Long> getAll();
Return Type | Description |
---|---|
Map<String, Long> | Map of Counter names and values |