indexes: Indexing Counters


In order to index counter values, create an index that inherits from AbstractCountersIndexCreationTask. Next, choose one of these two methods which take the index expression:"map");

map only indexes the counters with the specified name.

Examples of indexes using each method:

export class MyCounterIndex  extends AbstractCountersIndexCreationTask {
    constructor() {
        super(); = `from counter in docs.counters select new {
                Likes = counter.Value,
                Name = counter.Name,
                User = counter.DocumentId


Creating an index inheriting from AbstractCsharpCountersIndexCreationTask allows you to write your map and reduce functions in JavaScript.
Learn more about JavaScript indexes here.

class MyCounterIndex extends AbstractRawJavaScriptCountersIndexCreationTask {
    public constructor() {

            "'Companies', 'HeartRate', function (counter) {\n" +
            "return {\n" +
            "    heartBeat: counter.Value,\n" +
            "    name: counter.Name,\n" +
            "    user: counter.DocumentId\n" +
            "};\n" +


While indexes inheriting from AbstractIndexCreationTask cannot index counter values, the counterNamesFor() method is available which returns the names of all counters for a specified document:

Example of index using CounterNamesFor:

class Companies_ByCounterNames extends AbstractCountersIndexCreationTask{
    constructor() {
        super(); = "docs.Companies.Select(e => new {\n"+
            "e = e,\n"+
        "    counterNames = this.CounterNamesFor(e)\n"+
        "}).Select(this0 => new {\n"+
        "    CounterNames = Enumerable.ToArray(this0.counterNames)\n"+

Querying the Index

const companies = session
    .query({index: "Companies_ByCounterNames"})
    .containsAny("counterNames", ["Likes"])