目录
一、课程大纲
二、MongoDB 条件查询
1. 制造 MongoDB 测试数据
2. MongoDB 数据查询 与 SQL对应关系
3. MongoDB 查询运算符
4. MongoDB 数据查询、条件查询、过滤
5. MongoDB 条件查询命令
6. MongoDB 数据查询数组条件
A. 精确匹配数组元素:
B. 无顺序 and 精确 匹配
C. 至少匹配一个
D. 组合条件满足一个条件过滤
E. $elemMatch操作符查询内嵌文档
F. 正则:模糊查询:/.* .*/
G. 正则:以xx开头:/^ .*/
H. 正则:以xx结尾:/.* $/
7. MongoDB 数据查询嵌套数组文档
A. 文档嵌套:单条件查询
B. 文档嵌套:多条件查询(and)
三、映射Project 与 过滤
四、去除重复 Distinct
五、聚合分组统计 Group By
A. 三种方式
B:聚合函数 ( 了解即可 )
注意:4.2.1中已经不支持 group( db.users.help() 查询没有group函数 )
1. 分组统计不同年龄段的用户
2. 分组统计不同用户的订单总额
C:聚合管道
D:MapReduce (非常适用于大数据分析,如基于java的hadoop)
db.users.insert({name:'frank',age:16,city:'beijing' )
db.users.insert({name:'xulei',age:17,city:'hangzhou' )
db.users.insert(name:'frankxulei'age:18,city:'shanghai')
db.users.insert({name:'alibaba',age: 25,tags:['java','mysql','mongodb'],city:'hangzhou'})
db.users.save()
db.users.insertOne()
db.users.insertMany()
MongoDB | SQL语句 | 说明 |
find() | all | |
find({}) | all | |
find({fage:18}) | select * from users where age =18 | |
$lt:18 | where age<18 | less rhan |
$gt:18 | where age>18 | greater than |
$lte:18 | where age<=18 | less rhan or equal |
$gte=:18 | where age>=18 | greater than or equal |
$in:[18,19] | where age in(18,19) | |
$or:[{age:18}, { name:' frankxulei'}] | where age=18 or name= 'frankxuleil' | |
{age:18,name:'frankxulei'} | where age = 18 and name= 'frankxulei' | |
({“name”:/.*阿里.*/}) | select * from users where name like “%阿里%” |
db.users.find( { tags:["java","mongodb"]})
db.users.find( { tags:{$all:["java","mongodb"]}})
db.users.find( { tags:"java"})
db.users.find( { age: { $gt: 15, $lt: 20 }})
db.test.insert({"id":1, "members":[{"name":"BuleRiver1", "age":27, "gender":"M"}, {"name":"BuleRiver2", "age":23, "gender":"F"}, {"name":"BuleRiver3", "age":21, "gender":"M"}]});
db.test.find({"members":{"$elemMatch":{"name":"BuleRiver1", "age":27}}});
db.test.find({"members":{"$elemMatch":{"name":"BuleRiver1", "age":23}}});
db.users.find( { name:/.*feng.*/ })
db.users.find( { name:/^zhang.*/ })
db.users.find( { name:/.*g$/ })
db.users.insert([
{name:'ali',age:51,address:{country:'china',city:'beijing',district:'haidian',dd:'zhongguancunroad 10'}}
{name:'baby',age:51,address:{country:'china',city:'hangzhou',district:'xihu',dd:'zhongguancunroad 10'}}
{name:'baby2',age:17,address:{country:'china',city:'hangzhou',district:'xihu',dd:'zhongguancunroad 10'}}
{name:'baby3',age:18,address:{country:'china',city:'hangzhou',district:'xihu',dd:'zhongguancunroad 10'}}
{name:'xiaowang',age:19,address:{country:'china',city:'shanghai',district:'pudong',dd:'sjtu park 10'}}
])
db.orders.insert([
{name:"frank",items:[{title:"mongodb",count:1},{title:"java",count:3}]},
{name:"xulei",items:[{title:"iphone 8p",count:2}]},
{name:"wangsicong",items:[{title:"RR",count :1},{title:"Benz",count:10}]}]);
db.users.find({"address.country":'china'})
db.users.find({"address.country":'china',"address.city":"hangzhou",age:{$gte:18}})
作用:对查询结果的过滤
作用:对查询结果去重,并不会删除原文档
db.users.distinct("city")
db.users.distinct("address.city")
db.users.group(
{
key: { age: 1 },
cond:{ age:{$gt:16 }},
reduce:function(curr, result) {
result.total += 1;
},
initial: { total : 0 }
}
)