当前位置:   article > 正文

MongoDB在windows系统的安装配置详解以及基本操作,一篇学会MongoDB从入门到全会_windows mongodb服务配置内存

windows mongodb服务配置内存

1.数据库的种类

种类

  • 关系型:oracle、mysql、sqlite、sqlServer等
  • 非关系型(Not Only SQL):MongoDB(文档)、Redis、Memcache(内存)

关系型和非关系型数据库软件区别

相同点:

  • 都是数据库软件,用于存放项目数据

不同点

  • 关系型:
    • 遵循sql标准,换句话说语法大同小异
    • 有库和表约束等
  • 非关系型:
    • 没有统一标准
    • 一般键值对形式存储
    • 读取速度更快

2.MongoDB

MongoDB简介

MongoDB是什么

MongoDB是一个基于分布式文件存储的数据库,由c++语言编写,旨在为WEB应用提供可拓展的高性能数据存储解决方案。
MongoDB是一个介于关系型数据库和非关系型数据库之间的产品,是非关系数据库当中功能最丰富,最像关系型数据库的。

  • 基于分布式文件存储数据库(就是一个数据库)
  • C++语言编写
  • 支持的数据类型非常松散,是类似jsonbson格式(后期插入修改数据写json)
    解释:
json示例
{id:1,name:张三,age=20}
  • 1
  • 2

MongoDB功能

  • 存放项目数据
  • 实战开发写API接口(重要)
  • 等等

3.下载安装MongoDB

下载

  • MongoDB安装网址官网
  • 选择社区版下载网址
  • 点击最新版本,选择压缩包形式,开始下载

解压、创建包

  • 一定要强调的,不要解压到中文目录名下否则可能会报错!!!(作者踩雷)
  • 下载好之后解压
  • 创建几个包在解压后的目录下(与bin目录同级)
    • bak(用于备份)
    • log(日志)
    • data(数据)

如图
目录创建图解

创建服务

  • 以管理员身份运行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 服务已经启动成功。
  • 1
  • 2
  • 就是安装配置成功了

作者亲身踩雷解决

那么如果不成功怎么办?
举一个作者踩雷的例子

我把mongodb解压到中文的路径下了,在管理的服务中看到mongodb服务的属性下的可执行路径有中文乱码,然后执行net start
mongodb,报找不到可执行文件的错误(好像是这个记不清了)

如何解决

  • 果断切换cmd至bin目录下,运行mongod.exe --remove --serviceName "MongoDB",删除掉这个服务(前提cmd是管理员身份打开
  • 然后重新解压一个mongodb文件夹至一个没有中文的目录,然后就是之前的操作了(创建包、开启服务)

4.MongoDB的基本操作

基本概念

  • 生活中:仓库、架子、物品
  • 计算机:数据库(database)、集合(collection)、数据/文档(document)

进入数据库
bin目录下开启服务(net start mongodb)后,输入mongo进入数据库

查看数据库

  • show databases(详写)
> show databases
admin   0.000GB
config  0.000GB
local   0.000GB
  • 1
  • 2
  • 3
  • 4
  • show dbs(简写)
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
  • 1
  • 2
  • 3
  • 4

选择数据库以及创建数据库

选择数据库

  • user 数据库名
> use admin
switched to db admin
  • 1
  • 2

创建数据库(隐式创建)

  • user 数据库名
> use qq
switched to db qq
  • 1
  • 2

但是为什么show dbs看不到这个qq数据库呢?
因为虽然qq数据库已经创建了,但是qq数据库是空的,所以看不到,后面只要插入一点数据,就能看到了。

查看集合

这里的集合类似于关系型数据库的表

  • show collections
> show collections
  • 1

创建集合

  • db.createCollection("集合名")
> db.createCollection('c1')
{ "ok" : 1 }
  • 1
  • 2

此时我们再次查看数据库时,发现qq数据库已经存在了

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
qq      0.000GB
  • 1
  • 2
  • 3
  • 4
  • 5

同样查询qq数据库的集合也能查到集合了

> show collections
c1
  • 1
  • 2

删除集合

  • db.集合名.drop()
> db.c1.drop()
true
> show collections
  • 1
  • 2
  • 3

删除数据库

  • 选择数据库 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5.MongoDB文档增删改查(CURD)

明确需求

数据库主要用来存放项目数据
我们已经学会了数据库和集合的创建
思考:如何实现集合中数据的增删改查呢?
回答:通过MongoDB语法即可

C增(Create)

单条插入

  • 语法:db.集合名.insert(json数据)
  • 说明:集合存在,则直接插入数据,集合不存在,则隐式创建

实例


> use qq
switched to db qq
> show collections
> db.users.insert({name:"zhangsan",age:20})
WriteResult({ "nInserted" : 1 })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

小贴士:

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 }
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

