I've stumbled upon a rather strange problem. I have a mongo collection in my app with approximately 4 800 000 records. I am using a query that returns 2 400 000 records from this collection. Consequently I iterate over them and create embedded documents on each record. When I was looking through my logs I noticed that the iteration took place over 4 million times. I dug deeper and found out, that a lot of records got into the iteration multiple times. That means that a record with the same ID appeared as processed in the logs more than once. Below is the query:
MyModel.where({integer_array: {"$not" => {'$size' => 0}}}).all.each do |n|
Very strange is, that when I run in rails console MyModel.where({integer_array: {"$not" => {'$size' => 0}}}).count
I get circa 2400000 as a result.
I am running on rails 3.2.13, mongoid 3.0.21 and mongodb 2.4.0.