本文章由博主学习阿里云MongoDB从入门到高级云上开发实战课程后整理的笔记,访问超链接即可了解更多内容
文章目录
2. MongDB数据查询语句与聚合分析
2.1 MongoDB条件查询
客户端一般考虑到数据规模,一般返回20条
下表演示了一些基本的操作:
方法名称 | 注释 | 含义 |
---|---|---|
find() | 查询所有记录 | |
find({}) | {}表示空文档 | 查询所有文档 |
find({age:18}) | 查询年龄为18的数据 | 按照{}内的要求查询 |
$lt:18 | lt为less than的缩写 | age < 18 |
$gt:18 | gt为graet than的缩写 | age > 18 |
$lte:18 | age <= 18 | |
$gte=:18 | age >= 18 | |
$in:[18,19] | age in (18,19) | |
$or:[{age:19},{name:‘Jack’}] | or | 满足一个条件即可 |
{age:19,name:‘Jack’} | and | 需要条件全部满足即可 |
({“name”: /.阿里./}) | 模糊搜索 |
以下为代码实例
db.users.find()
db.users.find({})
db.users.find({age:18})
db.users.find({age:{$lt: 18}})
db.users.find({age:{$gt: 18}})
db.users.find({age:{$lte:18}})
db.users.find({age:{$gte:18}})
db.users.find({age:{$in:[18,19]}})
db.users.find({$or:[{age: 18},{name:'l frankxulei' }]})
db.users find({"name": /.*f.*/})
db.users.find({name:null})
db.users.find({age:{$exists: false}})
db.users.find({age:{$lt:18}},{name:1,age:0}) //投影
db.users.find().sort({age:1}) //1表示从小到大,-1表示从大到小
2.1.1 数组元素的匹配查询
- 精确匹配数组元素:
db.users.find( { tags: ["java", "mongodb"]} )
- 无顺序and精确匹配:
db.users.find( { tags: {$all:["java","mongodb"]}} )
- 至少匹配一个:
db.users.find( { tags: "java" })
- 组合条件满足一个条件过滤:
db.users.find( { age: { $gt: 15, $lt: 20}})
- 组合条件同时满足条件过滤:
db.users.find( { age: { $elemMatch: { $gt: 22, $lt: 30}}})
2.1.2 模糊匹配查询
- 只要求包含关键字
db.users find({"name": /.*f.*/})
- 要求关键字在开头
db.users find({"name": /^f.*/})
- 要求关键字在结尾
db.users find({"name": /.*f.$/})
2.1.3 MongoDB数据查询嵌套数组文档
我们以下数据为例子:
{ "_id" : ObjectId("5e3d5abdc8d00208cdfc8f8e"), "name" : "frank", "items" : [ { "title" : "mongodb", "count" : 1 }, { "title" : "java", "count" : 3 } ] }
{ "_id" : ObjectId("5e3d5abdc8d00208cdfc8f8f"), "name" : "xulei", "items" : [ { "title" : "iphone 8p", "count" : 2 } ] }
{ "_id" : ObjectId("5e3d5abdc8d00208cdfc8f90"), "name" : "wangsicong", "items" : [ { "title" : "RR", "count" : 1 }, { "title" : "Benz", "count" : 10 } ] }
- 精确匹配:
db.orders.find( { "items": { title: "java", count:3 }})
- Or匹配:
db.orders.find( { $or: [ {'items.title' : 'java'}, { 'items.count':1}]})
- 任意元素满足条件的:
db.orders.find( { 'items.count':{ $It: 2}} )
db.orders.find( { 'items.count':{ $gte: 20}})
- 第0个元素满足条件的:
db.orders.find( { 'items.0.count':{ $It: 2}})
db.orders.find( { 'items.0.count':{ $gte: 20}})
- 多条件并且and查询:
db.orders.find( { "items.count": { $gt: 0, $Ite: 100}})
2.2 映射Project与过滤
在查询过程中,我们可以通过以下语句来查询符合要求的字段,并返回其全部内容
- 返回所有字段:
//users is table name
db.users.find()
db.users.find({age:18})
db.users.find({age: { $It: 18 }})
db.users.find({age: { $gt: 18 }})
当然,在一些特殊情况下,我们只需要返回字段中的部分内容,那么我们可以用映射文档参数来实现,其中1表示选取,0表示不选取
- 返回特定字段:
db.users.find()
db.users.find({age:18}, {name:1,age:1})
db.users.find({age: { $It: 18 }} ,{name:1,age:1})
- 排除字段:
db.users.find({age: { $gt: 18 }}, {city:0})
2.3 去除重复Distinct
利用该操作,可以返回查询字段去重后的结果集
- 查询所有数据去重:
db.users.distinct("name")
db.users.distinct("item.count")
- 条件查询数据后去重:
db.users.distinct("name“, {age:18} )
db.users.distinct( "item.count ", {age:18} )
- 等价命令:
db.runCommand ( { distinct: "orders", key: "name" })
2.4 分组统计Group By
2.4.1 MongoDB聚合分析方式
MongoDB的聚合分析方式主要有以下三种:
2.4.2 MongoDB聚合分组统计GroupBy
我们首先使用Group进行聚合分析,以以下句子为例:
db.collection.group({ key, reduce, initial [, keyf] [, cond] [, finalize] })
以下是方法参数:
key: 指的是选取的字段
reduce: 削减,目的是从原始数据中统计出有效数据,把结果集进行简化
inital: 初始化
分组db.collection.group()
- 3.4版本后过期,建议使用新的聚合: db.collection.aggregate() + $group或者db.collection.mapReduce()
- 2.2版本超过2万个文档,建议使用: db.collection.mapReduce()
- 2.4版本发生db.collection.group() JavaScript lock ,阻塞其他JS执行
db.users.group(
{
key: { name:1,age: 1,city:1 },
cond:{ },
reduce: function ( curr, result){},
initial: {}
}
}
2.4.2.1 MongoDB聚合分析:分组统计不同年龄段的用户
db.users.group(
{
key: {age: 1},
cond:{age:{$gt:16}},
reduce: function (curr, result){
result.total += 1;
},
initial: {total:0}
}
}
2.4.2.1 MongoDB聚合分析:分组统计不同用户的订单总额
db.users.group(
{
key: {name: 1},
cond:{},
reduce: function (curr, result){
result.total += curr.price;
},
initial: {total:0}
}
)