# 模块二 基础巩固 MongoDB 介绍和基础

## 2.5.1 MongoDB -- 介绍 <a href="#id-251mongodb-jie-shao" id="id-251mongodb-jie-shao"></a>

* mysql vs mongo
* 快速开始

### mysql vs mongo <a href="#mysql-vs-mongo" id="mysql-vs-mongo"></a>

| 对比   | mysql                           | mongo                                           |
| ---- | ------------------------------- | ----------------------------------------------- |
| 数据存储 | table 二维表结构，需要预先定义结构            | json 类文档，不需要预先定义结构。可随意新增或删除字段，新增字段不会对已存在的字段产生影响 |
| 查询语法 | sql (structured query language) | mongo                                           |
| 索引   | 如果不定义索引，则进行全表扫描                 | 如果不定义索引，则进行全表扫描                                 |
| 集群   | 支持主从复制                          | 内置副本集、分片、和自动选举                                  |
| 场景   | 关系型结构，在多行插入时需要事务保障              | 实时数据分析、内容管理、iot设备、移动设备（事务需要有内置副本才可以做）           |
| 数据结构 | 结构化、数据 schema 定义清晰              | 未知数据结构类型                                        |
| 风险   | sql 注入攻击                        | 相对来说风险更低                                        |
| 分析   | 确实需要关系型数据库来保障                   | 写入并发高，没有 DBA                                    |

### 快速开始 <a href="#kuai-su-kai-shi" id="kuai-su-kai-shi"></a>

安装 mongo in docker

```
docker run -it --volume=/root/docker/mongo01/data:/data/db -p 27017:27017 --name mongo01 -d mongo
```

robt 3t 下载地址：\
<https://download.studio3t.com/robomongo/windows/robo3t-1.4.2-windows-x86_64-8650949.exe>

新增数据库books，新增集合author

增删改查

```
// 插入
db.author.insertOne({"name":"mingson", "age":25}) 
db.author.insertOne({"name":"jesse", "age":18}) 
db.author.insertOne({"name":"bobo", "age":18})

// 查询 
db.getCollection('author').find({"name":"mingson"})
db.getCollection('author').find({"name":{$eq:"mingson"}})

// 更新
db.author.updateOne({"name":"mingson"},{$set:{"age":20}})

// 删除
db.author.deleteOne({"name":"bobo"})

// 返回字段，1返回，0不返回
db.getCollection('author').find({"name":"mingson"},{"name":1,"_id":0})
```

## 2.5.2 MongoDB -- 基础 <a href="#id-252mongodb-ji-chu" id="id-252mongodb-ji-chu"></a>

mongo db 文档：\
<https://docs.mongodb.com/manual/introduction/>

中文 mongo db 手册：\
<https://mongoing.com/docs/tutorial/insert-documents.html>

### 数据库/集合/文档 <a href="#shu-ju-ku-ji-he-wen-dang" id="shu-ju-ku-ji-he-wen-dang"></a>

database/collection/document

| mongo      | mysql    |
| ---------- | -------- |
| database   | database |
| collection | table    |
| document   | row      |
| filed      | column   |

#### 数据库 <a href="#shu-ju-ku" id="shu-ju-ku"></a>

* 数据库的名称是大小写敏感

```
不能包含以下字符（win）：/\."$*<>:|?
不能包含以下字符（unix/linux）：/\."$
```

* 不能超过64个字符

#### 集合 <a href="#ji-he" id="ji-he"></a>

* 不能包含$
* 不能为空，不能包含null
* 不能以system.开头

#### 字段名 <a href="#zi-duan-ming" id="zi-duan-ming"></a>

* 不能为空，不能包含null
* 顶级字段不能以$开头
* \_id是保留字段名称

#### BosnTypes <a href="#bosntypes" id="bosntypes"></a>

<https://mongoing.com/docs/reference/bson-types.html>

| string    | string    |
| --------- | --------- |
| bool      | Boolean   |
| int       | int       |
| long      | long      |
| decimal   | decimal   |
| double    | double    |
| date      | date      |
| timestamp | timestamp |
| null      | null      |

* object
* array
* objectid
* regex
* javascripe
