Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to search multiple feilds' "$and"? #681

Closed
lanistor opened this issue Jul 5, 2021 · 4 comments
Closed

How to search multiple feilds' "$and"? #681

lanistor opened this issue Jul 5, 2021 · 4 comments

Comments

@lanistor
Copy link

lanistor commented Jul 5, 2021

Such as ((A>1 && C>1) or (A<5)) && ((C>1 && D<1 ) or ( C<5 )).
For only field "A" case, we can use

db.find({ 
  $or: [
    { A: { $gt: 1 }, B: {$gt: 1} },
    { A: { $lt: 5 } },
  ],
});

But with field "B", we cannot use:

db.find({ 
  $or: [
    { A: { $gt: 1 }, B: {$gt: 1} },
    { A: { $lt: 5 } },
  ],
  $or: [
    { C: { $gt: 1 }, D: {$lt: 1} },
    { C: { $lt: 5 } },
  ],
});

So, how we can do this?

@Jose134
Copy link

Jose134 commented Jul 23, 2021

I'm not sure why your solution isn't working but a workaround is to AND both conditions like this:

db.find({
    $and: [
        { $and: [ { A: { $gt: 1 } }, { A: { $lt: 5 } }, ]},
        { $and: [ { B: { $gt: 1 } }, { B: { $lt: 5 } }, ]}
    ]
});

Alternatively you can make it cleaner by putting everything in the same $and instead of nesting:

db.find({
    $and: [
        { A: { $gt: 1 } },
        { A: { $lt: 5 } },
        { B: { $gt: 1 } },
        { B: { $lt: 5 } }
    ]
});

@lanistor
Copy link
Author

I'm not sure why your solution isn't working but a workaround is to AND both conditions like this:

db.find({
    $and: [
        { $and: [ { A: { $gt: 1 } }, { A: { $lt: 5 } }, ]},
        { $and: [ { B: { $gt: 1 } }, { B: { $lt: 5 } }, ]}
    ]
});

Alternatively you can make it cleaner by putting everything in the same $and instead of nesting:

db.find({
    $and: [
        { A: { $gt: 1 } },
        { A: { $lt: 5 } },
        { B: { $gt: 1 } },
        { B: { $lt: 5 } }
    ]
});

It was meant to make the example a bit simpler, but it was ambiguous. In our real query scenario, it will be more complicated. I have modified my sample code.

@Jose134
Copy link

Jose134 commented Jul 23, 2021

This should match the updated query:

db.find({
    $and: [
        {$or: [
            { $and: [
                { A: { $gt: 1 } },
                { C: { $gt: 1 } }
            ]},
            {
                A: { $lt: 5 }
            }
        ]},
        {$or: [
            { $and: [
                { C: { $gt: 1 } },
                { D: { $lt: 1 } }
            ]},
            {
                C: { $lt: 5 }
            }
        ]}
    ]
});

It's not the prettiest thing ever but I've done a bit of testing and it seems to work properly

@lanistor
Copy link
Author

It's not the prettiest thing ever but I've done a bit of testing and it seems to work properly

Got it. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants