练习一:简单增删改查
//1.进入my_test数据库 use my_test //2.向数据库的user集合中插入一个文档 db.users.insert({ username:"sunwukong" }); //3.查询user集合中的文档 db.users.find(); //4.向数据库的user集合中插入一个文档 db.users.insert({ username:"zhubajie" }); //5.查询数据库user集合中的文档 db.users.find(); //6.统计数据库user集合中的文档数量 db.users.find().count(); //7.查询数据库user集合中username为sunwukong的文档 db.users.find({username:"sunwukong"}); //8.向数据库user集合中的username为sunwukong的文档,添加一个address属性,属性值为huaguoshan db.users.update({username:"sunwukong"},{$set:{address:"huaguoshan"}}); //9.使用{username:"tangseng"} 替换 username 为 zhubajie的文档 db.users.replaceOne({username:"zhubajie"},{username:"tangseng"}); //10.删除username为sunwukong的文档的address属性 db.users.update({username:"sunwukong"},{$unset:{address:1}}); //11.向username为sunwukong的文档中,添加一个hobby:{cities:["beijing","shanghai","shenzhen"] , movies:["sanguo","hero"]} //MongoDB的文档的属性值也可以是一个文档,当一个文档的属性值是一个文档时,我们称这个文档叫做 内嵌文档 db.users.update({username:"sunwukong"},{$set:{hobby:{cities:["beijing","shanghai","shenzhen"] , movies:["sanguo","hero"]}}}); db.users.find(); //12.向username为tangseng的文档中,添加一个hobby:{movies:["A Chinese Odyssey","King of comedy"]} db.users.update({username:"tangseng"},{$set:{hobby:{movies:["A Chinese Odyssey","King of comedy"]}}}) //13.查询喜欢电影hero的文档 //MongoDB支持直接通过内嵌文档的属性进行查询,如果要查询内嵌文档则可以通过.的形式来匹配 //如果要通过内嵌文档来对文档进行查询,此时属性名必须使用引号 db.users.find({'hobby.movies':"hero"}); //14.向tangseng中添加一个新的电影Interstellar //$push 用于向数组中添加一个新的元素 //$addToSet 向数组中添加一个新元素 , 如果数组中已经存在了该元素,则不会添加 db.users.update({username:"tangseng"},{$push:{"hobby.movies":"Interstellar"}}); db.users.update({username:"tangseng"},{$addToSet:{"hobby.movies":"Interstellar"}}); db.users.find(); //15.删除喜欢beijing的用户 db.users.remove({"hobby.cities":"beijing"}); //16.删除user集合 db.users.remove({}); db.users.drop(); show dbs; //17.向numbers中插入20000条数据 7.2s for(var i=1 ; i<=20000 ; i++){ db.numbers.insert({num:i}); } db.numbers.find() db.numbers.remove({}); //0.4s var arr = []; for(var i=1 ; i<=20000 ; i++){ arr.push({num:i}); } db.numbers.insert(arr);
练习二:条件查询
//18.查询numbers中num为500的文档 db.numbers.find({num:500}) //19.查询numbers中num大于5000的文档 db.numbers.find({num:{$gt:500}}); db.numbers.find({num:{$eq:500}}); //20.查询numbers中num小于30的文档 db.numbers.find({num:{$lt:30}}); //21.查询numbers中num大于40小于50的文档 db.numbers.find({num:{$gt:40 , $lt:50}}); //22.查询numbers中num大于19996的文档 db.numbers.find({num:{$gt:19996}}); //23.查看numbers集合中的前10条数据 db.numbers.find({num:{$lte:10}}); //limit()设置显示数据的上限 db.numbers.find().limit(10); //在开发时,我们绝对不会执行不带条件的查询 db.numbers.find(); //24.查看numbers集合中的第11条到20条数据 /* 分页 每页显示10条 1-10 0 11-20 10 21-30 20 。。。 skip((页码-1) * 每页显示的条数).limit(每页显示的条数); skip()用于跳过指定数量的数据 MongoDB会自动调整skip和limit的位置 */ db.numbers.find().skip(10).limit(10); //25.查看numbers集合中的第21条到30条数据 db.numbers.find().skip(20).limit(10); db.numbers.find().limit(10).skip(10);
练习三:文档间关系查询练习
dept:
{ "_id" : ObjectId("5941f2bac1bc86928f4de49a"), "deptno" : 10.0, "dname" : "财务部", "loc" : "北京" } { "_id" : ObjectId("5941f2bac1bc86928f4de49b"), "deptno" : 20.0, "dname" : "办公室", "loc" : "上海" } { "_id" : ObjectId("5941f2bac1bc86928f4de49c"), "deptno" : 30.0, "dname" : "销售部", "loc" : "广州" } { "_id" : ObjectId("5941f2bac1bc86928f4de49d"), "deptno" : 40.0, "dname" : "运营部", "loc" : "深圳" }
emp:
{ "_id" : ObjectId("5941f5bfc1bc86928f4de4ac"), "empno" : 7369.0, "ename" : "林冲", "job" : "职员", "mgr" : 7902.0, "hiredate" : ISODate("1980-12-16T16:00:00Z"), "sal" : 800.0, "depno" : 20.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4ad"), "empno" : 7499.0, "ename" : "孙二娘", "job" : "销售", "mgr" : 7698.0, "hiredate" : ISODate("1981-02-19T16:00:00Z"), "sal" : 1600.0, "comm" : 300.0, "depno" : 30.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4ae"), "empno" : 7521.0, "ename" : "扈三娘", "job" : "销售", "mgr" : 7698.0, "hiredate" : ISODate("1981-02-21T16:00:00Z"), "sal" : 1250.0, "comm" : 500.0, "depno" : 30.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4af"), "empno" : 7566.0, "ename" : "卢俊义", "job" : "经理", "mgr" : 7839.0, "hiredate" : ISODate("1981-04-01T16:00:00Z"), "sal" : 2975.0, "depno" : 20.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b0"), "empno" : 7654.0, "ename" : "潘金莲", "job" : "销售", "mgr" : 7698.0, "hiredate" : ISODate("1981-09-27T16:00:00Z"), "sal" : 1250.0, "comm" : 1400.0, "depno" : 30.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b1"), "empno" : 7698.0, "ename" : "西门庆", "job" : "经理", "mgr" : 7839.0, "hiredate" : ISODate("1981-04-30T16:00:00Z"), "sal" : 2850.0, "depno" : 30.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b2"), "empno" : 7782.0, "ename" : "柴进", "job" : "经理", "mgr" : 7839.0, "hiredate" : ISODate("1981-06-08T16:00:00Z"), "sal" : 2450.0, "depno" : 10.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b3"), "empno" : 7788.0, "ename" : "公孙胜", "job" : "分析师", "mgr" : 7566.0, "hiredate" : ISODate("1987-07-12T16:00:00Z"), "sal" : 3000.0, "depno" : 20.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b4"), "empno" : 7839.0, "ename" : "宋江", "job" : "董事长", "hiredate" : ISODate("1981-11-16T16:00:00Z"), "sal" : 5000.0, "depno" : 10.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b5"), "empno" : 7844.0, "ename" : "阎婆惜", "job" : "销售", "mgr" : 7698.0, "hiredate" : ISODate("1981-09-07T16:00:00Z"), "sal" : 1500.0, "comm" : 0.0, "depno" : 30.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b6"), "empno" : 7876.0, "ename" : "李逵", "job" : "职员", "mgr" : 7902.0, "hiredate" : ISODate("1987-07-12T16:00:00Z"), "sal" : 1100.0, "depno" : 20.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b7"), "empno" : 7900.0, "ename" : "武松", "job" : "职员", "mgr" : 7782.0, "hiredate" : ISODate("1981-12-02T16:00:00Z"), "sal" : 950.0, "depno" : 10.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b8"), "empno" : 7902.0, "ename" : "吴用", "job" : "分析师", "mgr" : 7566.0, "hiredate" : ISODate("1981-12-02T16:00:00Z"), "sal" : 3000.0, "depno" : 20.0 } { "_id" : ObjectId("5941f5bfc1bc86928f4de4b9"), "empno" : 7934.0, "ename" : "鲁智深", "job" : "职员", "mgr" : 7782.0, "hiredate" : ISODate("1982-01-22T16:00:00Z"), "sal" : 1300.0, "depno" : 10.0 }
将dept和emp集合导入到数据库中
//26.将dept和emp集合导入到数据库中 db.dept.find() db.emp.find() //27.查询工资小于2000的员工 db.emp.find({sal:{$lt:2000}}); //28.查询工资在1000-2000之间的员工 db.emp.find({sal:{$lt:2000 , $gt:1000}}); //29.查询工资小于1000或大于2500的员工 db.emp.find({$or:[{sal:{$lt:1000}} , {sal:{$gt:2500}}]}); //30.查询财务部的所有员工 //(depno) var depno = db.dept.findOne({dname:"财务部"}).deptno; db.emp.find({depno:depno}); //31.查询销售部的所有员工 var depno = db.dept.findOne({dname:"销售部"}).deptno; db.emp.find({depno:depno}); //32.查询所有mgr为7698的所有员工 db.emp.find({mgr:7698}) //33.为所有薪资低于1000的员工增加工资400元 db.emp.updateMany({sal:{$lte:1000}} , {$inc:{sal:400}}); db.emp.find()