This sounds odd, and I hope I am doing something wrong, but my MongoDB
collection is returning the Count
off by one in my collection.
I hav
According to this issue, this behaviour can occur if mongodb experiences a hard crash and is not shut down gracefully. If not issuing any query, mongodb probably just falls back to the collected statistics.
According to the article, calling db.COLLECTION.validate(true)
should reset the counters.
As now stated in the doc, db.collection.count() without using a query parameter, returns results based on the collection’s metadata:
This may result in an approximate count. In particular:
On a sharded cluster, the resulting count will not correctly filter out orphaned documents.
After an unclean shutdown, the count may be incorrect.
When using a query parameter, as you did in the second query ({_id: {$exists: true}}
), then it forces count
to not use the collection's metadata, but to scan the collection instead.
Starting Mongo 4.0.3
, count()
is considered deprecated and the following alternatives are recommended instead:
db.collection.countDocuments({})
which under the hood actually performs the following "expensive", but accurate aggregation (expensive since the whole collection is scanned to count records):
db.collection.aggregate([{ $group: { _id: null, n: { $sum: 1 } } }])
db.collection.estimatedDocumentCount()
which performs exactly what db.collection.count()
does/did (it's actually a wrapper around count
), which uses the collection’s metadata.
This is thus almost instantaneous, but may lead to an approximate result in the particular cases mentioned above.