开发者

Rails 3 + MongoDB: How to do a nested query?

开发者 https://www.devze.com 2023-03-05 18:52 出处:网络
I am using Ruby Mongo Driver. @surname = coll2.find(\"name\" => {\"surname\" => \"testing\"}) Shouldn\'t this be working? I get no results.

I am using Ruby Mongo Driver.

  @surname = coll2.find("name" => {"surname" => "testing"})

Shouldn't this be working? I get no results.

I have {"nam开发者_高级运维e" : { "surname" : "testing" }}


I think that the following would work too

coll2.find("name.surname"=>"testing").first


Your code should work perfectly.

> 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"}} 


For me, it worked only with curly brackets. Like that:

col2.find({"name.surname": "testing"})
0

精彩评论

暂无评论...
验证码 换一张
取 消