Aquarius_Girl
Aquarius_Girl

Reputation: 22916

When should $and operator be skipped in MongoDB?

https://www.w3resource.com/mongodb-exercises/mongodb-exercise-12.php

db.restaurants.find(
                           {
                             "cuisine" : {$ne : "American "},
                             "grades.score" :{$gt: 70},
                             "address.coord" : {$lt : -65.754168}
                            }
                     );

Following is mentioned there

Note : Do this query without using $and operator.

If it can be solved without $and operator, then is $and operator just for readability sake or there is any particular reason we should prefer to use $and operator?

Upvotes: 0

Views: 22

Answers (1)

Gibbs
Gibbs

Reputation: 22964

$and operator just for readability sake

No,

When should $and operator should be skipped in MongoDB?

other than below cases

  1. Queries With Multiple Expressions Specifying the Same Field, then only option to achieve is using and
  2. Queries With Multiple Expressions Specifying the Same Operator

Refer

If you have to perform A OR (B AND C), then you need to use $or, $and.

If you have to get all American cushiness or abc cushiness with grades score or address coord, you need to use $and, $or

And the link OP mentioned is an exercise, so you can achieve that with/without $and.

Upvotes: 1

Related Questions