在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}
}
);

以下是

Note : Do this query without using $and operator.

如果它可以在没有$和运算符的情况下解决,那么$和运算符只是为了可读性,还是有任何特殊的原因我们应该更喜欢使用$和运算符?

$和运算符只是为了可读性

否,

在MongoDB中什么时候应该跳过$和运算符?

other than below cases

  1. 具有多个表达式的查询指定Same Field,则唯一要实现的选项是使用and
  2. 具有指定Same Operator的多个表达式的查询

参考

如果必须执行A OR (B AND C),则需要使用$or$and

如果必须使用grades scoreaddress coord获取所有American cushinessabc cushiness,则需要使用$and, $or

上面提到的链接OP是一个练习,所以你可以在有/没有$and的情况下实现它。

最新更新