[Fixed]-Mongo:the return don't equal count()

1👍

✅

This is is due to one of the following reasons as mentioned in the documentation.

On a sharded cluster, db.collection.count() can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.

To avoid these situations, on a sharded cluster, use the $group stage of the db.collection.aggregate() method to $sum the documents. For example, the following operation counts the documents in a collection:

You can use the aggregate method to do this as suggested in the documentation.

Scan.aggregate(
    {'$group': {
        '_id': None, 
        'count': {'$sum': 1}
    }}
)

Leave a comment