Operations: Counters: How to Get Counters

This operation is used to get counters values for a specific document.
Can be used to get a single counter value, multiple counters values or all counters values.

Syntax

Get Single Counter

public GetCountersOperation(string docId, string counter, bool returnFullResults = false)
Parameters
docId string The ID of the document that holds the counters
counter string The name of the counter to get
returnFullResults bool A flag which indicates if the operation should include a dictionary of counter values per database node in the result

Return Full Results flag

If RavenDB is running in a distributed cluster, and the database resides on several nodes, a counter can have a different local value on each database node, and the total counter value is the sum of all the local values of this counter from each node.
In order to get the counter values per database node, set the returnFullResults flag to true

Get Multiple Counters

public GetCountersOperation(string docId, string[] counters, bool returnFullResults = false)
Parameters
docId string The ID of the document that holds the counters
counters string[] The names of the counters to get
returnFullResults bool A flag which indicates if the operation should include a dictionary of counter values per database node in the result

Get All Counters of a Document

public GetCountersOperation(string docId, bool returnFullResults = false)
Parameters
docId string The ID of the document that holds the counters
returnFullResults bool A flag which indicates if the operation should include a dictionary of counter values per database node in the result

Return Value

The operation returns a CountersDetail object, which holds a list of CounterDetail objects

public class CountersDetail
{
    public List<CounterDetail> Counters;
}

public class CounterDetail
{
    public string DocumentId; // ID of the document that holds the counter
    public string CounterName; // The counter name
    public long TotalValue; // Total counter value
    public Dictionary<string, long> CounterValues; // A dictionary of counter values per database node
    public long Etag; // Counter Etag
    public string ChangeVector; // Change vector of the counter
}

Examples

Assume we have a document "users/1" which holds 3 counters -
"likes", "dislikes" and "downloads" - with values 10, 20 and 30 (respectively)

Example #1 : Get single counter

var operationResult = store.Operations
    .Send(new GetCountersOperation("users/1", "likes"));

Result:

{
	"Counters": 
    [
		{
			"DocumentId" : "users/1",
			"CounterName" : "likes",
			"TotalValue" : 10,
			"CounterValues" : null
		}
	]
}

Example #2 : Get multiple counters

var operationResult = store.Operations
    .Send(new GetCountersOperation("users/1", new []{"likes", "dislikes" }));

Result:

{
	"Counters": 
    [
		{
			"DocumentId" : "users/1",
			"CounterName" : "likes",
			"TotalValue" : 10,
			"CounterValues" : null
		},
        {
			"DocumentId" : "users/1",
			"CounterName" : "dislikes",
			"TotalValue" : 20,
			"CounterValues" : null
		}
	]
}

Example #3 : Get all counters

var operationResult = store.Operations
    .Send(new GetCountersOperation("users/1"));

Result:

{
	"Counters": 
    [
		{
			"DocumentId" : "users/1",
			"CounterName" : "likes",
			"TotalValue" : 10,
			"CounterValues" : null
		},
        {
			"DocumentId" : "users/1",
			"CounterName" : "dislikes",
			"TotalValue" : 20,
			"CounterValues" : null
		},
        {
			"DocumentId" : "users/1",
			"CounterName" : "downloads",
			"TotalValue" : 30,
			"CounterValues" : null
		}
	]
}

Example #4 : Include full values in the result

var operationResult = store.Operations
    .Send(new GetCountersOperation("users/1", "likes", true));

Result:

{
	"Counters": 
    [
		{
			"DocumentId" : "users/1",
			"CounterName" : "likes",
			"TotalValue" : 10,
			"CounterValues" : 
            {
                "A:35-UuCp420vs0u+URADcGVURA" : 5,
                "B:83-SeCFU29daUOxfjUcAlLiJw" : 3,
                "C:27-7i7GP8bOOkGYLNflO/rSeg" : 2,
            }
		}
	]
}