赞
踩
相同点:
不同点:
MongoDB是什么
MongoDB是一个基于分布式文件存储的数据库,由c++语言编写,旨在为WEB应用提供可拓展的高性能数据存储解决方案。
MongoDB是一个介于关系型数据库和非关系型数据库之间的产品,是非关系数据库当中功能最丰富,最像关系型数据库的。
json示例
{id:1,name:张三,age=20}
MongoDB功能
如图
以管理员身份运行cmd窗口,切换路径至bin目录下
运行命令mongod --install --dbpath D:\mongoDB\zip\mongodb-windows-x86_64-4.4.6\mongodb-windows-4.4.6\data --logpath D:\mongoDB\zip\mongodb-windows-x86_64-4.4.6\mongodb-windows-4.4.6\log\mongodb.log
解释下:
--dbpath D:\mongoDB\zip\mongodb-windows-x86_64-4.4.6\mongodb-windows-4.4.6\data
是指你的数据存放的路径,是上一步我们创建的包data的路径
--logpath D:\mongoDB\zip\mongodb-windows-x86_64-4.4.6\mongodb-windows-4.4.6\log\mongodb.log
是日志包的路径加上日志文件(自己命名一个,不用创建)
回车,没有cmd任何输出
去此电脑右键->管理->服务和应用程序->服务
找到mongoDB服务
如图
net start mongodb
MongoDB 服务正在启动 .
MongoDB 服务已经启动成功。
那么如果不成功怎么办?
举一个作者踩雷的例子
我把mongodb解压到中文的路径下了,在管理的服务中看到mongodb服务的属性下的可执行路径有中文乱码,然后执行net start
mongodb,报找不到可执行文件的错误(好像是这个记不清了)
如何解决
mongod.exe --remove --serviceName "MongoDB"
,删除掉这个服务(前提cmd是管理员身份打开)进入数据库
bin目录下开启服务(net start mongodb
)后,输入mongo
进入数据库
show databases
(详写)> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
show dbs
(简写)> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
选择数据库
user 数据库名
> use admin
switched to db admin
创建数据库(隐式创建)
user 数据库名
> use qq
switched to db qq
但是为什么show dbs看不到这个qq数据库呢?
因为虽然qq数据库已经创建了,但是qq数据库是空的,所以看不到,后面只要插入一点数据,就能看到了。
这里的集合类似于关系型数据库的表
show collections
> show collections
db.createCollection("集合名")
> db.createCollection('c1')
{ "ok" : 1 }
此时我们再次查看数据库时,发现qq数据库已经存在了
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
qq 0.000GB
同样查询qq数据库的集合也能查到集合了
> show collections
c1
db.集合名.drop()
> db.c1.drop()
true
> show collections
use 数据库名
db.dropDatabase()
> use qq
switched to db qq
> db.dropDatabase()
{ "dropped" : "qq", "ok" : 1 }
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
明确需求
数据库主要用来存放项目数据
我们已经学会了数据库和集合的创建
思考:如何实现集合中数据的增删改查呢?
回答:通过MongoDB语法即可
单条插入
实例
> use qq
switched to db qq
> show collections
> db.users.insert({name:"zhangsan",age:20})
WriteResult({ "nInserted" : 1 })
小贴士:
1.数据库和集合不存在就隐式创建
2.对象的键统一不加引号方便查看,但是在查看集合数据时系统会自动添加
3.mongodb会给每条数据增加一个全球唯一的_id键,当然这个键你是可以在添加时,写这个的,但是实战极其不推荐!!!
举个例子
db.users.insert({_id:123,name:“lisi”,age:21})
WriteResult({ “nInserted” : 1 })
db.users.find()
{ “_id” : ObjectId(“60a3bd32e98e23691411768e”), “name” : “zhangsan”, “age” : 20 }
{ “_id” : 123, “name” : “lisi”, “age” : 21 }
多条插入
db.集合名.insert([{},{},{}...])
实例
> db.users.insert([{name:"xiaoming",age:18},{name:"xiaogang",age:21},{name:"xiaohong",age:19}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.users.find() { "_id" : ObjectId("60a3bd32e98e23691411768e"), "name" : "zhangsan", "age" : 20 } { "_id" : 123, "name" : "lisi", "age" : 21 } { "_id" : ObjectId("60a3c022e98e23691411768f"), "name" : "xiaoming", "age" : 18 } { "_id" : ObjectId("60a3c022e98e236914117690"), "name" : "xiaogang", "age" : 21 } { "_id" : ObjectId("60a3c022e98e236914117691"), "name" : "xiaohong", "age" : 19 } >
小拓展
> for(var i=1;i<=10;i++){
... print(i)
... }
1
2
3
4
5
6
7
8
9
10
你明白了吗??!!
意思就是说你可以循环插入多条数据
实例
> use qq switched to db qq > db.createCollection('c2') { "ok" : 1 } > show collections c2 users > for(var i=1;i<=10;i++){ ... db.c2.insert({uid:i,name:"aaa",age:18}) ... } WriteResult({ "nInserted" : 1 }) > db.c2.find() { "_id" : ObjectId("60a3c239e98e236914117692"), "uid" : 1, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117693"), "uid" : 2, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117694"), "uid" : 3, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117695"), "uid" : 4, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117696"), "uid" : 5, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117697"), "uid" : 6, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117698"), "uid" : 7, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117699"), "uid" : 8, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e23691411769a"), "uid" : 9, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e23691411769b"), "uid" : 10, "name" : "aaa", "age" : 18 }
查询全部数据
db.集合名.find()
实例
> db.users.find()
{ "_id" : ObjectId("60a3bd32e98e23691411768e"), "name" : "zhangsan", "age" : 20 }
查询全部数据,只看某一列
db.集合名.find({},{要查看的列名:1})
实例
> db.users.find({},{name:1})
{ "_id" : ObjectId("60a3bd32e98e23691411768e"), "name" : "zhangsan" }
{ "_id" : 123, "name" : "lisi" }
{ "_id" : ObjectId("60a3c022e98e23691411768f"), "name" : "xiaoming" }
{ "_id" : ObjectId("60a3c022e98e236914117690"), "name" : "xiaogang" }
{ "_id" : ObjectId("60a3c022e98e236914117691"), "name" : "xiaohong" }
查询全部数据,除了某一列都看
db.集合名.find({},{不看的列名:0})
实例
> db.users.find({},{name:0})
{ "_id" : ObjectId("60a3bd32e98e23691411768e"), "age" : 20 }
{ "_id" : 123, "age" : 21 }
{ "_id" : ObjectId("60a3c022e98e23691411768f"), "age" : 18 }
{ "_id" : ObjectId("60a3c022e98e236914117690"), "age" : 21 }
{ "_id" : ObjectId("60a3c022e98e236914117691"), "age" : 19 }
查询年龄大于20岁的数据
db.集合名.find({列名:{$gt:数}})
实例
> db.users.find({age:{$gt:20}})
{ "_id" : 123, "name" : "lisi", "age" : 21 }
{ "_id" : ObjectId("60a3c022e98e236914117690"), "name" : "xiaogang", "age" : 21 }
运算符表格
运算符 | 作用 |
---|---|
$gt | 大于 |
$gte | 大于等于 |
$lt | 小于 |
$lte | 小于等于 |
$ne | 不等于 |
$in | in |
$nin | not in |
实例
--查询全部数据 > db.c3.find() { "_id" : ObjectId("60a4652c965f0a085d48481c"), "uid" : 1, "uname" : "a1", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481d"), "uid" : 2, "uname" : "a2", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481e"), "uid" : 3, "uname" : "a3", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481f"), "uid" : 4, "uname" : "a4", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484820"), "uid" : 5, "uname" : "a5", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484821"), "uid" : 6, "uname" : "a6", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484822"), "uid" : 7, "uname" : "a7", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484823"), "uid" : 8, "uname" : "a8", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484824"), "uid" : 9, "uname" : "a9", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484825"), "uid" : 10, "uname" : "a10", "usex" : "男" } --查询uid大于5的数据 > db.c2.find({uid:{$gt:5}}) { "_id" : ObjectId("60a3c239e98e236914117697"), "uid" : 6, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117698"), "uid" : 7, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e236914117699"), "uid" : 8, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e23691411769a"), "uid" : 9, "name" : "aaa", "age" : 18 } { "_id" : ObjectId("60a3c239e98e23691411769b"), "uid" : 10, "name" : "aaa", "age" : 18 } --查询uid大于等于5的数据 > db.c3.find({uid:{$gte:5}}) { "_id" : ObjectId("60a4652c965f0a085d484820"), "uid" : 5, "uname" : "a5", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484821"), "uid" : 6, "uname" : "a6", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484822"), "uid" : 7, "uname" : "a7", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484823"), "uid" : 8, "uname" : "a8", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484824"), "uid" : 9, "uname" : "a9", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484825"), "uid" : 10, "uname" : "a10", "usex" : "男" } --查询uid小于等于5的数据 > db.c3.find({uid:{$lte:5}}) { "_id" : ObjectId("60a4652c965f0a085d48481c"), "uid" : 1, "uname" : "a1", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481d"), "uid" : 2, "uname" : "a2", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481e"), "uid" : 3, "uname" : "a3", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481f"), "uid" : 4, "uname" : "a4", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484820"), "uid" : 5, "uname" : "a5", "usex" : "男" } --查询 uid小于5的数据 > db.c3.find({uid:{$lt:5}}) { "_id" : ObjectId("60a4652c965f0a085d48481c"), "uid" : 1, "uname" : "a1", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481d"), "uid" : 2, "uname" : "a2", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481e"), "uid" : 3, "uname" : "a3", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481f"), "uid" : 4, "uname" : "a4", "usex" : "男" } -- 查询uid在[1,3,5]之中的数据 > db.c3.find({uid:{$in:[1,3,5]}}) { "_id" : ObjectId("60a4652c965f0a085d48481c"), "uid" : 1, "uname" : "a1", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481e"), "uid" : 3, "uname" : "a3", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484820"), "uid" : 5, "uname" : "a5", "usex" : "男" } -- 查询uid不在[1,3,5]之中的数据 > db.c3.find({uid:{$nin:[1,3,5]}}) { "_id" : ObjectId("60a4652c965f0a085d48481d"), "uid" : 2, "uname" : "a2", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d48481f"), "uid" : 4, "uname" : "a4", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484821"), "uid" : 6, "uname" : "a6", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484822"), "uid" : 7, "uname" : "a7", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484823"), "uid" : 8, "uname" : "a8", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484824"), "uid" : 9, "uname" : "a9", "usex" : "男" } { "_id" : ObjectId("60a4652c965f0a085d484825"), "uid" : 10, "uname" : "a10", "usex" : "男" }
db.集合名.update(条件,新数据[是否新增,是否修改多条])
解释小贴士:
是否新增:指条件匹配不带数据则插入(true是插入,false是不插入默认)
是否修改多条:指匹配成功的数据都修改(true是都修改,false是只修改单条数据默认)
修改器表格
修改器 | 作用 |
---|---|
$inc | 递增(正数增,负数减) |
$rename | 重命名列 |
$set | 修改列值 |
$unset | 删除列 |
开始实例讲解
准备工作
> show collections > for(var i=1;i<=10;i++){ ... db.c1.insert({uid:i,uname:"user"+i,age:10+i}) ... } WriteResult({ "nInserted" : 1 }) > db.c1.find() { "_id" : ObjectId("60a478e9965f0a085d484827"), "uid" : 1, "uname" : "user1", "age" : 11 } { "_id" : ObjectId("60a478e9965f0a085d484828"), "uid" : 2, "uname" : "user2", "age" : 12 } { "_id" : ObjectId("60a478e9965f0a085d484829"), "uid" : 3, "uname" : "user3", "age" : 13 } { "_id" : ObjectId("60a478e9965f0a085d48482a"), "uid" : 4, "uname" : "user4", "age" : 14 } { "_id" : ObjectId("60a478e9965f0a085d48482b"), "uid" : 5, "uname" : "user5", "age" : 15 } { "_id" : ObjectId("60a478e9965f0a085d48482c"), "uid" : 6, "uname" : "user6", "age" : 16 } { "_id" : ObjectId("60a478e9965f0a085d48482d"), "uid" : 7, "uname" : "user7", "age" : 17 } { "_id" : ObjectId("60a478e9965f0a085d48482e"), "uid" : 8, "uname" : "user8", "age" : 18 } { "_id" : ObjectId("60a478e9965f0a085d48482f"), "uid" : 9, "uname" : "user9", "age" : 19 } { "_id" : ObjectId("60a478e9965f0a085d484830"), "uid" : 10, "uname" : "user10", "age" : 20 } >
将uname为user1的数据修改为user20
> db.c1.update({uname:"user1"},{uname:"user20"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c1.find()
{ "_id" : ObjectId("60a478e9965f0a085d484827"), "uname" : "user20" }
{ "_id" : ObjectId("60a478e9965f0a085d484828"), "uid" : 2, "uname" : "user2", "age" : 12 }
-- 后面的数据不展示了
-- 之前的数据
{ "_id" : ObjectId("60a478e9965f0a085d484828"), "uid" : 2, "uname" : "user2", "age" : 12 }
--执行修改
> db.c1.update({uname:"user2"},{$set:{uname:"user21"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
-- 修改之后的数据
> db.c1.find()
{ "_id" : ObjectId("60a478e9965f0a085d484828"), "uid" : 2, "uname" : "user21", "age" : 12 }
给uname为user3的数据的age加或者减3岁
-- 加年龄之前 { "_id" : ObjectId("60a478e9965f0a085d484829"), "uid" : 3, "uname" : "user3", "age" : 13 } -- 执行加两岁年龄 > db.c1.update({uname:"user3"},{$inc:{age:2}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) -- 加2岁年龄之后 > db.c1.find({uname:"user3"}) { "_id" : ObjectId("60a478e9965f0a085d484829"), "uid" : 3, "uname" : "user3", "age" : 15 } -- 执行减三岁年龄 > db.c1.update({uname:"user3"},{$inc:{age: -3}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) --执行减三岁年龄之后 { "_id" : ObjectId("60a478e9965f0a085d484829"), "uid" : 3, "uname" : "user3", "age" : 12 }
修改器综合练习
> db.c2.insert({uname:"张无忌",age:20,who:"男",other:"古代人"})
WriteResult({ "nInserted" : 1 })
> db.c2.find()
{ "_id" : ObjectId("60a47d45965f0a085d484831"), "uname" : "张无忌", "age" : 20, "who" : "男", "other" : "古代人" }
-- 执行修改
> db.c2.update({uname:"张无忌"},{
... $set:{uname:"唐三"},
... $inc:{age:20},
... $rename:{who:"sex"},
... $unset:{other:true}
... })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
-- 修改结果
> db.c2.find()
{ "_id" : ObjectId("60a47d45965f0a085d484831"), "uname" : "唐三", "age" : 40, "sex" : "男" }
验证语法的最后两个参数(了解)
> for(var i=1;i<=5;i++){
... db.c3.insert({uid:i,uname:"user"+i,age:10+i,sex:"男"})
... }
WriteResult({ "nInserted" : 1 })
> db.c3.find()
{ "_id" : ObjectId("60a480da965f0a085d484837"), "uid" : 1, "uname" : "user1", "age" : 11, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484838"), "uid" : 2, "uname" : "user2", "age" : 12, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484839"), "uid" : 3, "uname" : "user3", "age" : 13, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483a"), "uid" : 4, "uname" : "user4", "age" : 14, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483b"), "uid" : 5, "uname" : "user5", "age" : 15, "sex" : "男" }
>
>
开始
> db.c3.update({uname:"user6"},{$set: {age:20}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.c3.find()
{ "_id" : ObjectId("60a480da965f0a085d484837"), "uid" : 1, "uname" : "user1", "age" : 11, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484838"), "uid" : 2, "uname" : "user2", "age" : 12, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484839"), "uid" : 3, "uname" : "user3", "age" : 13, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483a"), "uid" : 4, "uname" : "user4", "age" : 14, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483b"), "uid" : 5, "uname" : "user5", "age" : 15, "sex" : "男" }
> db.c3.update({uname:"user6"},{$set: {age:20}},false)
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.c3.find()
{ "_id" : ObjectId("60a480da965f0a085d484837"), "uid" : 1, "uname" : "user1", "age" : 11, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484838"), "uid" : 2, "uname" : "user2", "age" : 12, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484839"), "uid" : 3, "uname" : "user3", "age" : 13, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483a"), "uid" : 4, "uname" : "user4", "age" : 14, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483b"), "uid" : 5, "uname" : "user5", "age" : 15, "sex" : "男" }
> db.c3.update({uname:"user6"},{$set: {age:20}},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("60a4816b6314f27a324b7ad5")
})
> db.c3.find()
{ "_id" : ObjectId("60a480da965f0a085d484837"), "uid" : 1, "uname" : "user1", "age" : 11, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484838"), "uid" : 2, "uname" : "user2", "age" : 12, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d484839"), "uid" : 3, "uname" : "user3", "age" : 13, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483a"), "uid" : 4, "uname" : "user4", "age" : 14, "sex" : "男" }
{ "_id" : ObjectId("60a480da965f0a085d48483b"), "uid" : 5, "uname" : "user5", "age" : 15, "sex" : "男" }
{ "_id" : ObjectId("60a4816b6314f27a324b7ad5"), "uname" : "user6", "age" : 20 }
-- 先看下集合有哪些数据 > db.c3.find() { "_id" : ObjectId("60a480da965f0a085d484837"), "uid" : 1, "uname" : "user1", "age" : 11, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d484838"), "uid" : 2, "uname" : "user2", "age" : 12, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d484839"), "uid" : 3, "uname" : "user3", "age" : 13, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d48483a"), "uid" : 4, "uname" : "user4", "age" : 14, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d48483b"), "uid" : 5, "uname" : "user5", "age" : 15, "sex" : "男" } { "_id" : ObjectId("60a4816b6314f27a324b7ad5"), "uname" : "user6", "age" : 20 } 只删除满足条件的第一条数据 > db.c3.remove({},true) WriteResult({ "nRemoved" : 1 }) --查看还剩那些数据 > db.c3.find() { "_id" : ObjectId("60a480da965f0a085d484838"), "uid" : 2, "uname" : "user2", "age" : 12, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d484839"), "uid" : 3, "uname" : "user3", "age" : 13, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d48483a"), "uid" : 4, "uname" : "user4", "age" : 14, "sex" : "男" } { "_id" : ObjectId("60a480da965f0a085d48483b"), "uid" : 5, "uname" : "user5", "age" : 15, "sex" : "男" } { "_id" : ObjectId("60a4816b6314f27a324b7ad5"), "uname" : "user6", "age" : 20 } -- 删除满足条件的全部数据 > db.c3.remove({},false) WriteResult({ "nRemoved" : 5 }) 再次查询数据 > db.c3.find() >
升降序
练习:年龄升序&降序
> for(var i=1;i<=10;i++){ ... db.c4.insert({uid:i,uname:"user"+i,sex:"男",age:10+i}) ... } WriteResult({ "nInserted" : 1 }) > db.c4.insert([{uid:11,uname:"user11",sex:"女",age:18},{uid:11,uname:"user12",sex:"女",age:18}, {uid:13,uname:"user13",sex:"女",age:17}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.c4.find() { "_id" : ObjectId("60a4bc6e965f0a085d48483c"), "uid" : 1, "uname" : "user1", "sex" : "男", "age" : 11 } { "_id" : ObjectId("60a4bc6e965f0a085d48483d"), "uid" : 2, "uname" : "user2", "sex" : "男", "age" : 12 } { "_id" : ObjectId("60a4bc6e965f0a085d48483e"), "uid" : 3, "uname" : "user3", "sex" : "男", "age" : 13 } { "_id" : ObjectId("60a4bc6e965f0a085d48483f"), "uid" : 4, "uname" : "user4", "sex" : "男", "age" : 14 } { "_id" : ObjectId("60a4bc6e965f0a085d484840"), "uid" : 5, "uname" : "user5", "sex" : "男", "age" : 15 } { "_id" : ObjectId("60a4bc6e965f0a085d484841"), "uid" : 6, "uname" : "user6", "sex" : "男", "age" : 16 } { "_id" : ObjectId("60a4bc6e965f0a085d484842"), "uid" : 7, "uname" : "user7", "sex" : "男", "age" : 17 } { "_id" : ObjectId("60a4bc6e965f0a085d484843"), "uid" : 8, "uname" : "user8", "sex" : "男", "age" : 18 } { "_id" : ObjectId("60a4bc6e965f0a085d484844"), "uid" : 9, "uname" : "user9", "sex" : "男", "age" : 19 } { "_id" : ObjectId("60a4bc6e965f0a085d484845"), "uid" : 10, "uname" : "user10", "sex" : "男", "age" : 20 } { "_id" : ObjectId("60a4bcc4965f0a085d484846"), "uid" : 12, "uname" : "user11", "sex" : "女", "age" : 18 } { "_id" : ObjectId("60a4bd1e965f0a085d484847"), "uid" : 11, "uname" : "user12", "sex" : "女", "age" : 18 } { "_id" : ObjectId("60a4bd1e965f0a085d484848"), "uid" : 13, "uname" : "user13", "sex" : "女", "age" : 17 } >
根据年龄降序和升序
-- 降序 > db.c4.find().sort({age:-1}) { "_id" : ObjectId("60a4bc6e965f0a085d484845"), "uid" : 10, "uname" : "user10", "sex" : "男", "age" : 20 } { "_id" : ObjectId("60a4bc6e965f0a085d484844"), "uid" : 9, "uname" : "user9", "sex" : "男", "age" : 19 } { "_id" : ObjectId("60a4bc6e965f0a085d484843"), "uid" : 8, "uname" : "user8", "sex" : "男", "age" : 18 } { "_id" : ObjectId("60a4bcc4965f0a085d484846"), "uid" : 11, "uname" : "user11", "sex" : "女", "age" : 18 } { "_id" : ObjectId("60a4bd1e965f0a085d484847"), "uid" : 12, "uname" : "user12", "sex" : "女", "age" : 18 } { "_id" : ObjectId("60a4bc6e965f0a085d484842"), "uid" : 7, "uname" : "user7", "sex" : "男", "age" : 17 } { "_id" : ObjectId("60a4bd1e965f0a085d484848"), "uid" : 13, "uname" : "user13", "sex" : "女", "age" : 17 } { "_id" : ObjectId("60a4bc6e965f0a085d484841"), "uid" : 6, "uname" : "user6", "sex" : "男", "age" : 16 } { "_id" : ObjectId("60a4bc6e965f0a085d484840"), "uid" : 5, "uname" : "user5", "sex" : "男", "age" : 15 } { "_id" : ObjectId("60a4bc6e965f0a085d48483f"), "uid" : 4, "uname" : "user4", "sex" : "男", "age" : 14 } { "_id" : ObjectId("60a4bc6e965f0a085d48483e"), "uid" : 3, "uname" : "user3", "sex" : "男", "age" : 13 } { "_id" : ObjectId("60a4bc6e965f0a085d48483d"), "uid" : 2, "uname" : "user2", "sex" : "男", "age" : 12 } { "_id" : ObjectId("60a4bc6e965f0a085d48483c"), "uid" : 1, "uname" : "user1", "sex" : "男", "age" : 11 } -- 升序 > db.c4.find().sort({age:1}) { "_id" : ObjectId("60a4bc6e965f0a085d48483c"), "uid" : 1, "uname" : "user1", "sex" : "男", "age" : 11 } { "_id" : ObjectId("60a4bc6e965f0a085d48483d"), "uid" : 2, "uname" : "user2", "sex" : "男", "age" : 12 } { "_id" : ObjectId("60a4bc6e965f0a085d48483e"), "uid" : 3, "uname" : "user3", "sex" : "男", "age" : 13 } { "_id" : ObjectId("60a4bc6e965f0a085d48483f"), "uid" : 4, "uname" : "user4", "sex" : "男", "age" : 14 } { "_id" : ObjectId("60a4bc6e965f0a085d484840"), "uid" : 5, "uname" : "user5", "sex" : "男", "age" : 15 } { "_id" : ObjectId("60a4bc6e965f0a085d484841"), "uid" : 6, "uname" : "user6", "sex" : "男", "age" : 16 } { "_id" : ObjectId("60a4bc6e965f0a085d484842"), "uid" : 7, "uname" : "user7", "sex" : "男", "age" : 17 } { "_id" : ObjectId("60a4bd1e965f0a085d484848"), "uid" : 13, "uname" : "user13", "sex" : "女", "age" : 17 } { "_id" : ObjectId("60a4bc6e965f0a085d484843"), "uid" : 8, "uname" : "user8", "sex" : "男", "age" : 18 } { "_id" : ObjectId("60a4bcc4965f0a085d484846"), "uid" : 11, "uname" : "user11", "sex" : "女", "age" : 18 } { "_id" : ObjectId("60a4bd1e965f0a085d484847"), "uid" : 12, "uname" : "user12", "sex" : "女", "age" : 18 } { "_id" : ObjectId("60a4bc6e965f0a085d484844"), "uid" : 9, "uname" : "user9", "sex" : "男", "age" : 19 } { "_id" : ObjectId("60a4bc6e965f0a085d484845"), "uid" : 10, "uname" : "user10", "sex" : "男", "age" : 20 } >
Limit与Skip方法
练习:
> db.c4.find().sort({age:-1}).skip(0).limit(2)
{ "_id" : ObjectId("60a4bc6e965f0a085d484845"), "uid" : 10, "uname" : "user10", "sex" : "男", "age" : 20 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484844"), "uid" : 9, "uname" : "user9", "sex" : "男", "age" : 19 }
> db.c4.find().sort({age:-1}).skip(2).limit(2)
{ "_id" : ObjectId("60a4bcc4965f0a085d484846"), "uid" : 11, "uname" : "user11", "sex" : "女", "age" : 18 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484843"), "uid" : 8, "uname" : "user8", "sex" : "男", "age" : 18 }
>
实战分页
实例
-- 第一页的数据
> db.c4.find().skip((1-1)*3).limit(3)
{ "_id" : ObjectId("60a4bc6e965f0a085d48483c"), "uid" : 1, "uname" : "user1", "sex" : "男", "age" : 11 }
{ "_id" : ObjectId("60a4bc6e965f0a085d48483d"), "uid" : 2, "uname" : "user2", "sex" : "男", "age" : 12 }
{ "_id" : ObjectId("60a4bc6e965f0a085d48483e"), "uid" : 3, "uname" : "user3", "sex" : "男", "age" : 13 }
-- 第三页的数据
> db.c4.find().skip((3-1)*3).limit(3)
{ "_id" : ObjectId("60a4bc6e965f0a085d484842"), "uid" : 7, "uname" : "user7", "sex" : "男", "age" : 17 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484843"), "uid" : 8, "uname" : "user8", "sex" : "男", "age" : 18 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484844"), "uid" : 9, "uname" : "user9", "sex" : "男", "age" : 19 }
>
db.集合名称.aggregate( [ {管道:{表达式}}... ] )
管道 | 功能 |
---|---|
$group | 将集合中的文档分组,用于统计结果 |
$match | 过滤数据,只要输出符合条件的文档 |
$sort | 聚合数据进一步排序 |
$skip | 跳过指定文档数 |
$limit | 限制集合数据返回文档数 |
表达式 | 功能 |
---|---|
$sum | 总和($sum:1,与count相同,表示统计) |
$avg | 平均 |
$min | 最小值 |
$max | 最大值 |
实例
> db.c4.find()
{ "_id" : ObjectId("60a4bc6e965f0a085d48483c"), "uid" : 1, "uname" : "user1", "sex" : "男", "age" : 11 }
{ "_id" : ObjectId("60a4bc6e965f0a085d48483d"), "uid" : 2, "uname" : "user2", "sex" : "男", "age" : 12 }
{ "_id" : ObjectId("60a4bc6e965f0a085d48483e"), "uid" : 3, "uname" : "user3", "sex" : "男", "age" : 13 }
{ "_id" : ObjectId("60a4bc6e965f0a085d48483f"), "uid" : 4, "uname" : "user4", "sex" : "男", "age" : 14 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484840"), "uid" : 5, "uname" : "user5", "sex" : "男", "age" : 15 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484841"), "uid" : 6, "uname" : "user6", "sex" : "男", "age" : 16 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484842"), "uid" : 7, "uname" : "user7", "sex" : "男", "age" : 17 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484843"), "uid" : 8, "uname" : "user8", "sex" : "男", "age" : 18 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484844"), "uid" : 9, "uname" : "user9", "sex" : "男", "age" : 19 }
{ "_id" : ObjectId("60a4bc6e965f0a085d484845"), "uid" : 10, "uname" : "user10", "sex" : "男", "age" : 20 }
{ "_id" : ObjectId("60a4bcc4965f0a085d484846"), "uid" : 11, "uname" : "user11", "sex" : "女", "age" : 18 }
{ "_id" : ObjectId("60a4bd1e965f0a085d484847"), "uid" : 12, "uname" : "user12", "sex" : "女", "age" : 18 }
{ "_id" : ObjectId("60a4bd1e965f0a085d484848"), "uid" : 13, "uname" : "user13", "sex" : "女", "age" : 17 }
>
> db.c4.aggregate([
... {
... $group:{
... _id:"$sex",
... rs:{$sum:"$age"}
... }
... }
... ])
{ "_id" : "男", "rs" : 155 }
{ "_id" : "女", "rs" : 53 }
> db.c4.aggregate([
... {
... $group:{
... _id:"$sex",
... rs:{$sum:1}
... }
... }
... ])
{ "_id" : "男", "rs" : 10 }
{ "_id" : "女", "rs" : 3 }
-- 降序 > db.c4.aggregate([ ... { ... $group:{ ... _id:"$sex", ... rs:{$sum:1} ... } ... }, ... {$sort:{ ... rs:-1 ... } ... } ... ]) { "_id" : "男", "rs" : 10 } { "_id" : "女", "rs" : 3 } -- 升序 > db.c4.aggregate([ ... { ... $group:{ ... _id:"$sex", ... rs:{$sum:1} ... } ... }, ... {$sort:{ ... rs:1 ... } ... } ... ]) { "_id" : "女", "rs" : 3 } { "_id" : "男", "rs" : 10 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。