Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-41569

Make countDocuments() explainable

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.3
    • Component/s: Querying
    • Server Tooling & Methods

      At this time, unlike count(), it appears that an explain() cannot be run on the countDocuments().

      For example, I can successfully run an explain() on count(), as seen here:

      PRIMARY> db.users.count({ "name" : "Robb Stark" })PRIMARY> db.users.count({ "name" : "Robb Stark" })
      1
      
      PRIMARY> db.users.explain().count({ "name" : "Robb Stark" })
      
      { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "sample_mflix.users", "indexFilterSet" : false, "parsedQuery" : { "name" : { "$eq" : "Robb Stark" } }, "queryHash" : "01AEE5EC", "planCacheKey" : "01AEE5EC", "winningPlan" : { "stage" : "COUNT", "inputStage" : { "stage" : "COLLSCAN", "filter" : { "name" : { "$eq" : "Robb Stark" } }, "direction" : "forward" } }, "rejectedPlans" : [ ] }...

       
      However, if I try to run explain() on countDocuments(), it throws an error like this:

      PRIMARY> db.users.countDocuments({ "name" : "Robb Stark" })
      1
      PRIMARY> db.users.explain().countDocuments({ "name" : "Robb Stark" })
      2019-06-06T12:30:58.431-0700 E QUERY [js] TypeError: db.users.explain(...).countDocuments is not a function :
      @(shell):1:1
      

      Now, I know that countDocuments() wraps the following aggregation operation and returns just the value of n:

      db.collection.aggregate([
       { $match: <query> },
       { $group: { _id: null, n: { $sum: 1 } } }
      ])

      Therefore, I tried to run explain() on the equivalent aggregation pipeline, which correctly yielded results.

      PRIMARY> db.users.aggregate([ { $match: { "name" : "Robb Stark" } }, { $group: { _id: null, n: { $sum: 1 } } }])PRIMARY> db.users.aggregate([ { $match: { "name" : "Robb Stark" } }, { $group: { _id: null, n: { $sum: 1 } } }])
      { "_id" : null, "n" : 1 }
      
      PRIMARY> db.users.explain().aggregate([ { $match: { "name" : "Robb Stark" } }, { $group: { _id: null, n: { $sum: 1 } } }])
      { "stages" : [ { "$cursor" : { "query" : { "name" : "Robb Stark" }, "queryPlanner" : { "plannerVersion" : 1, "namespace" : "sample_mflix.users", "indexFilterSet" : false, "parsedQuery" : { "name" : { "$eq" : "Robb Stark" } }, "queryHash" : "01AEE5EC", "planCacheKey" : "01AEE5EC", "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "name" : { "$eq" : "Robb Stark" } }, "direction" : "forward" }, "rejectedPlans" : [ ] } } }, { "$group" : { "_id" : { "$const" : null }, "n" : { "$sum" : { "$const" : 1 } } } } ]
      

       
      So, it appears that there is something specific to countDocuments() that prevents explain() from being executed on it.

            Assignee:
            backlog-server-stm Backlog - Server Tooling and Methods (STM) (Inactive)
            Reporter:
            harshad.dhavale@mongodb.com Harshad Dhavale
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: