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

countDocuments throws an error when matching 0 document

    • Minor Change
    • ALL
    • v4.2, v4.0
    • Hide

      ```

      use count

      db.count.insert([\{a: 1}, \{a: 2}])

      db.count.count({a:1}) // returns 1

      db.count.count({a:3}) // returns 0

      db.count.countDocuments({a:1}) // returns 1

      db.count.countDocuments({a:3}) // throws an exception

      ```

      Show
      ``` use count db.count.insert( [\{a: 1}, \{a: 2}] ) db.count.count({a:1}) // returns 1 db.count.count({a:3}) // returns 0 db.count.countDocuments({a:1}) // returns 1 db.count.countDocuments({a:3}) // throws an exception ```

      `countDocuments` throws an error when matching 0 document.

      I would have expected it to follow what `count` does, which is return 0.

      With both versions `4.0.3` and `4.1.4` (haven't checked intermediary ones), the error I get from the bellow `steps to reproduce` is:

      2018-10-20T16:26:44.252+0100 E QUERY [js] TypeError: res[0] is undefined :
       DBCollection.prototype.countDocuments@src/mongo/shell/collection.js:1418:5
       @(shell):1:1
       

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            x.guihot@gmail.com Xavier GUIHOT
            Votes:
            2 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: