【发布时间】:2011-05-11 23:10:41
【问题描述】:
我正在使用 Ruby Mongo 驱动程序。
@surname = coll2.find("name" => {"surname" => "testing"})
这不应该工作吗?我没有得到任何结果。
我有{"name" : { "surname" : "testing" }}
【问题讨论】:
标签: ruby-on-rails-3 mongodb mongodb-ruby
我正在使用 Ruby Mongo 驱动程序。
@surname = coll2.find("name" => {"surname" => "testing"})
这不应该工作吗?我没有得到任何结果。
我有{"name" : { "surname" : "testing" }}
【问题讨论】:
标签: ruby-on-rails-3 mongodb mongodb-ruby
我认为以下方法也可以
coll2.find("name.surname"=>"testing").first
【讨论】:
您的代码应该可以完美运行。
> coll2.insert({"name" => {"surname" => "testing"})
# => BSON::ObjectId('4dcb2e53abad691f62000002')
> coll2.insert({"name" => {"surname" => "another"})
# => BSON::ObjectId('4dcb2e53abad691f62000003')
> coll2.find().count
# => 2
> coll2.find("name" => {"surname" => "testing"}).count
# => 1
> coll2.find("name" => {"surname" => "testing"}).first
# => {"_id"=>BSON::ObjectId('4dcb2e53abad691f62000002'), "name"=>{"surname"=>"testing"}}
【讨论】:
对我来说,它只适用于大括号。像这样:
col2.find({"name.surname": "testing"})
【讨论】: