oplog count discrepancies

45 views
Skip to first unread message

Justin W

unread,
Sep 21, 2016, 8:09:46 PM9/21/16
to mongodb-user
Greetings,

We've recently been investigating using oplog tailing as a way for us to export mongo data on-the-fly and have ran into something that is making us second guess this solution.  When using the 'local' db and running 'db.oplog.rs.count()' we get a lower number then running 'db.oplog.rs.find({'t': {$exists: true}}).count()'.  Is this expected behavior and if so can someone provide me with an explanation so I'm not freaking about this anymore.  :)  I'm running mongo 3.2.



Thank you,
Justin

John Murphy

unread,
Sep 28, 2016, 1:02:32 AM9/28/16
to mongodb-user

Hi Justin,

Is this expected behavior and if so can someone provide me with an explanation so I’m not freaking about this anymore.

The count() command is generally an estimate of the current document count, based upon statistics provided by the storage engine.

After an unclean shutdown of a mongod using the WiredTiger storage engine, count statistics reported by count may be inaccurate. If this does occur you can run validate() on each collection to restore the correct statistics.

To accurately determine the current document count of a collection use the $group stage of the db.collection.aggregate() method to $sum the documents.

For example, the following query provides a real-time count of the documents in the oplog.rs collection:

db.getSiblingDB('local').oplog.rs.aggregate([{ $group: { _id : null, count : { $sum: 1 } } }])

Regards,
John Murphy

Reply all
Reply to author
Forward
0 new messages