Discussion:
[mongodb-user] oplog count discrepancies
Justin W
2016-09-21 18:50:47 UTC
Permalink
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.

<Loading Image...>


Thank you,
Justin
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/2741471a-2bd7-4fae-9fba-86ca5a12fe45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
John Murphy
2016-09-28 05:02:32 UTC
Permalink
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() <https://docs.mongodb.com/manual/reference/command/count/>
command is generally an estimate of the current document count, based upon
statistics provided by the storage engine.

After an unclean shutdown
<https://docs.mongodb.com/manual/reference/command/count/#accuracy-after-unexpected-shutdown>
of a mongod
<https://docs.mongodb.com/manual/reference/program/mongod/#bin.mongod>
using the WiredTiger <https://docs.mongodb.com/manual/core/wiredtiger/>
storage engine, count statistics reported by count may be inaccurate. If
this does occur you can run validate()
<https://docs.mongodb.com/manual/reference/command/validate/#dbcmd.validate>
on each collection to restore the correct statistics.

To accurately determine the current document count of a collection use the
$group
<https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group>
stage of the db.collection.aggregate()
<https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/#db.collection.aggregate>
method to $sum
<https://docs.mongodb.com/manual/reference/operator/aggregation/sum/#grp._S_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
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/4ce1db4f-8c87-40e9-bf1e-f6866d974017%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...