3

I have a model in MongoId with this field:

  field :m, as: :monthly, type: Hash

The structure is per month hash with month number as key and quantity as values

When querying in Rails console a specific row I have the following:

<_id: 5ddf95cd1dd6150008356ec6, m(monthly): {"1"=>0, "2"=>0, "3"=>3, "4"=>0, "5"=>0, "6"=>0, "7"=>0, "8"=>0, "9"=>0, "10"=>0, "11"=>0, "12"=>0}>

When querying in MongoDB console the same id I have the following:

db.myCollection.find({"_id": ObjectId("5ddf95cd1dd6150008356ec6")});
{ "_id" : ObjectId("5ddf95cd1dd6150008356ec6"), "m" : { "1" : 0, "2" : 0, "3" : 0, "4" : 0, "5" : 0, "6" : 0, "7" : 0, "8" : 0, "9" : 0, "10" : 0, "11" : 2, "12" : 0 } }
  • For monthly[3] I have 0 in MongoDb console and 3 in Rails console
  • For monthly[11] I have 2 in MongoDb console and 0 in Rails console

I can't understand why

My MongoDb versions is v3.4.13 (shell and server)

And I am in Rails 5.2.2 with Ruby 2.4.5 and MongoId 7.0.2

EDIT :

Here is info about the queries with MongoId and the result:

MyCollection.where(id: '5ddf95cd1dd6150008356ec6').first

<MyCollection _id: 5ddf95cd1dd6150008356ec6, m(monthly): {"1"=>0, "2"=>0, "3"=>3, "4"=>0, "5"=>0, "6"=>0, "7"=>0, "8"=>0, "9"=>0, "10"=>0, "11"=>0, "12"=>1}>
MyCollection.collection.find(_id: BSON::ObjectId('5ddf95cd1dd6150008356ec6')).first

{"_id"=>BSON::ObjectId('5ddf95cd1dd6150008356ec6'), "m"=>{"1"=>0, "2"=>0, "3"=>3, "4"=>0, "5"=>0, "6"=>0, "7"=>0, "8"=>0, "9"=>0, "10"=>0, "11"=>0, "12"=>1} }

My issue is only for this id 5ddf95cd1dd6150008356ec6, for other id data in mongodb and mongoid are matching

5
  • Do you know which of the two queries is the correct one? Commented Dec 2, 2019 at 6:52
  • Seems to be MongoDb Commented Dec 2, 2019 at 10:38
  • What db.myCollection.find({"_id": "5ddf95cd1dd6150008356ec6"}); in mongo shell returns? Commented Dec 6, 2019 at 12:11
  • Great suggestion but I have nothing returned Commented Dec 9, 2019 at 8:43
  • Can you enable profiler db.setProfilingLevel(2), run the MyCollection.where(id: '5ddf95cd1dd6150008356ec6').first query and check the actual find query executed by mongoid in system.profile collection. Profiler docs: docs.mongodb.com/manual/tutorial/manage-the-database-profiler It will help to localise the problem - before or after data retrieval. Commented Dec 10, 2019 at 12:37

1 Answer 1

0

What was the Mongoid query?

Use direct collection access to retrieve the raw document using the driver and compare to the Mongoid result:

irb(main):014:0> Band.collection.find(_id: Band.first.id).first
=> {"_id"=>BSON::ObjectId('5de4ba7b026d7c31708db208')}

Verify you are querying the same collection on the same database (e.g. same environment is used in both queries).

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. I will edit my question with this info. And yes I am using the same collection on the same database in same environment
Both queries in MongoId are returning the same result but this result is still different from MongoDb console
Most likely you are querying different databases or collections.
No I am not as I have other fields on this collection which are corrects and are incremented correctly and I have other documents with no issues. I have checked this multiple times
Bump some counters in this document and see what happens. Are you using secondary reads somewhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.