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

Suggestion to improve CountDocuments performance

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.0.9
    • Component/s: None
    • None
    • Server Triage
    • ALL

      collection.countDocuments is converted in an aggregate with $math + $group by _id.
       

      db.getCollection("test").countDocuments({"type":"test","auditEnabled":{"$ne":true}})

       
      becomes:

      db.getCollection("test").aggregate(
          [
              {
                  "$match" : {
                      "type" : "test",
                      "auditEnabled" : {
                          "$ne" : true
                      }
                  }
              },
              {
                  "$group" : {
                      "_id" : NumberInt(1),
                      "n" : {
                          "$sum" : NumberInt(1)
                      }
                  }
              }
          ],
          {
              "allowDiskUse" : false
          }
      );

       

      Problem is that if documents in the collection are large, they are fully loaded in memory (apparently) to aggregate them.

      Why don't you add a $project by _id after $match? This will greatly improve (10x in our case) performance. Something like:

      db.getCollection("test").aggregate(
          [
              {
                  "$match" : {
                      "type" : "test",
                      "auditEnabled" : {
                          "$ne" : true
                      }
                  }
              },
              {
                  "$project" : 
                  {
                      "_id" : 1
                  }
              },
              {
                  "$group" : {
                      "_id" : NumberInt(1),
                      "n" : {
                          "$sum" : NumberInt(1)
                      }
                  }
              }
          ],
          {
              "allowDiskUse" : false
          }
      );

       

       

            Assignee:
            noopur.gupta@mongodb.com Noopur Gupta
            Reporter:
            ivan.fioravanti@4ward.it Ivan Fioravanti
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: