设置Mongoid哈希字段值 [英] setting mongoid hash field values

查看:73
本文介绍了设置Mongoid哈希字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Rails项目(均为4.0.x)中使用Mongoid,并且我有一个带有哈希字段的文档,其中存储了一些无模式数据.

I'm using Mongoid in a Rails project (both 4.0.x), and I've got a document with a hash field that stores some schema-less data.

class Thing
  field :name, type: String
  field :mass, type: Integer
  field :info, type: Hash
end

通过此设置,我可以查询具有键:endDate的内容,例如:

With this setup, I can query for things, say, that have a key :endDate like so:

Thing.where("info.endDate"=>{'$exists'=>true})

这一切都很好用.为该:info字段使用哈希字段会很好,因为我要存储的内容没有固定的架构,并且因一件事而异.

And that's all nice and handy. Using a hash field for this :info field is nice because what I want to store doesn't have a fixed schema and varies from one thing to another.

好,但是,我不能对:info哈希中的$set键/值对使用相同的点语法. [1]

Ok, but, I can't use the same dot syntax to $set key/value pairs in the :info hash. [1]

thing.set("info.endDate"=>Time.now) 

引发Mongoid::Errors::UnknownAttribute错误.

它告诉我我必须在模型中包含Mongoid::Attributes::Dynamic才能执行此操作,但这对我来说似乎不合适. 哈希字段类型的要点似乎是为了允许您使用没有固定模式的数据.看来我不必包括一个特殊的动态属性"模块才能使用哈希字段.

It tells me I'd have to include Mongoid::Attributes::Dynamic in my model to do this, but that doesn't seem right to me. The point of the hash field type seems to be to allow you to work with data that doesn't have a fixed schema. It doesn't seem like I should have to include a special "dynamic attributes" module to use hash fields.

现在,我正在使用常规的旧[]语法更新值,然后在模型上调用save,如下所示:

So right now, I'm updating values using regular old [] syntax, and then calling save on the model, like so:

thing.info[:endDate] = Time.now
thing.save

但是很多时候,只是$set值会更好.还有其他用于设置哈希字段值的语法吗?我对上述错误消息和动态属性的理解有误吗?我现在是否坚持对哈希字段进行两步更新?

But a lot of the time it happens that it would be nicer to just $set the value. Is there some other syntax for setting hash field values? Am I wrong about the above error message and Dynamic Attributes being wrong-headed? Am I stuck doing two step updates to hash fields for now?

[1]诚然,我最近从mongomapper迁移了,所以我对这种语法的期望在一定程度上是因为以前能够在mongomapper中做到这一点.

[1] admittedly, I've recently migrated from mongomapper, and so my expectations of this syntax are partly set by having been able to do this previously in mongomapper.

推荐答案

带有Hash字段的东西是,它可以根据需要动态地变化.因此,为防止代码中的错误导致意外字段污染数据库架构,默认情况下此功能处于禁用状态.

The thing with Hash field is, it can be dynamic as much as you want. Therefore to prevent polluting your DB schema with unintended fields caused by bugs in your code this functionality is disabled by default.

不,您根本不会对哈希进行两步更新!

No you are not stuck using 2-step updates for your hashes at all!

[],[] =是read_attribute()write_attribute()的快捷方式,如果不包含Mongoid::Attributes::Dynamic,则应使用它们.如果您尝试在不启用动态属性的情况下使用$set,则会出现无方法错误,因为它不会将动态属性视为已定义的属性.

[],[]= are the shortcuts for read_attribute() and write_attribute() and should be used if you don't include Mongoid::Attributes::Dynamic. If you try to use $set without enabling dynamic attributes you will get a no-method error because it does not see your dynamic attributes as defined attributes.

如果您阅读 Mongoid的源代码:: Attributes :: Dynamic ,那么您会发现添加动态属性功能是必需的.

If you'll read the source of Mongoid::Attributes::Dynamic then you'd find that this is required to add the dynamic attributes functionality.

要通过包含Mongoid::Attributes::Dynamic来更新值,您需要执行以下步骤:

To update the values by including Mongoid::Attributes::Dynamic you need to follow these steps:

thing = Thing.first
thing.set("info.endDate" => Time.now)
thing.reload # This will update the current variable 

否则,如果需要,您可以轻松地跳过此步骤,并通过两步方法进行值更新

Otherwise if you need you can easily skip this and do the value update by 2-step method

我希望这对您的查询有所帮助.

I hope this sheds some light over your query.

来源:

滚动Mongoid动态字段-没有方法错误

具有Rails和Mongoid的动态属性

这篇关于设置Mongoid哈希字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