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
db.myCollection.find({"_id": "5ddf95cd1dd6150008356ec6"});
in mongo shell returns? – Alex Blexdb.setProfilingLevel(2)
, run theMyCollection.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. – Alex Blex