MongoDB count results in incorrect number (MongoDB count a pit)

Source: Internet
Author: User
Tags mongoclient mongo shell

Https://www.cnblogs.com/Kellana/p/5844919.html

In a MongoDB cluster, if exists orphaned documents and chunk migration, the count query may result in an incorrect query result, such as I was stepping on the pit, first not talking, see the result:

Skip can only get to 54631, and count found out 77,396 data, this is the pit, the same query conditions, not the same results, in order to avoid this result, you can use the aggregate query,

As shown, the inside is a pit.

For more information on the count of MongoDB, see official website: https://docs.mongodb.com/manual/reference/command/count/

https://segmentfault.com/q/1010000008787002

The problem has been resolved, using the latest driver mongo-java-driver-3.4.0, through the following method can be in the Shard cluster mode, accurate statistics to the number of records, thank you for your help! MONGO Shell >> db.collection.aggregate ([{$match: {categories: "Bakery"},{$group: {"_id": null, "Count": {$sum: 1}} }}])
  Public LongGetCount () {String user="User name"; String Database="Admin"; String Password="Password"; Mongocredential Credential=mongocredential.createcredential (User,database, Password.tochararray ()); Mongoclientoptions Options=Mongoclientoptions.builder (). Connectionsperhost (Ten). Threadsallowedtoblockforconnectionmultiplier (Ten). Sockettimeout (20000). ConnectTimeout (15000). Maxwaittime (50000). build (); Mongoclient mongoclient=NewMongoclient (NewServerAddress ("IP Address","Port"), Arrays.aslist (credential), options); Mongodatabase Mongodatabase= Mongoclient.getdatabase ("Database"); Mongocollection<Document> collection = Mongodatabase.getcollection ("Data Sheet"); FinalLong[] Count =New Long[1]; Block<Document> Printblock =NewBlock<document>() {@Override Public voidApply (Final Document document) {count[0] = (Long) document.Get("Count");                }                }; Bson Bson= Filters.eq ("Categories","Bakery");                                Collection.aggregate (Arrays.aslist (Aggregates.match (Bson), Aggregates.group (NULL, Accumulators.sum ("Count",1L)) . ForEach (Printblock); returncount[0];}

MongoDB count results in incorrect number (MongoDB count a pit)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.