3
votes

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

1
Do you know which of the two queries is the correct one?prasad_
Seems to be MongoDbphyzalis
What db.myCollection.find({"_id": "5ddf95cd1dd6150008356ec6"}); in mongo shell returns?Alex Blex
Great suggestion but I have nothing returnedphyzalis
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.Alex Blex

1 Answers

0
votes

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).