Incorrect Count returned by MongoDB (WiredTiger)

后端 未结 2 1635
情书的邮戳
情书的邮戳 2020-12-31 08:27

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

相关标签:
2条回答
  • 2020-12-31 09:00

    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.

    0 讨论(0)
  • 2020-12-31 09:00

    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:

    • Exact count of douments:
    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 } } }])
    
    • Approximate count of documents:
    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.

    0 讨论(0)
提交回复
热议问题