# mongodb之逻辑查询运算符
###1、逻辑查询运算符
运算符 | 含义 |
---|---|
$exists | 连接多个查询语句,返回同时符合的 |
$not | 一个查询语句,返回不符合这个查询语句的 |
$nor | 连接多个查询语句,返回不符合这些查询语句的 |
$or | 连接多个查询语句,返回符合其中一个查询语句的 |
###2、and运算符
$and(且)连接多个查询语句,在中括号[]里面表示,
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
db.inventory.find( {
$and : [
{ $or : [ { price : 0.99 }, { price : 1.99 } ] },
{ $or : [ { sale : true }, { qty : { $lt : 20 } } ] }
]
} )
###3、not运算符
作用在field后面,用在查询条件前:
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
支持正则匹配:
db.inventory.find( { item: { $not: /^p.*/ } } )
###4、nor运算符
连接多个:
db.inventory.find( { $nor: [ { price: 1.99 }, { sale: true } ] } )
db.inventory.find( { $nor: [ { price: 1.99 }, { price: { $exists: false } },
{ sale: true }, { sale: { $exists: false } } ]
} )
###5、or运算符
连接多个:
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
![](https://i-blog.csdnimg.cn/blog_migrate/07df64cf5f8739d9e4ee18585da0bd9c.jpeg)