我正在使用Ruby Mongo Driver。Rails 3 + MongoDB:如何做一個嵌套的查詢?
@surname = coll2.find("name" => {"surname" => "testing"})
這不工作?我沒有得到任何結果。
我{"name" : { "surname" : "testing" }}
我正在使用Ruby Mongo Driver。Rails 3 + MongoDB:如何做一個嵌套的查詢?
@surname = coll2.find("name" => {"surname" => "testing"})
這不工作?我沒有得到任何結果。
我{"name" : { "surname" : "testing" }}
您的代碼應該很好地工作。
> 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"}}
我認爲,以下將太
coll2.find("name.surname"=>"testing").first
工作對我來說,用兩個大括號能工作。像這樣:
col2.find({"name.surname": "testing"})