1.1 MongoDB 增加
1、插入数据
1. 插入文档: insert 如果插入数据的时候,collection还不存在,自动创建集合
2. insertOne: 插入一条数据
3. insertMany: 接收数组,插入多条文档
#1、插入单条数据
db.student1.insertOne({_id:"stu001","name":"Tom","age":25,grade:{"chinese":80,"math":90,"english":88}}) #2、插入多条数据
db.student1.insertMany([
{_id:"stu002","name":"Mary","age":23,grade:{"chinese":80,"math":90}},
{_id:"stu003","name":"Mike","age":23,grade:{"chinese":81,"math":90,"english":88}}
]);
2、更新文档: updateOne和updateMany
#1、更新_id=7839的薪水 ---> 8000
db.emp.updateOne({"_id":7839},{$set:{"sal":8000}})
db.emp.find({"_id":7839}) # 查询id=7893的文档 #2、更新多条数据:更新10号部门的员工薪水,加100块钱
错误:db.emp.updateMany({"deptno":{$eq:10}},{$set:{"sal":"sal"+100}}) ---> 不对
正确:db.emp.updateMany({"deptno":{$eq:10}},{$inc:{"sal",100}})
3、删除文档: deleteOne和deleteMany
db.emp.deleteOne({"_id":7839})
4、批处理
注: 为了提高效率,db.collection.bulkWrite,支持:insert update remove 同时也支持insertMany
db.mystudents.bulkWrite([
{insertOne:{"document":{"_id":100,"name":"Tom","age":25}}},
{insertOne:{"document":{"_id":101,"name":"Mary","age":24}}},
{updateOne:{"filter":{"_id":100},"update":{$set:{"name":"Tom123"}}}}
]);
1.2 MongoDB查询操作
1、基本查询
db.emp.insert(
[
{_id:7369,ename:'SMITH' ,job:'CLERK' ,mgr:7902,hiredate:'17-12-80',sal:800,comm:0,deptno:20},
{_id:7499,ename:'ALLEN' ,job:'SALESMAN' ,mgr:7698,hiredate:'20-02-81',sal:1600,comm:300 ,deptno:30},
{_id:7521,ename:'WARD' ,job:'SALESMAN' ,mgr:7698,hiredate:'22-02-81',sal:1250,comm:500 ,deptno:30},
{_id:7566,ename:'JONES' ,job:'MANAGER' ,mgr:7839,hiredate:'02-04-81',sal:2975,comm:0,deptno:20},
{_id:7654,ename:'MARTIN',job:'SALESMAN' ,mgr:7698,hiredate:'28-09-81',sal:1250,comm:1400,deptno:30},
{_id:7698,ename:'BLAKE' ,job:'MANAGER' ,mgr:7839,hiredate:'01-05-81',sal:2850,comm:0,deptno:30},
{_id:7782,ename:'CLARK' ,job:'MANAGER' ,mgr:7839,hiredate:'09-06-81',sal:2450,comm:0,deptno:10},
{_id:7788,ename:'SCOTT' ,job:'ANALYST' ,mgr:7566,hiredate:'19-04-87',sal:3000,comm:0,deptno:20},
{_id:7839,ename:'KING' ,job:'PRESIDENT',mgr:0,hiredate:'17-11-81',sal:5000,comm:0,deptno:10},
{_id:7844,ename:'TURNER',job:'SALESMAN' ,mgr:7698,hiredate:'08-09-81',sal:1500,comm:0,deptno:30},
{_id:7876,ename:'ADAMS' ,job:'CLERK' ,mgr:7788,hiredate:'23-05-87',sal:1100,comm:0,deptno:20},
{_id:7900,ename:'JAMES' ,job:'CLERK' ,mgr:7698,hiredate:'03-12-81',sal:950,comm:0,deptno:30},
{_id:7902,ename:'FORD' ,job:'ANALYST' ,mgr:7566,hiredate:'03-12-81',sal:3000,comm:0,deptno:20},
{_id:7934,ename:'MILLER',job:'CLERK' ,mgr:7782,hiredate:'23-01-82',sal:1300,comm:0,deptno:10}
]
);
创建emp表并插入14条数据
#1、查询所有的员工信息
db.emp.find() #2、查询职位值经理的员工
db.emp.find({"job":"MANAGER"}) #3、操作符:$in和$or
#查询职位是MANAGER或者是CLERK员工信息
db.emp.find({"job":{$in:["MANAGER","CLERK"]}})
db.emp.find({$or:[{"job":"MANAGER"},{"job":"CLERK"}]}) #4、查询10号部门工资大于2000的员工
db.emp.find({"sal":{$gt:2000},"deptno":10})
基本查询
2、查询嵌套文档
db.student2.insertMany([
{_id:"stu0001",name:"Mary",age:25,grade:{chinese:80,math:85,english:90}},
{_id:"stu0002",name:"Tom",age:25,grade:{chinese:86,math:82,english:95}},
{_id:"stu0003",name:"Mike",age:25,grade:{chinese:81,math:90,english:88}},
{_id:"stu0004",name:"Jerry",age:25,grade:{chinese:95,math:87,english:89}}
])
查询嵌套文档student2
#1、查询语文是81分,英语成绩是88分的文档
db.student2.find({grade:{chinese:81,english:88}}) ---> 得不到结果 #2、查询语文是81分,数学90分,英语成绩是88分的文档
db.student2.find({grade:{chinese:81,math:90,english:88}}) ---> 得到结果
# { "_id" : "stu0003", "name" : "Mike", "age" : 25, "grade" : { "chinese" : 81, "math" : 90, "english" : 88 } }
小结:如果是相等查询,保证匹配所有的field,顺序也要一样 #3、查询嵌套文档中的一个列:查询数学成绩是82分的文档
db.student2.find({"grade.math":82}) #4、使用比较运算符:查询英语成绩大于88分文档
db.student2.find({"grade.english":{$gt:88}}) #5、使用AND运算符:查询英语成绩大于88分,语文成绩大于85分的文档
db.student2.find({"grade.english":{$gt:88},"grade.chinese":{$gt:85}})
查询嵌套文档
3、查询数组文档
db.studentbook.insert([
{_id:"stu001",name:"Tom",books:["Hadoop","Java","NoSQL"]},
{_id:"stu002",name:"Mary",books:["C++","Java","Oracle"]},
{_id:"stu003",name:"Mike",books:["Java","MySQL","PHP"]},
{_id:"stu004",name:"Jerry",books:["Hadoop","Spark","Java"]},
{_id:"stu005",name:"Jone",books:["C","Python"]}
])
查询数组文档studentbook
#1、查询所有有Hadoop和Java的文档
错误:db.studentbook.find({books:["Hadoop","Java"]}) ---> 没有结果
正确:db.studentbook.find({books:{$all:["Hadoop","Java"]}})
'''
{ "_id" : "stu001", "name" : "Tom", "books" : [ "Hadoop", "Java", "NoSQL" ] }
{ "_id" : "stu004", "name" : "Jerry", "books" : [ "Hadoop", "Spark", "Java" ] }
''' #2、根查询嵌套的文档一样,匹配每个元素,顺序也要一致
db.studentbook.find({books:["Hadoop","Java","NoSQL"]})
'''
{ "_id" : "stu001", "name" : "Tom", "books" : [ "Hadoop", "Java", "NoSQL" ] }
'''
查询数组文档
4、查询数组中嵌套的文档
db.studentbook1.insertMany([
{_id:"stu001",name:"Tome",books:[{"bookname":"Hadoop", quantity:2},{"bookname":"Java", quantity:3},{"bookname":"NoSQL", quantity:4}]},
{_id:"stu002",name:"Mary",books:[{"bookname":"C++", quantity:4}, {"bookname":"Java", quantity:3},{"bookname":"Oracle", quantity:5}]},
{_id:"stu003",name:"Mike",books:[{"bookname":"Java", quantity:4}, {"bookname":"MySQL", quantity:1},{"bookname":"PHP", quantity:1}]},
{_id:"stu004",name:"Jone",books:[{"bookname":"Hadoop", quantity:3},{"bookname":"Spark", quantity:2},{"bookname":"Java", quantity:4}]},
{_id:"stu005",name:"Jane",books:[{"bookname":"C", quantity:1}, {"bookname":"Python", quantity:5}]}])
查询数组中嵌套文档studentbook1
#1、查询Java有4本的文档
db.studentbook1.find({books:{"bookname":"Java","quantity":4}})
'''
{
"_id": "stu003",
"name": "Mike",
"books": [{
"bookname": "Java",
"quantity": 4
}, {
"bookname": "MySQL",
"quantity": 1
}, {
"bookname": "PHP",
"quantity": 1
}]
}
''' #2、指定查询的条件:查询数组中第一个元素大于3本的文档
db.studentbook1.find({"books.0.quantity":{$gt:3}})
'''
{
"_id": "stu002",
"name": "Mary",
"books": [{
"bookname": "C++",
"quantity": 4
}, {
"bookname": "Java",
"quantity": 3
}, {
"bookname": "Oracle",
"quantity": 5
}]
}
''' #3、如果不知道field的位置: 查询文档中至少有一个quantity的值大于3
db.studentbook1.find({"books.quantity":{$gt:3}}) #4、查询Java等于4本的文档
db.studentbook1.find({"books":{$elemMatch:{"bookname":"Java","quantity":4}}})
'''
{
"_id": "stu003",
"name": "Mike",
"books": [{
"bookname": "Java",
"quantity": 4
}, {
"bookname": "MySQL",
"quantity": 1
}, {
"bookname": "PHP",
"quantity": 1
}]
}
'''
查询数组中嵌套文档
5、查询空值null或者缺失的列
db.student3.insertMany([
{ _id: 1,name:"Tom",age:null },
{ _id: 2,name:"Mary"}
])
查询null或缺失的列
#1、查询值为null的文档
db.student3.find({age:null}) ---> 返回两条记录
'''
{ "_id" : 1, "name" : "Tom", "age" : null }
{ "_id" : 2, "name" : "Mary" }
''' #2、只返回null的记录:BSON表示null:10
db.student3.find({"age":{$type:10}})
'''
{ "_id" : 1, "name" : "Tom", "age" : null }
''' #3、检查是否缺失某个列
db.student3.find({age:{$exists:false}})
db.student3.find({age:{$exists:true}})
查询空值null或者缺失的列
6、使用游标
#1、定义游标
var mycursor = db.emp.find()
mycursor #2、使用游标访问文档(打印json格式数据)
var mycursor = db.emp.find()
while(mycursor.hasNext()){
printjson(mycursor.next())
} #3、游标和数组
var mycursor = db.emp.find() # 定义一个游标
var myarray = mycursor.toArray() # 将查询结果转换成数组
var mydoc = myarray[3] # 取出数组中第3条数据 #4、分页操作
第一页: limit表示查询过滤出前5条数据
var mycursor = db.emp.find().limit(5)
第二页: skip(5)表示跳过多少条数据
var mycursor = db.emp.find().limit(5).skip(5)
使用游标
1.3 聚合操作:aggregation
1、聚合操作说明
1. Pipeline:速度快于MapReduce,单个的聚合操作耗费的内存不能超过20%,返回的结果集:限制在16M
2. MapReduce:多个Server上并行计算
2、Pipeline聚合操作
#1、$match和$project
$match: 过滤进入PipeLine的数据
$project:指定提取的列,其中: 1表示提取列 0不提取
#查询部门id=10,只显示ename、sal、deptno
db.emp.aggregate(
{$match:{"deptno":{$eq:10}}},
{$project:{"ename":1,"sal":1,"deptno":1}}
);
'''
{ "_id" : 7782, "ename" : "CLARK", "sal" : 2450, "deptno" : 10 }
{ "_id" : 7839, "ename" : "KING", "sal" : 8000, "deptno" : 10 }
{ "_id" : 7934, "ename" : "MILLER", "sal" : 1300, "deptno" : 10 }
'''
$match和$project,只显示指定列
db.emp.aggregate(
{$project:{"sal":1,"deptno":1}},
{$group:{"_id":"$deptno",salTotal:{$sum:"$sal"}}}
); '''
{ "_id" : 10, "salTotal" : 11750 }
{ "_id" : 30, "salTotal" : 9400 }
{ "_id" : 20, "salTotal" : 10875 }
'''
使用$group: 求每个部门的工资总额
#3、按照部门,不同的职位求工资总额
#select deptno,job,sum(sal) from emp group by deptno,job;
db.emp.aggregate(
{$project:{"job":1,"sal":1,"deptno":1}},
{$group:{"_id":{"deptno":"$deptno","job":"$job"},salTotal:{$sum:"$sal"}}}
);
'''
{ "_id" : { "deptno" : 20, "job" : "ANALYST" }, "salTotal" : 6000 }
{ "_id" : { "deptno" : 30, "job" : "SALESMAN" }, "salTotal" : 5600 }
{ "_id" : { "deptno" : 20, "job" : "CLERK" }, "salTotal" : 1900 }
'''
按照部门,不同的职位求工资总额
111111111111111111111111