MongoDB逻辑运算符

mac2022-06-30  105

逻辑与   $and:要求满足所有查询条件 ,否则返回空

语法:db.集合名.find{ $and: [ { <expression1> }, { <expression2> } , ... , {<expressionN> } ] }

//逻辑与 查询价格在2000-5000之间的手机 db.product1.find({$and:[{"category":"手机"},{"price":{$gt:2000,$lt:5000}}]})

逻辑或   $or:满足一个条件就行

语法:db.集合名.find{ $or: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }

//逻辑或 查询食品类和饮料类的食品 db.product1.find({$or:[{"category":"食品"},{"category":"饮料"}]})

异或    $nor:逻辑或原理取反

语法:db.集合名.find{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }

//异或 逻辑或取反 db.product1.find({$nor:[{"category":"书籍"},{"category":"手机"}]})

 逻辑非   $not :取反查询条件

语法:db.集合名.find{ field: { $not: { <operator-expression> } } }

//逻辑非 db.product1.find({category:{$not:{$eq:"手机"}}})

 

转载于:https://www.cnblogs.com/dyd520/p/11451851.html

最新回复(0)