Counters And Other Features

Counters and Indexing

Indexing Counters can speed-up finding them and the documents that contain them.

  • Indexing Counter Values Dynamic indexes (aka auto-indexes) cannot index counter values. To index counter values, create a static index that inherits from AbstractCountersIndexCreationTask.

  • Indexing Counter Names
    Re-indexing due to Counter-name modification is normally rare enough to pause no performance issues.
    To index a document's Counters by name, use counterNamesFor.

Counters and Queries

Create queries using code, or send the server raw queries for execution.

  • Either way, you can query Counters by name but not by value.
    This is because queries are generally based on indexes, and Counter values are not indexed.
  • Counter values can be projected from query results, as demonstrated in the following examples.
    This way a client can get Counter values from a query without downloading whole documents.

  • Use Session.Query to code queries yourself.

    • Returned Counter Value: Accumulated
      A Counter's value is returned as a single sum, with no specification of the Counter's value on each node.
      using (var session = docStore.OpenSession())
          //Select documents from the "Products" collection, with a Counter named `ProductLikes`.  
          //Querying upon Counter values (e.g. using "Where") is not possible.  
          //In this example the documents that contain the Counter are NOT returned, only Counter values.
          var query = session.Query<Product>()
          .Select(product => RavenQuery.Counter(product, "ProductLikes"));
          var queryResults = query.ToList();
          //Show ProductLikes's value, or NULL for documents with no such Counter.  
          foreach (var counterValue in queryResults)
              Console.WriteLine("ProductLikes: " + counterValue);
  • Use rawQuery to send the server raw RQL expressions for execution.

    • You can use the counter method.
      Returned Counter Value: Accumulated

    • You can use the counterRaw method.
      Returned Counter Value: Distributed
      A Counter's value is returned as a series of values, each maintained by one of the nodes.

      • It is not expected of you to use this in your application.
        Applications normally use the Counter's overall value, and very rarely refer to the value each node gives it.

    counter and counterRaw samples:

    //Various RQL expressions sent to the server using counter()
    //Returned Counter value is accumulated
    List<CounterResult> rawQuery1 = session
        .rawQuery(CounterResult.class, "from products as p select counter(p, \"productLikes\")")
    List<CounterResult> rawQuery2 = session.advanced().rawQuery(CounterResult.class,
        "from products select counter(\"productLikes\") as productLikesCount")
    List<CounterResult> rawQuery3 = session.advanced()
            "from products where PricePerUnit > 50 select Name, counter(\"productLikes\")")
    //An RQL expression sent to the server using counterRaw()
    //Returned Counter value is distributed
    List<CounterResultRaw> query = session
            "from users as u select counterRaw(u, \"downloads\")")

Counters and Revisions

A document revision stores all the document Counters' names and values when the revision was created.

  • Stored Counter Values: Accumulated
    A revision stores a Counter's value as a single sum, with no specification of the Counter's value on each node.

  • Revisions-creation can be initiated by Counter-name modification.

    • When the Revisions feature is enabled, the creation or deletion of a Counter initiates the creation of a new document revision.
    • Counter value modifications do not cause the creation of new revisions.

Counters and Smuggler

Smuggler is a DocumentStore property, that can be used to export chosen database items to an external file or to import database items from an existing file into the database.

  • Transferred Counter Value: Distributed
    Smuggler transfers the entire series of values that the different nodes maintain for a Counter.
  • To make Smuggler handle Counters, include DatabaseItemType.COUNTERS in OperateOnTypes's list of entities to import or export.

Counters and Changes API

Changes API is a Push service, that can inform you of various changes on the Server, including changes in Counter values.
You can target all Counters, or specify the ones you wish to follow.

  • Pushed Counter Value: Accumulated
    Changes API methods return a Counter's value as a single sum, without specifying its value on each node.
  • The service is initiated by Counter Value Modification.

Counters and Ongoing Tasks:

Each ongoing task relates to Counters in its own way.

  • Counters and the Backup task
    There are two backup types: logical-backup and snapshot.
    Both types store and restore all data, including Counters.
    Both types operate as an ongoing backup routine, with a pre-set time schedule.

    • Logical Backup:
      Backed-up Counter values: Distributed
      A logical backup is a higher-level implementation of Smuggler.
      As with Smuggler, Counters are backed-up and restored including their values on all nodes.
    • Snapshot:
      A snapshot stores all data and settings as a single binary image. All components, including Counters, are restored to the exact same state they've been at during backup.
  • Counters and the External Replication task
    The ongoing external replication task replicates all data, including Counters.

    • Replicated Counter Value: Distributed
      Counters are replicated along with their values on all nodes.
    • Replication can be initiated by both Counter-name update and Counter-value modification.
  • Counters and the ETL task
    ETL is used to export data from RavenDB to an external (either Raven or SQL) database.

    • SQL ETL is not supported.
      Counters cannot be exported to an SQL database over SQL ETL.
    • RavenDB ETL is supported.
      Counters are exported over RavenDB ETL.
      • Export can be initiated by both Counter-name update and Counter-value modification.
      • Exported Counter Value: Distributed
        Counters are exported along with their values on all nodes.
      • Counters can be exported using a script.
        Default behavior: When an ETL script is not provided, Counters are exported.
  • Counters and the Data Subscriptions task
    Data Subscriptions can be initiated by document changes, including those caused by Counter Name updates.
    Documents will not be delivered in reaction to Counter Value modification.

Counters and Other Features: Summary

Use this table to find if and how various RavenDB features are triggered by Counters, and how the various features handle Counter values.

  • In the Triggered By column:
    • Document Change - Feature is triggered by a Counter Name update.
    • Countrer Value Modification - Feature is triggered by a Counter Value modification.
    • Time Schedule - Feature is invoked by a pre-set time routine.
    • No Trigger - Feature is executed manually, through the Studio or by a Client.
  • In the Counter Value column:
    • Accumulated - Counter Value is handled as a single accumulated sum.
    • Distributed - Counter Value is handled as a series of values maintained by cluster nodes.
Feature Triggered by Counter Value
Indexing Document Change doesn't handle values
Raw Queries No trigger counter() - Accumulated
counterRaw() - Distributed
Smuggler No trigger Distributed
Backup Task Time Schedule Distributed
RavenDB ETL Task Document Change,
Countrer Value Modification
External Replication task Document Change,
Countrer Value Modification
Data Subscriptions Update Task Document Change
Changes API Countrer Value Modification Accumulated
Revision creation Document Change Accumulated

Including Counters

You can include Counters while loading a document.
An included Counter is retrieved in the same request as its owner-document and is held by the session, so it can be immediately retrieved when needed with no additional remote calls.

  • Including Counters when using Session.load:

    • Include a single Counter using includeCounter.
    • Include multiple Counters using includeCounters.

    includeCounter and includeCounters usage samples:

    //include single Counters
    Product productPage = session
        .load(Product.class, "products/1-C", includeBuilder -> {
    //include multiple Counters
    //note that you can combine the inclusion of Counters and documents.
    Product productPage = session.load(Product.class, "orders/1-A", includeBuilder -> {
            .includeCounters(new String[]{"productLikes", "productDislikes"});
  • Including Counters when using Session.query:

    • Include a single Counter using includeCounter.
    • Include multiple Counters using includeCounters.

    includeCounter and includeCounters usage samples:

    //include a single Counter
    IDocumentQuery<Product> query = session.query(Product.class)
        .include(includeBuilder -> {
    //include multiple Counters
    IDocumentQuery<Product> query = session.query(Product.class)
        .include(includeBuilder -> {
            includeBuilder.includeCounters(new String[]{"productLikes", "productDownloads"});

Counters Bulk-Insert

store.bulkInsert is RavenDB's high-performance data insertion operation.
Use its countersFor interface's increment method to add or update counters with great speed.

  • Syntax

    • countersFor

      public CountersBulkInsert countersFor(String id);
      Parameters Type Description
      id String Document ID
    • increment

      void increment(String counterName);
      void increment(String id, String name, long delta);
      Parameters Type Description
      name String Counter Name
      id String Document ID
      delta long Default: 1L
  • Usage Flow

    • Create a store.bulkInsert instance.
    • Pass the instance's countersFor interface, the document ID
    • Call increment as many times as you like. Pass it -
      The Counter Name and Value (delta to be added).
  • Usage Sample
    In this sample, we attach a counter to all User documents.

    try (IDocumentSession session = docStore.openSession()) {
        IDocumentQuery<User> query = session.query(User.class)
            .whereLessThan("age", 30);
        result = query.toList();
    try (BulkInsertOperation bulkInsert = docStore.bulkInsert()) {
        for (User user : result) {
            String userId = user.getId();
            CountersBulkInsert countersFor = bulkInsert.countersFor(userId);
            bulkInsert.countersFor(userId).increment("downloaded", 100);