Question regarding mongo-ruby-driver and having multiple $or query in a single find.

121 views
Skip to first unread message

Rakeesh Ranjan

unread,
Jan 21, 2011, 1:08:04 AM1/21/11
to mongodb-user
Hi,

What is the best way to do the following MongoDB query via the mongo-
ruby-driver?

db.collection.find({ $or : [{"field1" : "value1"}, {"field2" :
"value2"}], $or : [{"field3" : "value3"}, {"field4" : "value4"}]})

Since mongo-ruby-driver find takes a hash it is not possible to pass
multiple "$or" keys into it.

Basically I want to achieve the following type of query:

(field1 == "value1" or field2 == "value2") and (field3 == "value3" or
field4 == "value4")

Thanks
Rakeesh

Nat

unread,
Jan 21, 2011, 1:38:16 AM1/21/11
to mongodb-user
so you would like to do
(a || b) && (c || d)

you should be able to do it in the form of
(a && c) || (a && d) || (b && c) || (b && d)

Rakeesh Ranjan

unread,
Jan 21, 2011, 1:50:46 AM1/21/11
to mongod...@googlegroups.com
Thanks Nat. Will try that approach, although it becomes a little cumbersome when query is getting formed dynamically from different parts of the code. 

Best 
Rakeesh

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
Thanks
Rakeesh Ranjan

Kyle Banker

unread,
Jan 21, 2011, 2:32:28 AM1/21/11
to mongod...@googlegroups.com
This is one place where an $and operator would be nice. You can vote on it here:
http://jira.mongodb.org/browse/SERVER-1089

Rakeesh Ranjan

unread,
Jan 21, 2011, 12:39:05 PM1/21/11
to mongodb-user
Agree Kyle. Will vote for the same. Thanks.

On Jan 21, 12:32 pm, Kyle Banker <k...@10gen.com> wrote:
> This is one place where an $and operator would be nice. You can vote on it here:http://jira.mongodb.org/browse/SERVER-1089
>
> On Thu, Jan 20, 2011 at 11:50 PM, Rakeesh Ranjan
>
>
>
> <rakeesh.ran...@gmail.com> wrote:
> > Thanks Nat. Will try that approach, although it becomes a little cumbersome
> > when query is getting formed dynamically from different parts of the code.
> > Best
> > Rakeesh
>
Reply all
Reply to author
Forward
0 new messages