小拓展

> for(var i=1;i<=10;i++){
... print(i)
... }
1
2
3
4
5
6
7
8
9
10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

你明白了吗??!!
意思就是说你可以循环插入多条数据

实例

> 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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

R查(Retrieve)

查询全部数据

  • 语法:db.集合名.find()

实例

> db.users.find()
{ "_id" : ObjectId("60a3bd32e98e23691411768e"), "name" : "zhangsan", "age" : 20 }
  • 1
  • 2

查询全部数据,只看某一列

  • 语法: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" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查询全部数据,除了某一列都看

  • 语法: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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查询年龄大于20岁的数据

  • 语法: db.集合名.find({列名:{$gt:数}})

实例

> db.users.find({age:{$gt:20}})
{ "_id" : 123, "name" : "lisi", "age" : 21 }
{ "_id" : ObjectId("60a3c022e98e236914117690"), "name" : "xiaogang", "age" : 21 }
  • 1
  • 2
  • 3

运算符表格

运算符作用
$gt大于
$gte大于等于
$lt小于
$lte小于等于
$ne不等于
$inin
$ninnot 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" : "男" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

U改(Update)

  • 基础语法: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 }
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

将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 }
-- 后面的数据不展示了
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 解决:使用升级语法修改器
  • 需求:将uname为user2的数据修改为user21
-- 之前的数据
{ "_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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

给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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

修改器综合练习

  • 准备工作
> db.c2.insert({uname:"张无忌",age:20,who:"男",other:"古代人"})
WriteResult({ "nInserted" : 1 })
> db.c2.find()
{ "_id" : ObjectId("60a47d45965f0a085d484831"), "uname" : "张无忌", "age" : 20, "who" : "男", "other" : "古代人" }
  • 1
  • 2
  • 3
  • 4
  • 需求:uname改为唐三,age加上10岁,who字段名改为sex,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" : "男" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

验证语法的最后两个参数(了解)

  • 准备工作
>  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" : "男" }
>
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 验证:是否新增true为是,false为否
  • 业务需求:修改uname为user6的数据的年龄为20
  • 代码实现

开始

  • 不加参数默认false
> 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" : "男" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 加false参数
> 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" : "男" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 加true参数
> 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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

D删(delete)

  • 语法:db.集合名.remove(条件[ ,是否删除一条])
  • 注意:是否删除一条,true是,false否 默认
-- 先看下集合有哪些数据

> 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()
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

排序

升降序

  • 语法:db.集合名.find().sort(json数据)
  • 说明:键-就是要排序的列/字段,值:1升序 -1降序

练习:年龄升序&降序

  • 准备工作
> 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 }
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

根据年龄降序和升序

-- 降序

> 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 }
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

Limit与Skip方法

  • 语法:db.集合名.find().sort().skip(数字).limit(数字)
  • 说明: skip跳过指定数量(可选),limit限制查询数量

练习:

  • 降序查询两条
> 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 }
  • 1
  • 2
  • 3
  • 降序跳过两条并查询两条
> 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 }
>
  • 1
  • 2
  • 3
  • 4

实战分页

  • 需求:实现每页显示3条数据
  • 语法:db.集合名.find().skip((当前页-1)*每页条数).limit(每页显示的条数)

实例

-- 第一页的数据

> 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 }
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

MongoDB的聚合函数

  • 语法: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 }
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 需求:统计男生、女生的总年龄
> db.c4.aggregate([
...  {
...  $group:{
...  _id:"$sex",
...  rs:{$sum:"$age"}
...  }
...  }
... ])
{ "_id" : "男", "rs" : 155 }
{ "_id" : "女", "rs" : 53 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 需求:统计男生、女生的总人数
> db.c4.aggregate([
...  {
...  $group:{
...  _id:"$sex",
...  rs:{$sum:1}
...  }
...  }
... ])
{ "_id" : "男", "rs" : 10 }
{ "_id" : "女", "rs" : 3 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 查询男生、女生人数,按人数数量降序降序
-- 降序

> 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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/824659
推荐阅读
相关标签
  

闽ICP备14008679号