简体   繁体   English

带有 Hash 字段的 MongoId:MongoDB 和 Rails 的值不同

[英]MongoId with Hash field: values are different with MongoDB and with Rails

I have a model in MongoId with this field:我在 MongoId 中有一个带有此字段的模型:

  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:在 Rails 控制台中查询特定行时,我有以下内容:

<_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:在 MongoDB 控制台中查询相同的 id 时,我有以下内容:

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每月 [3] 我在 MongoDb 控制台中有 0 个,在 Rails 控制台中有 3 个
  • For monthly[11] I have 2 in MongoDb console and 0 in Rails console每月 [11] 我在 MongoDb 控制台中有 2 个,在 Rails 控制台中有 0 个

I can't understand why我不明白为什么

My MongoDb versions is v3.4.13 (shell and server)我的 MongoDb 版本是 v3.4.13(外壳和服务器)

And I am in Rails 5.2.2 with Ruby 2.4.5 and MongoId 7.0.2我在使用 Ruby 2.4.5 和 MongoId 7.0.2 的 Rails 5.2.2

EDIT :编辑 :

Here is info about the queries with MongoId and the result:以下是有关 MongoId 查询和结果的信息:

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我的问题只针对这个id 5ddf95cd1dd6150008356ec6,因为mongodb和mongoid中的其他id数据是匹配的

What was the Mongoid query?什么是 Mongoid 查询?

Use direct collection access to retrieve the raw document using the driver and compare to the Mongoid result:使用直接集合访问使用驱动程序检索原始文档并与 Mongoid 结果进行比较:

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 (eg same environment is used in both queries).验证您在同一个数据库上查询同一个集合(例如,在两个查询中使用相同的环境)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM