MongoDB Mongoid 模糊查询

menghuanwd · April 21, 2013 · Last by lehug replied at March 24, 2016 · 6088 hits

Book.where(:name => '%'+params[:name]+'%'),这么查,不行, 网上搜到: @books = Book.where(:name => /params[:name].*/i),这样查,也不行, 求正确的 mongoid 模糊查询方式。

@books = Book.where(:name => /#{params[:name]}.*/i) 无责任猜测哦,你试试

#1 楼 @keating 也不行啊,试了下,查到到还是空

然后,/#{params[:name]}/ 怎么样?

#3 楼 @keating 哇,你是天才,成了,谢了哥们。

#3 楼 @keating User.where(nickname: /ta/).first 这样可以查出 nickname 为 star 的用户,但是参数是一个字符串 User.where(nickname: /“ta”/).first 这样是查不出来的,有没有办法啊?

怎么模糊查询开头是 XXX 的?

You need to Sign in before reply, if you don't have an account, please Sign up first.