【问题标题】:Rails 3 + MongoDB: How to do a nested query?Rails 3 + MongoDB:如何进行嵌套查询?
【发布时间】:2011-05-11 23:10:41
【问题描述】:

我正在使用 Ruby Mongo 驱动程序。

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

这不应该工作吗?我没有得到任何结果。

我有{"name" : { "surname" : "testing" }}

【问题讨论】:

    标签: ruby-on-rails-3 mongodb mongodb-ruby


    【解决方案1】:

    我认为以下方法也可以

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

    【讨论】:

      【解决方案2】:

      您的代码应该可以完美运行。

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

      【讨论】:

        【解决方案3】:

        对我来说,它只适用于大括号。像这样:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-16
          • 1970-01-01
          • 2012-05-04
          • 2021-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多