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

Sharded collection counts (on primary) can report too many results

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: planned but not scheduled
    • Component/s: Sharding
    • Labels:
      None
    • Operating System:
      ALL

      Description

      Summary

      Count does not filter out unowned (orphaned) documents and can therefore report larger values than one will find via a normal query, or using itcount() in the shell.

      Causes

      The following conditions can lead to counts being off:

      • Active migrations
      • Orphaned documents (left from failed migrations)
      • Non-Primary read preferences (see SERVER-5931)

      Workaround

      A workaround to get accurate counts is to ensure all migrations have been cleaned up and no migrations are active. To query non-primaries you must also ensure that there is no replication lag including any migration data, in addition to the above requirements.

      Non-Primary Reads

      For issues with counts/reads from non-primaries please see SERVER-5931

        Issue Links

          Activity

          Hide
          justanyone Kevin J. Rice added a comment -

          @Sam Flint - THANK YOU, that explain().n saves a bunch of time! Great hint!

          Show
          justanyone Kevin J. Rice added a comment - @Sam Flint - THANK YOU, that explain().n saves a bunch of time! Great hint!
          Hide
          jonhyman Jon Hyman added a comment -

          Do you know if this is going to make it into 2.8? We have a 6 hour balancer window and our counts can be wrong during 25% of the day due to it.

          Show
          jonhyman Jon Hyman added a comment - Do you know if this is going to make it into 2.8? We have a 6 hour balancer window and our counts can be wrong during 25% of the day due to it.
          Hide
          digi604 Patrick Lauber added a comment -

          this bug cost me almost a week as i tried to understand why my import numbers differed from my source...

          Show
          digi604 Patrick Lauber added a comment - this bug cost me almost a week as i tried to understand why my import numbers differed from my source...
          Hide
          markus.mahlberg@icloud.com Markus Mahlberg added a comment -

          Interestingly, an aggregation counting the documents returns the correct value:

          db.collection.aggregate({$group:{_id:"uniqueDocs",count:{$sum:1}}})
          

          Show
          markus.mahlberg@icloud.com Markus Mahlberg added a comment - Interestingly, an aggregation counting the documents returns the correct value: db.collection.aggregate({$group:{_id: "uniqueDocs" ,count:{$sum:1}}})
          Hide
          farid.sharipov@basware.com Farid Sharipov added a comment -

          In our case we are seeing this problem in un-sharded collection with Mongo 3.0

          mongos> db.messages.count()
          15
          mongos> db.messages.count()
          15
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.count()
          15
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.find({})
          mongos> db.messages.findOne()
          null
          mongos> db.messages.count()
          15
          mongos> db.messages.count()
          15
          mongos> db.messages.count()
          15
          mongos> db.messages.count()
          15
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.find()
          mongos> db.messages.find().count()
          15
          

          Show
          farid.sharipov@basware.com Farid Sharipov added a comment - In our case we are seeing this problem in un-sharded collection with Mongo 3.0 mongos> db.messages.count() 15 mongos> db.messages.count() 15 mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.count() 15 mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.find({}) mongos> db.messages.findOne() null mongos> db.messages.count() 15 mongos> db.messages.count() 15 mongos> db.messages.count() 15 mongos> db.messages.count() 15 mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.find() mongos> db.messages.find().count() 15

            People

            • Votes:
              44 Vote for this issue
              Watchers:
              75 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since reply:
                36 weeks, 5 days ago
                Date of 1st Reply: