Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4206

Collection method countDocuments is order or magnitude slower that native query

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 4.2.3
    • Component/s: Internal
    • Environment:
      Azure CosmosDB (Mongo API 3.6)
      Java Sync Driver 4.2.3

      We are currently using Azure CosmosDB with Mongo API 3.6. We have a single collection, where all relevant properties are indexed. The collection currently has something about 28m records.

      Here is a count query result directly from Azure CosmosDB shell.

      Here is the same query run from Robo3T.

      Both results are instant. But when I try to run this via Java Driver, it ends up with exception after 60 seconds.

      `com.mongodb.MongoExecutionTimeoutException: Request timed out. Retries due to rate limiting: False.`

      Here's the part of the code responsible for calling the count query.


      public Long count(MongoClient client, String database, String collection, Document findQuery, Boolean stats)

      {      MongoDatabase db = client.getDatabase(database);      MongoCollection<TransactionEntity> coll = db.getCollection(collection,            TransactionEntity.class);     Long result = coll.countDocuments(findQuery);     printStats(db, stats);     return result; }


      I am unfortunately unable to pinpoint, what can cause this. From the various reads it looks like the internal implementation is using aggregate pipelines instead of count command, see CountStrategy.AGGREGATE and CountStrategy.COMMAND.

      My guess is that the underlying aggregation pipeline is using some unindexed field (although everything in this query is indexed) or not using indexes at all.

            ross@mongodb.com Ross Lawley
            mongodb@tomastokamrazek.cz Tomáš Toka Mrázek
            0 Vote for this issue
            2 Start watching this issue