0

The query logic is like that:

(A > x OR A < y) AND (B > m OR B < n) AND C = z db.test.find({$or:[{A:{$gt:x}}, {A:{$lt:y}}], C:z}) 

but how to add B's condition into this query.

2 Answers 2

1

Being (A > x OR A < y) A and (B > m OR B < n) B, then the rest C, wrap them inside an $and comparison: {$and: [A, B, C]}, such as:

db.test.find({ $and: [ {$or: [{A: {$gt: x}}, {A: {$lt: y}}]}, {$or: [{B: {$gt: m}}, {B: {$lt: n}}]}, C: z ] }) 
Sign up to request clarification or add additional context in comments.

2 Comments

But my MongoDB server version is 1.8.2, don't support $and operator
if $and operator is not supported, do it at application level. This is the way out. You may have to get data in two or three queries and then 'AND' them on app level.
0

As you can use $or but can't use $and, you can split the first query group (totally 3 groups) like this:

db.test.find({ $or : [ { A : { $gt : x }, $or : [ { B : { $gt : m } }, { B : { $lt : n } } ] }, { A : { $lt : y }, $or : [ { B : { $gt : m } }, { B : { $lt : n } } ] } ], C : z }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.