Ruby on rails 在Mongoid中查询哈希

Ruby on rails 在Mongoid中查询哈希,ruby-on-rails,hash,mongoid,Ruby On Rails,Hash,Mongoid,我创建了一个产品文档,如下所示: #<Product _id: 539ac4285468691170000000, created_at: 2014-06-13 09:28:08 UTC, updated_at: 2014-06-13 10:28:57 UTC, properties: {"first"=>{"element_1"=>"test", "element_2"=>"bigtest"}, "second"=>"layer_one"}> 请您指出我可

我创建了一个产品文档,如下所示:

#<Product _id: 539ac4285468691170000000, created_at: 2014-06-13 09:28:08 UTC, updated_at: 2014-06-13 10:28:57 UTC, properties: {"first"=>{"element_1"=>"test", "element_2"=>"bigtest"}, "second"=>"layer_one"}>

请您指出我可以看到更多Mongoid查询示例的参考资料,尤其是提取散列的单个组件的示例。非常感谢。

以下是访问散列值的方法:

> w.properties['first']
=> {"element_1"=>"test", "element_2"=>"bigtest"}

> w.properties['first']['element_2']
=> "bigtest"

> w.properties['first']['element_3'] = "grandetest"
=> {"element_1"=>"test", "element_2"=>"bigtest", "element_3"=>"grandetest"}
您可以使用“where”按“element_2”的值搜索产品,例如:

> Product.where("properties.first.element_2" => "bigtest").count
=> 1

我希望你能有所帮助。

我非常喜欢你的回答。与此同时,我找到了一种不太方便的方法:
>w.properties.\uuu嵌套的\uuuu('first.element\u2')=>“bigtest”
> Product.where("properties.first.element_2" => "bigtest").count
=> 1