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

Count command with no predicate and limit of min long returns incorrect result

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.0.4
    • Component/s: Querying
    • Query
    • ALL

      In general, the count command treats a limit of -n as identical to a limit of n:

      > db.runCommand({count: "c"})
      { "n" : 11, "ok" : 1 }
      > db.runCommand({count: "c", limit: -10})
      { "n" : 10, "ok" : 1 }
      

      This is implemented by taking the absolute value of a long long. Taking the absolute value is incorrect for the smallest negative long long, since its absolute value is too large to be represented with 64 signed bits. Consequently, the count command returns an incorrect result when the limit is -2^63:

      > db.runCommand({count: "c", limit: NumberLong(-9223372036854775808)})
      { "n" : NumberLong("-9223372036854775808"), "ok" : 1 }
      

      This bug affects 4.0, and probably older branches (but I only confirmed on 4.0). It was fixed in 4.1 development by f0b39d91840 under SERVER-37446.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            david.storch@mongodb.com David Storch
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: