Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid OR query syntax

This must be asked alot but it is very poorly documented. There is no mention at 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 OR type query

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

Surely there must be a simple clear syntax to do this correctly?

like image 221
Tarang Avatar asked Oct 21 '12 15:10

Tarang


People also ask

How do I capture a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How do I run a MongoDB query in Robo 3T?

Open Studio 3T and connect to your MongoDB database. Next, open the Import Wizard from the toolbar. Then, choose JSON as the import format. Click OK.


1 Answers

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 docs or relevant impl.

like image 146
James Lim Avatar answered Sep 25 '22 15:09

James Lim