can I use combination of OR and AND in mongodb queries?
the code below doesn\'t work as expected
db.things.find({
$and:[
{
This is possible in following manner (mongodb 3.4)
Here is the good example
https://docs.mongodb.com/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-operator
db.getCollection('tbl_menu_items').find({
$and : [
{ $or : [ { price : 0.99 }, { price : 1.99 } ] },
{ $or : [ { sale : true }, { qty : { $lt : 20 } } ] }
]
} )
This query will select all documents where:
the price field value equals 0.99 or 1.99, and the sale field value is equal to true or the qty field value is less than 20.
Here is the example of $and condition with $or query i.e matching any of condition for example
consider following query...
db.getCollection('tbl_menu_items').find(
{ '$or':
[ { '$and':
[ { location: { '$in': [ ObjectId("588054c63879f2e767a1d553") ] } },
{ is_ryward: 1 },
{ points_reqiured_to_redeem_reward: { '$lte': 500 } } ] },
{ '$and':
[ { location: { '$in': [ ObjectId("588054c63879f2e767a1d553") ] } },
{ is_freebie: 1 } ] } ] }
)
This query will select all documents where:
location array in 588054c63879f2e767a1d553 and is_ryward = 1 and points_reqiured_to_redeem_reward < 500
Or
location array in 588054c63879f2e767a1d553 and is_freebie