第一部分 前期准备
1.1 插入测试数据
db.test1.insertMany([
{"name": "zhangsan", "age": 19, "score": [90, 80]},
{"name": "lisi", "age": 29, "score": [40, 60]},
{"name": "xiaoming", "age": 18, "score": [20, 40]},
{"name": "xiaohong"}
])
第二部分 Find
2.1 Find查询数据的语法格式如下:
> db.collection.find(query, projection)
1. query[可选]: 主要是一些筛选条件, 文档类型数据;
2. projection[可选]: 返回的限制字段, 文档类型.
2.2 find 筛选全部数据
第一种, 罗列全部数据
> db.test1.find()
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ] }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2a"), "name" : "lisi", "age" : 29, "score" : [ 40, 60 ] }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
第二种 筛选某一条数据
> db.test1.find({"name": "xiaoming"})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
第三种 指定返回字段
1或true
代表显示, 0或false
代表不显示
> db.test1.find({"name": "xiaoming"}, {"age": 1, "name": 1})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18 }
第三部分 查询选择器
3.1 比较选择器
[eq: equal, g: greater, l: less, t: than, n: not, in: in]
1. $eq : 对比值等于指定值
2. $gt : ...大于...
3. $gte: ...大于等于...
4. $lt: ...大于...
5. $lte: ...小于等于...
6. $ne : 对比值 全部不等于 指定值
7. $in: 指定数组包含对比值
8. $nin: 指定数组不包含对比值
3.1.1 $eq
配置键值对[两种写法]
#eq> db.test1.find({"age": {"$eq": 18}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }#省略, 结果一样> db.test1.find({"age": 18})
匹配数组元素
> db.test1.find({"score": {"$eq": 80}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }#省略, 结果一样> db.test1.find({"score": 80})
匹配数组
#无结果, 因为在此是相当于 db.test1.find({"score": {"$eq": [20, 30]}})> db.test1.find({"score": [20, 30]})
> db.test1.find({"score": [20, 40]})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
3.1.2 $gt 除了数字可以比较之外, 不能忽略BJSON其他类型都可以比较
数字
> db.test1.find({"age": {"$gt": 20}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2a"), "name" : "lisi", "age" : 29, "score" : [ 40, 60 ] }
字符串同样可以比较更多参考BSON Types
> db.test1.find({"name": {"$gt": "lisi"}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
3.1.3 $gte
> db.test1.find({"age": {"$gte": 19}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2a"), "name" : "lisi", "age" : 29, "score" : [ 40, 60 ] }
3.1.4. $lt
> db.test1.find({"age": {"$lt": 19}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
3.1.5. $lte
> db.test1.find({"age": {"$lte": 19}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
3.1.6 $ne 不等于指定值
> db.test1.find({"name": {"$ne": "xiaoming"}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2a"), "name" : "lisi", "age" : 29, "score" : [ 40, 60 ] }
3.1.7 $in 符合指定数组内的任一个即满足
> db.test1.find({"score": {"$in": [90, 100]}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
> db.test1.find({"score": {"$in": [89, 100]}})
3.1.8 $nin 指定数据都不属于指定数组的子集
#三条数据全部不存在89 100, 就输出> db.test1.find({"score": {"$nin": [89, 100]}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2a"), "name" : "lisi", "age" : 29, "score" : [ 40, 60 ] }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
3.2 逻辑选择器
1. $or: 在一个数组上(二个或更多)执行逻辑||, 满足至少一个, 就算是满足2. $and: 逻辑 &&3. $not: 指定字段不匹配操作表达式, 或不存在该字段4. $nor: $not 与 $or结合体
3.2.1 $or
语法
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
> db.test1.find({"$or": [{"age": 18}, {"name": "zhangsan"}]})#满足{"name": "zhangsan"}, 输出{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }#满足{"age": 18}, 输出{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
$or
与$in
的异同
3.2.2 $and
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
#没有满足的> db.test1.find({"$and": [{"age": 18}, {"name": "zhangsan"}]})#同时满足全部条件: 1.{"age": 18} 2.{"name": "xiaoming"}> db.test1.find({"$and": [{"age": 18}, {"name": "xiaoming"}]})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
3.2.3 $not
{ field: { $not: { <operator-expression> } } }
执行$not
> db.test1.find({"age": {"$not":{"$gt": 19}}})
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [ 90, 80 ], "qty" : 10 }
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
{ "_id" : ObjectId("58c9e4b80c4d4cf6f4563b26"), "name" : "xiaohong" }
{"age": {"$not":{"$gt": 19}}}
筛选的数据:
1. 小于等于19;
2. 不存在age字段
3.2.4. $nor
{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
备选数据
> db.test1.find().pretty()
{ "_id" : ObjectId("58c8dc54ef9b994487420f29"), "name" : "zhangsan", "age" : 19, "score" : [
90,
80
], "qty" : 10
}
{ "_id" : ObjectId("58c8dc54ef9b994487420f2a"), "name" : "lisi", "age" : 29, "score" : [
40,
60
]
}
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [
20,
40
]
}
{ "_id" : ObjectId("58c9e4b80c4d4cf6f4563b26"), "name" : "xiaohong" }
执行$nor
> db.test1.find({"$nor": [{"age": {"$gt": 20}}, {"qty": 10}]})
{ "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming", "age" : 18, "score" : [ 20, 40 ] }
{ "_id" : ObjectId("58c9e4b80c4d4cf6f4563b26"), "name" : "xiaohong" }
分解上述执行过程
1. age小于等于20, qty不等于10;
2. age小于等于20, qty字段不存在;
3. age字段不存在, qty不等于10;
4. age字段不存在, qty字段不存在;
Tip
1. db.collection.count(): 集合内对象个数;
2. db.collection.dataSize(): 集合所占大小;
更多精彩内容请关注“IT实战联盟”公众号哦~~~
注意:本文归作者所有,未经作者允许,不得转载