【MongoDB】查看文档(逻辑运算)
继续上一篇文档,本章测试逻辑运算 and & or & not & nor
测试数据如下:
1 2 3 4 5 |
> db.t.find() { "_id" : ObjectId("59827e4ec566b07b8733e77d"), "name" : "liuneng", "age" : 22, "sex" : 0 } { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } { "_id" : ObjectId("59828735c566b07b8733e77f"), "name" : "xiaomeng", "age" : 20, "sex" : 1 } > |
1:and
【语法】
1 |
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] } |
【测试】
1 2 3 4 5 6 7 |
> db.t.find({$and:[{sex:0}, {age:{$gt:22}}]}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } > # 当然,$and操作符可以省略,如下 > db.t.find({sex:0, age:{$gt:22}}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } > |
2:or
【语法】
1 |
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } |
【测试】
1 2 3 4 |
> db.t.find({$or:[{age:{$gt:22}}, {sex:1}]}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } { "_id" : ObjectId("59828735c566b07b8733e77f"), "name" : "xiaomeng", "age" : 20, "sex" : 1 } > |
3:and & or
1 2 3 4 5 6 7 8 |
# 等价于关系型数据库(age > 22 or sex = 1) and name = 'zhaosi' > db.t.find({name:"zhaosi" ,$or:[{age:{$gt:22}}, {sex:1}]}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } > # 或者 > db.t.find({$or:[{age:{$gt:22}}, {sex:1}], name:"zhaosi"}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } > |
4:not
【语法】
1 |
{ field: { $not: { <operator-expression> } } } |
【测试】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 所有文档 > db.t.find() { "_id" : ObjectId("59827e4ec566b07b8733e77d"), "name" : "liuneng", "age" : 22, "sex" : 0 } { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } { "_id" : ObjectId("59828735c566b07b8733e77f"), "name" : "xiaomeng", "age" : 20, "sex" : 1 } > # 显示age大于22的文档 > db.t.find({age:{$gt:22}}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } > # 显示 age 不大于22的文档,也就是小于等于22的文档 > db.t.find({age:{$not:{$gt:22}}}) { "_id" : ObjectId("59827e4ec566b07b8733e77d"), "name" : "liuneng", "age" : 22, "sex" : 0 } { "_id" : ObjectId("59828735c566b07b8733e77f"), "name" : "xiaomeng", "age" : 20, "sex" : 1 } > |
5:nor
【语法】
1 |
{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] } |
【测试】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 所有文档 > db.t.find() { "_id" : ObjectId("59827e4ec566b07b8733e77d"), "name" : "liuneng", "age" : 22, "sex" : 0 } { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } { "_id" : ObjectId("59828735c566b07b8733e77f"), "name" : "xiaomeng", "age" : 20, "sex" : 1 } > # 显示age大于22或sex为1的文档 > db.t.find({$or:[{age:{$gt:22}}, {sex:1}]}) { "_id" : ObjectId("59827e62c566b07b8733e77e"), "name" : "zhaosi", "age" : 23, "sex" : 0 } { "_id" : ObjectId("59828735c566b07b8733e77f"), "name" : "xiaomeng", "age" : 20, "sex" : 1 } > # 显示 not (age大于22或sex为1) 的文档 > db.t.find({$nor:[{age:{$gt:22}}, {sex:1}]}) { "_id" : ObjectId("59827e4ec566b07b8733e77d"), "name" : "liuneng", "age" : 22, "sex" : 0 } > |