Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count incorrect in MongoDB

Tech:

  • MongoDB 3.0.8 (MMAPv1), not sharded, dedicated cluster hosted on AWS via mLab, (primary, secondary and arbiter), 3.7GB RAM
  • C# driver for MongoDB 2.3
  • Connection string:

mongodb://USER:[email protected]:1234,MYMONGO2.com:1234/DB_NAME?replicaSet=REPLICA_SET_NAME

Assumptions

  • I have collection Products with one of the fields called Package.
  • There is index: "Package": 1
  • Objects from this collection are never being deleted.
  • The field Package is never being updated.
  • New objects are inserted from time to time.

Once a day I log a specific count on this collection (same parameters every time):

db.Products({"Package": "Box"}).count()
// actual code running in C#:
productsCollection.Find(p => p.Package == "Box").Count()

I expect the result to be the same or greater every day. But sometimes I get a smaller value. The next day it becomes correct again. It reproduced on two different environments.

Example:

  • Day 1: 4,563,135
  • Day 2: 4,563,135
  • Day 3: 4,563,124 (exactly 11 less than expected)
  • Day 4: 4,563,135

I was trying to manually reproduce it both via C# and directly against Mongo, but failed (the value was always correct).

What's going on?

like image 974
Gena Avatar asked Nov 20 '16 10:11

Gena


People also ask

Where is count in MongoDB Atlas?

The Atlas Search count option adds a field to the metadata results document that displays a count of the search results for the query. You can use count to determine the size of the result set. You can use it in the $search or $searchMeta stage.

How do I find the size of a collection in MongoDB?

collection. totalSize() method is used to reports the total size of a collection, including the size of all documents and all indexes on a collection. Returns: The total size in bytes of the data in the collection plus the size of every index on the collection.

Which method returns the number of documents for a given query?

count() The count() method counts the number of documents that match the selection criteria. It returns the number of documents that match the selection criteria. It takes two arguments first one is the selection criteria and the other is optional.


1 Answers

This is probably due to balancing round was taking place when you code was executed. From MongoDB 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.

More about it here: MongoDB documentation

To get the exact result one should use the aggregation framework queries

like image 153
Boris Avatar answered Oct 11 '22 00:10

Boris