Ruby-on-rails Mongoid OR 查询语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12999368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoid OR query syntax
提问by Akshat
This must be asked alot but it is very poorly documented. There is no mention at http://mongoid.org/en/mongoid/docs/querying.html
这必须被问到很多,但它的记录很差。http://mongoid.org/en/mongoid/docs/querying.html 中没有提及
I'm trying to check whether a user exists (below is an AND query), how can I change it to an ORtype query
我正在尝试检查用户是否存在(以下是 AND 查询),如何将其更改为OR类型查询
Username.where(:username=>@username, :email=>@email)
(Either the email or the username must match).
(电子邮件或用户名必须匹配)。
I have found some pretty complicated ways online including sending a direct javascript (from): http://omarqureshi.net/articles/2010-6-17-using-or-in-mongoid
我在网上发现了一些非常复杂的方法,包括直接发送 javascript(来自):http: //omarqureshi.net/articles/2010-6-17-using-or-in-mongoid
Surely there must be a simple clear syntax to do this correctly?
当然必须有一个简单清晰的语法才能正确地做到这一点?
回答by James Lim
For the sake of others who end up on this page, the updated syntax is now
为了其他最终出现在此页面上的人,更新的语法现在是
Username.or({username: @username}, {email: @email})
Please refer to the updated docsor relevant impl.
回答by Sergio Tulentsev
Yeah, this used to be documented better. Try this:
是的,这曾经被更好地记录下来。尝试这个:
Username.any_of({:username => @username},
{:email => @email})
回答by Johan Tique
Also, in Mongoid 5.0, if you still want to use the wheremethod, use the following
另外,在 Mongoid 5.0 中,如果你还想使用该where方法,请使用以下方法
Username.where('$or' => [ { username: @username }, { email: @email } ])
this is very useful when you are building a kind of query hash in a dynamic way and you need to keep the wheremethod
当您以动态方式构建一种查询哈希并且需要保留该where方法时,这非常有用
回答by Laura
There is a typo in @miguel-savignano's response. According to Stackoverflow the "edit queue is full", which is why I didn't submit an edit.
@miguel-savignano 的回复中有一个错字。根据 Stackoverflow,“编辑队列已满”,这就是我没有提交编辑的原因。
Proper syntax:
正确的语法:
Username.or({username: @username}).or({email: @email})
A more concise solution:
更简洁的解决方案:
Username.or({username: @username}, {email: @email})
The Mongo selector will resolve to:
Mongo 选择器将解析为:
{"$or"=>[{"username"=>@username}, {"email"=>@email}]}
I found this question as I was trying to solve for creating "or" queries.
我在尝试解决创建“或”查询时发现了这个问题。
If you are looking to match a string or any one of an array of elements, then you will need to write a Mongoid query with the Mongo '$in' selector.
如果要匹配字符串或元素数组中的任何一个,则需要使用 Mongo '$in' 选择器编写 Mongoid 查询。
For example, you have a specific username and an array of emails. You would like to return all results where at least one of the fields matches either the username or one of the emails within the array.
例如,您有一个特定的用户名和一组电子邮件。您希望返回至少有一个字段与数组中的用户名或电子邮件之一匹配的所有结果。
@username = "jess"
@emails = ["[email protected]", "[email protected]", "[email protected]"]
User.or({username: ""}, {email: {'$in': @emails}})
The Mongo selector will resolve to:
Mongo 选择器将解析为:
{"$or"=>[{"first_name"=>""}, {"email"=>{:$in=>["[email protected]", "[email protected]", "[email protected]"]}}]}
- If you have
Userswith 2 of the 3 emails in your database, then the selector will return a count of 2. - If you have a
Userwith theusername"jess" and 3 additionalUserseach with one of the given emails, then the selector will return a count of 4.
- 如果您
Users的数据库中有 3 封电子邮件中的 2 封,则选择器将返回计数 2。 - 如果您有一封
User带有username“jess”Users的邮件,另外还有 3封带有给定电子邮件之一的邮件,那么选择器将返回 4 的计数。
回答by miguel savignano
In Mongoid 5.0, this works for me
在 Mongoid 5.0 中,这对我有用
Username.or({username: @username}).or({email: @email})

