tags:

views:

47

answers:

2

I need to write a MongoDB query of the form "A OR B OR (C AND D)" to return some records. We are using Mongoid for data access to our models.

I expanded that query to "(A OR B OR C) AND (A OR B OR D)" and was hoping that using Mongoid's Criteria method any_of like this: Model.any_of(A, B, C).any_of(A, B, D) would accomplish what I want, but that is expanded as "A OR B OR C OR A OR B OR D" before being sent to the database.

Is there a way to build this query, or do I have to build one query to do A OR B and another to do C AND D and take the union of them?

A: 

Check this link, hope it might help you -

http://mongoid.org/docs/querying/

Probably if possible, you can combine ".any_in" and ".and" and check out.

Sachin Shanbhag
A: 

It looks like this is a bug in mongo, but I'm having a tough time figuring out where mongo issues are reported / tracked.

> db.things.insert({x : 1, y: 2});
> db.things.insert({x : 2, y: 3});
> db.things.insert({x : 3, y: 4});
> db.things.find( { $or : [{x:1,x:2}], $or : [{y:1,y:2}] });
{ ..., "x" : 1, "y" : 2 }
{ ..., "x" : 2, "y" : 3 }
Ben Taitelbaum