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 fromsession.load
.
- Either pass
- Execute
countersFor.get
Get Code Sample:
// 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);
}
getAll Method - Retrieve ALL Counters of a document
getAll Syntax:
- Use
getAll
to retrieve all names and values of a document's Counters.
Map<String, Long> getAll();
Return Type | Description |
---|---|
Map<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
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());
}
}