【问题标题】:Mongoid search like with integer像整数一样的 Mongoid 搜索
【发布时间】:2014-01-13 06:45:07
【问题描述】:

我想使用 mongoid 搜索,例如带有整数列的查询。

我知道使用 mongodb 可以使用下面的命令来查询

db.test.find({ $where: "/^123.*/.test(this.example)" })

用mongoid怎么写?

【问题讨论】:

    标签: mongodb mongoid


    【解决方案1】:

    你知道你可以使用所有常见的 MongoDB 查询运算符和 Mongoid 的 where 所以:

    Test.where(:$where => '/^123/.test(this.example)')
    

    如果您查看where 为您提供的Mongoid::Criteria,您会看到如下内容:

     => #<Mongoid::Criteria
      selector: {"$where"=>"/^123/.test(this.example)"}
      options:  {}
      class:    Test
      embedded: false>
    

    selector 中有底层 MongoDB 查询。

    顺便说一句,.* 在你的正则表达式中没有做任何有用的事情,所以我把它拿出来了。

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多