【发布时间】:2014-01-13 06:45:07
【问题描述】:
我想使用 mongoid 搜索,例如带有整数列的查询。
我知道使用 mongodb 可以使用下面的命令来查询
db.test.find({ $where: "/^123.*/.test(this.example)" })
用mongoid怎么写?
【问题讨论】:
我想使用 mongoid 搜索,例如带有整数列的查询。
我知道使用 mongodb 可以使用下面的命令来查询
db.test.find({ $where: "/^123.*/.test(this.example)" })
用mongoid怎么写?
【问题讨论】:
你知道你可以使用所有常见的 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 查询。
顺便说一句,.* 在你的正则表达式中没有做任何有用的事情,所以我把它拿出来了。
【讨论】: