mongodb://USER:[email protected]:1234,MYMONGO2.com:1234/DB_NAME?replicaSet=REPLICA_SET_NAME
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:
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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With