# MongoDB

# 【Mac】安裝MongoDB

```
brew tap mongodb/brew
```

```
brew update
```

```
brew install mongodb-community@6.0
```

[https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/)

---

或是使用docker-composer

\*docker-compose.yml

```YAML
version: '3.1'

services:

  mongo:
    image: mongo:6.0
    restart: always
    container_name: mongo1
    volumes:
      - ./data:/data/db:/data/db
    ports:
      - 27017:27017
```

```shell
#起動 docker
docker-compose up

```

# 常用指令

使用mongosh 連入db

```shell
srou@momo-full-devdeiMac ~ % mongosh
Current Mongosh Log ID:	63de0489619b4a08812f03ee
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB:		6.0.4
Using Mongosh:		1.6.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-02-04T06:35:33.363+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2023-02-04T06:35:33.363+00:00: vm.max_map_count is too low
------

test> 

```

```JSON
db.order.aggregate([
  	{ 	$match : {
    		liveId: "aaaa",
      		orderDate: { $gt:ISODate("2023-11-01T23:00:00+08:00")}
    	}
    },
    {
        $group: {
            _id: {
                orderNo: "$orderNo",
                liveId: "$liveId"
            },
            count: { $sum: 1 }
        }
    },
    {
        $match: {
            count: { $gte: 2 }
        }
    },
  {
    $sort: { count : -1}
  },
  {
  	$limit:5
  }
])
/////////////////////////////////////////
select count(orderNo), count(liveId) 
from order 
group by orderNo, liveId 
order by count(liveId)
having count(*) >= 2
limit 5
             

```

# MongoDB教育訓練-01

####  

#### mongodb 儲存格式 是 Bson

[![image-1639619984280.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/oxUMylTkL0FwEXyZ-image-1639619984280.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/oxUMylTkL0FwEXyZ-image-1639619984280.png)

---

#### MongoDB 與關連式資料庫結構對照

---

[![image-1639620745002.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/W9Lmzan35b3QRvxu-image-1639620745002.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/W9Lmzan35b3QRvxu-image-1639620745002.png)

---

##### 企業版的Tool

---

[![image-1639621669047.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/JAqoDNlA6CbxSY8E-image-1639621669047.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/JAqoDNlA6CbxSY8E-image-1639621669047.png)

---

#### 何時使用ＭongoDB

- 需要存取資料
- 需要很大的數據
- 需要快速迭代開發
- 需要經常異動結構的資料
- 需要水平擴展

[![image-1639621823585.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/lXhirUMxp8JUe5I6-image-1639621823585.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/lXhirUMxp8JUe5I6-image-1639621823585.png)

[![image-1639621986744.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/tMwkeefhczCy8lgJ-image-1639621986744.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/tMwkeefhczCy8lgJ-image-1639621986744.png)

[![image-1639622039228.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/elifzXOUWxBiD6wh-image-1639622039228.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/elifzXOUWxBiD6wh-image-1639622039228.png)

---

#### MongoDB 與聯式資料庫對比

---

[![image-1639622524198.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/F2XJZ4J4HPuEp8Y6-image-1639622524198.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/F2XJZ4J4HPuEp8Y6-image-1639622524198.png)

[![image-1639622553219.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/9g5qPu1DqUbrjfJQ-image-1639622553219.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/9g5qPu1DqUbrjfJQ-image-1639622553219.png)

[![image-1639622880932.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/tHfDGn5LL3Xu3W0v-image-1639622880932.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/tHfDGn5LL3Xu3W0v-image-1639622880932.png)

# MongoDB教育訓練-02

[![image-1639624442610.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/3k1ivaJGMWhtW5tT-image-1639624442610.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/3k1ivaJGMWhtW5tT-image-1639624442610.png)

註冊

[https://cloud.mongodb.com/](https://cloud.mongodb.com/)

tool

https://www.mongodb.com/try/download/shell

[![image-1639625251215.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/DXUNYmjkudfFbzFL-image-1639625251215.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/DXUNYmjkudfFbzFL-image-1639625251215.png)

[![image-1639625277499.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/aGAaOBHIp8Oh9CNU-image-1639625277499.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/aGAaOBHIp8Oh9CNU-image-1639625277499.png)

### 1.新增帳號

[![image-1639625301233.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/NLdjaVAFQFaPwxnX-image-1639625301233.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/NLdjaVAFQFaPwxnX-image-1639625301233.png)

### 2.新增你的存取IP-&gt; 【add My Current Ip address】

[![image-1639625403123.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/r3no6agALJXsY2RP-image-1639625403123.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/r3no6agALJXsY2RP-image-1639625403123.png)

### 3.下載sample資料庫

[![image-1639626142620.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/WnivN3wogBd9wSxH-image-1639626142620.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/WnivN3wogBd9wSxH-image-1639626142620.png)

[![image-1639626286659.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/iLF5QzfcZF7ipXp7-image-1639626286659.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/iLF5QzfcZF7ipXp7-image-1639626286659.png)

#### 下載中.....

[![image-1639626839316.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/2coyhuTSbwCCNGcy-image-1639626839316.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/2coyhuTSbwCCNGcy-image-1639626839316.png)

#### 已下載完成

[![image-1639626966774.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/wIqc6xXs7smVRpZJ-image-1639626966774.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/wIqc6xXs7smVRpZJ-image-1639626966774.png)

###  

---

### 4.新增連線 【Connect】

[![image-1639625864493.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/9uGFPUTDgwZL09pv-image-1639625864493.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/9uGFPUTDgwZL09pv-image-1639625864493.png)

### Connect with the MongoDB Shell

### [![image-1639625922470.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/32XCdwdH7eL1qDbx-image-1639625922470.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/32XCdwdH7eL1qDbx-image-1639625922470.png)

[![image-1639626096319.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BeiGLEI11ItjPM37-image-1639626096319.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BeiGLEI11ItjPM37-image-1639626096319.png)

#### Mac 安裝

```shell
brew install mongosh
```

#### 連線(依照網頁url)

```shell
mongosh "mongodb+srv://cluster0.0roj8.mongodb.net/myFirstDatabase" --username admin
```

[![image-1639626216471.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/NpReneOzL4k6Ct01-image-1639626216471.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/NpReneOzL4k6Ct01-image-1639626216471.png)

# MongoDB教育訓練-03

[![image-1639632341110.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/94lf1AMIPzr4hZtu-image-1639632341110.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/94lf1AMIPzr4hZtu-image-1639632341110.png)

[![image-1639632437556.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/WKjief5bW4SVievQ-image-1639632437556.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/WKjief5bW4SVievQ-image-1639632437556.png)

count 儘量不要用

db.inspections.countDocuments() #精確查詢(1)

db.inspections.count() #

[![image-1639632701684.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PvIPPDc1MJeJEHO7-image-1639632701684.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PvIPPDc1MJeJEHO7-image-1639632701684.png)

[![image-1639632747819.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FmITDA5ZEs1EcJLL-image-1639632747819.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FmITDA5ZEs1EcJLL-image-1639632747819.png)

### insertOne 插入一筆資料

```shell
#插入一條資料
insertOne 
```

[![image-1639633018479.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/nPd523UFpm9GLEdp-image-1639633018479.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/nPd523UFpm9GLEdp-image-1639633018479.png)

id 本身會包含時間

```shell
getTimestamp #取得時間
```

[![image-1639633066887.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ezNU46uA4bVaj8YV-image-1639633066887.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ezNU46uA4bVaj8YV-image-1639633066887.png)

---

### insertMany 插入多筆資料

---

[![image-1639633247450.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PK8p90XrwmMYMVob-image-1639633247450.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PK8p90XrwmMYMVob-image-1639633247450.png)

---

### 有序插入＆無序插入

{ordered: true | false}

- 有序可保證順序，速度較慢，中間錯誤會全部返回
- 使用無序不影響，中間錯誤紀錄

[![image-1639633329359.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/6t8AKkTj2D95v1K7-image-1639633329359.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/6t8AKkTj2D95v1K7-image-1639633329359.png)

---

### 搜尋

- find()
- findOne()

[![image-1639633549742.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/muIv1v567N87Gkzz-image-1639633549742.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/muIv1v567N87Gkzz-image-1639633549742.png)

[![image-1639633608684.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/cLLolKcRHsNGZ2oS-image-1639633608684.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/cLLolKcRHsNGZ2oS-image-1639633608684.png)

---

### 返回特定欄位

[![image-1639633633419.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/KbQT1216Sq9SAD2i-image-1639633633419.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/KbQT1216Sq9SAD2i-image-1639633633419.png)

[![image-1639633835708.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/hlXc6ACses2PRdDQ-image-1639633835708.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/hlXc6ACses2PRdDQ-image-1639633835708.png)

Find 搜尋20筆資料

```shell
for(var i = 0; i < 200; i++) db.taxis.insertOne({plate: i})

# plate = 5 
db.taxis.find({plate:5})

# plate >= 5 
db.taxis.find({plate:{$gt: 5}})

#下20筆
it 

```

[![image-1639633889739.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/HAJKD0YI6qjXbzCS-image-1639633889739.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/HAJKD0YI6qjXbzCS-image-1639633889739.png)

[![image-1639634201304.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/S6OTyAfpsm9EnT7f-image-1639634201304.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/S6OTyAfpsm9EnT7f-image-1639634201304.png)

[![image-1639634238534.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BqQDUaktlTiyjEE0-image-1639634238534.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BqQDUaktlTiyjEE0-image-1639634238534.png)

[![image-1639634282001.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/SHspsUv1MESPkWVs-image-1639634282001.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/SHspsUv1MESPkWVs-image-1639634282001.png)

[![image-1639634354192.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/0eB5sNzuWeFblYm5-image-1639634354192.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/0eB5sNzuWeFblYm5-image-1639634354192.png)

curosr 10分鐘會被丟棄

[![image-1639634486948.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/HqauJyTiUugDrl2S-image-1639634486948.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/HqauJyTiUugDrl2S-image-1639634486948.png)

[![image-1639634849381.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4WaVjcgxz6nUFEpi-image-1639634849381.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4WaVjcgxz6nUFEpi-image-1639634849381.png)

```shell
db.diaries.drop()

db.diaries.insertMany([
{ name: "dug", day: ISODate("2014-11-04"), txt: "went for a walk"},
{ name: "dug", day: ISODate("2014-11-06"), txt: "saw a squirrel"},
{ name: "ray", day: ISODate("2014-11-06"), txt: "met dug in the park"},
{ name: "dug", day: ISODate("2014-11-09"), txt: "got a treat"}
])

#找出 name = dug

#輸出 name, text

```

---

### 子查詢

```shell
# city = "New York" 即可
address.city :"New York"
#{ city:"New York"} 完整匹配
address : { city:"New York"}} 
```

[![image-1639635141931.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/OojZsQyA1JD09N0q-image-1639635141931.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/OojZsQyA1JD09N0q-image-1639635141931.png)

---

### 範圍查詢

[![image-1639635381525.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/XfYn3OcXjJzF0Uim-image-1639635381525.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/XfYn3OcXjJzF0Uim-image-1639635381525.png)

---

### Boolen 查詢

```SQL
$or : [ {species:"cat", color:"black"},
		{species:"dog", color:"brown"}}]
#等於

where (species = "cat" and color = "black") or (species = "dog" and color = "brown")
```

[![image-1639635516284.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/jSZrykdfTp5dpaZ5-image-1639635516284.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/jSZrykdfTp5dpaZ5-image-1639635516284.png)

[![image-1639635768002.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/skJ9wrMqTynMI6U1-image-1639635768002.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/skJ9wrMqTynMI6U1-image-1639635768002.png)

```JavaScript
db.grades.find({ student_id : {$lt: 65}})

db.inspections.find({$or :[ {result:"Pass"},{result:"Fail"}]})
```

# MongoDB教育訓練-04

<div id="bkmrk-"></div>### Array 查詢

[![image-1639636664729.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/orQuFRgAB53eZmlc-image-1639636664729.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/orQuFRgAB53eZmlc-image-1639636664729.png)

Array 查詢 操作

- $all
- $size(少用無法使用index)
- $element 元素滿足

[![image-1639636792202.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/fE5DFJqTbqsCgw85-image-1639636792202.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/fE5DFJqTbqsCgw85-image-1639636792202.png)

```shell
#age > = 18 && age <= 3 , array 其中一個值滿足其中一個條件即可
age : { $gt : 18, $lt: 30}

#age > = 18 && age <= 3 , array 其中一個值滿足所有條件
age : { $elemMatch { $gt : 18, $lt: 30}}
```

[![image-1639636930873.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Zck5NBmCJvnWMS4V-image-1639636930873.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Zck5NBmCJvnWMS4V-image-1639636930873.png)

[![image-1639637035674.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ozw8oxnaZmpBD8QU-image-1639637035674.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ozw8oxnaZmpBD8QU-image-1639637035674.png)

[![image-1639637083583.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Q66Txy8VQ3dAAJTk-image-1639637083583.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Q66Txy8VQ3dAAJTk-image-1639637083583.png)

---

### 排序

[![image-1639637186726.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/jL5gOADRIlPthog5-image-1639637186726.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/jL5gOADRIlPthog5-image-1639637186726.png)

[![image-1639637211917.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Al2q4DEcAzFIr5x4-image-1639637211917.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Al2q4DEcAzFIr5x4-image-1639637211917.png)

```JavaScript
_id: 0 #不顯示id
```

---

[![image-1639637453427.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/r3xpI3omOeXaZ0NI-image-1639637453427.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/r3xpI3omOeXaZ0NI-image-1639637453427.png)

---

### 更新

- updateOne : 更新查詢第一筆
- updateMany: 更新所有

[![image-1639637602587.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/yjByIjfU24o6QG8H-image-1639637602587.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/yjByIjfU24o6QG8H-image-1639637602587.png)

### 更新欄位

[![image-1639637827684.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bdVrg0JxuMzGLh08-image-1639637827684.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bdVrg0JxuMzGLh08-image-1639637827684.png)

[![image-1639637871930.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/VRZtnZN7od3TLmQT-image-1639637871930.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/VRZtnZN7od3TLmQT-image-1639637871930.png)

```shell
# 加減
$inc : +
# 乘
$mul: *
```

[![image-1639637930449.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/GaZKRKlrgtwLVKc6-image-1639637930449.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/GaZKRKlrgtwLVKc6-image-1639637930449.png)

```shell
#大於更新
$max
#小於才更新
$min 
```

[![image-1639638023450.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/YIpGvQK6qx2wj20t-image-1639638023450.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/YIpGvQK6qx2wj20t-image-1639638023450.png)

[![image-1639638110007.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/OJhetSgj9ZPNc72l-image-1639638110007.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/OJhetSgj9ZPNc72l-image-1639638110007.png)

```shell
#q1
db.inspections.updateMany({result:"Completed"},{$set:{"result":"No Violation Issued"}})
#q2
db.inspections.updateMany({result:'Fail'},{$set:{fine:100}})
#q3
db.inspections.updateMany({"address.city": "ROSEDALE"}, {$inc: {fine: 150}})
```

---

### Array 新增移除

```
$pop : 1 (最後元素)
$pop : -1 (第一個元素)

# 檢查不存在才新增
$addToSet
```

[![image-1639638757601.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/NvJZxjfM99yuQTFD-image-1639638757601.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/NvJZxjfM99yuQTFD-image-1639638757601.png)

[![image-1639638822064.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/hd5Z3nbzmaof7vlr-image-1639638822064.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/hd5Z3nbzmaof7vlr-image-1639638822064.png)

[![image-1639638913289.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bLM6lJ8hzRL8DBOp-image-1639638913289.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bLM6lJ8hzRL8DBOp-image-1639638913289.png)

[![image-1639638947708.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/mPpEqgSYth8HLQIF-image-1639638947708.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/mPpEqgSYth8HLQIF-image-1639638947708.png)

基本mongoDB 基本無lock，每次更新檢更新序號，是否為最新，若不是最新，嘗試合併最新，重新更新

[![image-1639639033397.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/pQCq9IUqhPqAlcuE-image-1639639033397.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/pQCq9IUqhPqAlcuE-image-1639639033397.png)

# MongoDB教育訓練-05

[![image-1639640556450.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/3sgHFnkOoHgAfKv0-image-1639640556450.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/3sgHFnkOoHgAfKv0-image-1639640556450.png)

---

### 更新所有陣列元素

[![image-1639640396053.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/5lmZP0UTk0LeoiX2-image-1639640396053.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/5lmZP0UTk0LeoiX2-image-1639640396053.png)

[![image-1639640527546.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/kMqpqa5yjRU052vs-image-1639640527546.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/kMqpqa5yjRU052vs-image-1639640527546.png)

---

[![image-1639640566773.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/SfSj67qNxS0Og62P-image-1639640566773.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/SfSj67qNxS0Og62P-image-1639640566773.png)

[![image-1639640635519.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/TRpqG0HqBVwRqOjP-image-1639640635519.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/TRpqG0HqBVwRqOjP-image-1639640635519.png)

[![image-1639640671909.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/CN7aSkpj4av8t6pn-image-1639640671909.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/CN7aSkpj4av8t6pn-image-1639640671909.png)

[![image-1639640727694.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/luynlVajG4Z0613P-image-1639640727694.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/luynlVajG4Z0613P-image-1639640727694.png)

---

### 子查詢更新

[![image-1639640872931.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/IrUDJD3mUnixhPZU-image-1639640872931.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/IrUDJD3mUnixhPZU-image-1639640872931.png)

[![image-1639641079436.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/KADc5o84b4xZDYqF-image-1639641079436.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/KADc5o84b4xZDYqF-image-1639641079436.png)

[![image-1639641130771.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/psIaRrfHQ9wBZpfw-image-1639641130771.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/psIaRrfHQ9wBZpfw-image-1639641130771.png)

[![image-1639642205256.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/jw5l0WzI5Y8ALfgU-image-1639642205256.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/jw5l0WzI5Y8ALfgU-image-1639642205256.png)

[![image-1639641985534.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/DrNJjO9jkx5xE6PO-image-1639641985534.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/DrNJjO9jkx5xE6PO-image-1639641985534.png)

[![image-1639642001500.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/8noLiSkdjt2fqqH8-image-1639642001500.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/8noLiSkdjt2fqqH8-image-1639642001500.png)

[![image-1639642223899.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/gF1KtsZ7PX1rOcwm-image-1639642223899.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/gF1KtsZ7PX1rOcwm-image-1639642223899.png)

---

### Update &amp; Insert

資料存在則更新，否則新增

[![image-1639642268831.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/U2dkOvazrx7zLp4p-image-1639642268831.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/U2dkOvazrx7zLp4p-image-1639642268831.png)

---

### 返回更新前後的值(updateOne無法返回)

需要返回值的時候使用，不然就只用updateOne

[![image-1639642579954.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/R9Pllrf4SotvLTP4-image-1639642579954.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/R9Pllrf4SotvLTP4-image-1639642579954.png)

# MongoDB教育訓練-06

[![image-1639705033770.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/t1AM4RJGX5ZBtLVf-image-1639705033770.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/t1AM4RJGX5ZBtLVf-image-1639705033770.png)

[![image-1639705137852.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/khGTD0wPcEe6ELd8-image-1639705137852.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/khGTD0wPcEe6ELd8-image-1639705137852.png)

mongoDB 使用Ｂ-tree索引

[![image-1639705482607.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FahDdBDIM56kjSwr-image-1639705482607.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FahDdBDIM56kjSwr-image-1639705482607.png)

查詢至少要用到索引，不要full table scan(所以要至少建立可用索引)

[![image-1639705535799.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/IcNsjB8malFbXVe9-image-1639705535799.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/IcNsjB8malFbXVe9-image-1639705535799.png)

[![image-1639705770711.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/3HGEO8hp5SuzV7VI-image-1639705770711.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/3HGEO8hp5SuzV7VI-image-1639705770711.png)

檢查執行計畫快取 -&gt; (無) 有可能優化的索引找出來，同時跑看誰的效率快，加入快取

更新快取時機:

- 當前索引變慢(系統判斷)
- 當新的索引被創建
- server 重啟(memory 清除)

[![image-1639705807311.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/qTqMHpE3PRcGVKSh-image-1639705807311.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/qTqMHpE3PRcGVKSh-image-1639705807311.png)

- 需要指定欄位
- 欄位可以是任何類型

[![image-1639706094542.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/8w46WyVtDVVFOrOX-image-1639706094542.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/8w46WyVtDVVFOrOX-image-1639706094542.png)

[![image-1639706346750.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/zrUfOeVQ03fLN8eh-image-1639706346750.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/zrUfOeVQ03fLN8eh-image-1639706346750.png)

Explain 執行計畫

- queryPlanner : 只顯示執行計畫
- executionStats(最常用): 會把<span style="color: #ff0000;">最優</span>執行計畫執行一遍，並收集相關資訊
- allPlansExecution:把<span style="color: #ff0000;">所有</span>執行計畫執行一遍，並收集相關資訊

[![image-1639706656145.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/UvwFOcKDjW3wYOTv-image-1639706656145.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/UvwFOcKDjW3wYOTv-image-1639706656145.png)

- nReturn: 返回數量
- totalKeyExamined ：找到索引
- totalDocExamined : 查找document數量
- stage:COLLSAN =&gt; 無index
- totalKeyExamined / nReturn 最好小於1000

[![image-1639706909824.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ANlcZf5NJFiDeENQ-image-1639706909824.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ANlcZf5NJFiDeENQ-image-1639706909824.png)

number\_of\_reviews: 1 (1: 升冪,-1:降冪)

[![image-1639707147638.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/V7W991262RVWH978-image-1639707147638.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/V7W991262RVWH978-image-1639707147638.png)

通過index 取得資訊：FETCH

[![image-1639707345619.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/qNaUUJCZBvCPfNua-image-1639707345619.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/qNaUUJCZBvCPfNua-image-1639707345619.png)

- nReturn:totalKeyExamined:totalDocExamined 1:1:1 最佳情況

[![image-1639707445597.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/iMAC2vsFjDUcagYR-image-1639707445597.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/iMAC2vsFjDUcagYR-image-1639707445597.png)

[![image-1639707511649.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/b0jijKi7hs3NW8l5-image-1639707511649.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/b0jijKi7hs3NW8l5-image-1639707511649.png)

[![image-1639707565143.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/UPwVfU3YeI8ebflZ-image-1639707565143.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/UPwVfU3YeI8ebflZ-image-1639707565143.png)

取得所有索引

[![image-1639707652623.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/9CLzgoztLs1ULVyh-image-1639707652623.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/9CLzgoztLs1ULVyh-image-1639707652623.png)

刪除索引

[![image-1639707687385.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Pru2KUGncvB033Iv-image-1639707687385.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Pru2KUGncvB033Iv-image-1639707687385.png)

[![image-1639707697169.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/azQ4hbFPVFwHFByu-image-1639707697169.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/azQ4hbFPVFwHFByu-image-1639707697169.png)

需要放得下index的記憶體（最低要求）

[![image-1639707720558.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7GAr6sU8Usp7VSP5-image-1639707720558.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7GAr6sU8Usp7VSP5-image-1639707720558.png)

db.tablename.stats(檔案除以？(1024 =&gt; KB,102481024 =&gt; MB)).indexSizes

[![image-1639707935930.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/65z7pfIxCgvv4WCQ-image-1639707935930.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/65z7pfIxCgvv4WCQ-image-1639707935930.png)

- storageSize : 物理檔案大小
- 刪除資料不會減少資料庫大小，只是把硬碟位置空出來，重複使用
- 釋放空間：compact [https://docs.mongodb.com/manual/reference/command/compact/](https://docs.mongodb.com/manual/reference/command/compact/)

[![image-1639708000892.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/mUDxLz2Iz2xMZzn2-image-1639708000892.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/mUDxLz2Iz2xMZzn2-image-1639708000892.png)

[![image-1639708353823.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/QLPhvFC28gLvwdnr-image-1639708353823.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/QLPhvFC28gLvwdnr-image-1639708353823.png)

# MongoDB教育訓練-07

- 只能保證分片唯一，不能保證全局唯一
- sparse index : 欄位可能不存在document中使用
- partial index : 依據欄位的值決定要不要放入索引（刪除的不放）

[![image-1639709435352.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/wWPaTmNWtK43dfFl-image-1639709435352.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/wWPaTmNWtK43dfFl-image-1639709435352.png)

- hashed indexes : md5 取前20個byte，並不可作為唯一索引，使用時機：控制索引大小（如url）
- 關於時間不適合，會造成分散在所有分片

[![image-1639709740460.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BNDz6MjJGH4XQVMy-image-1639709740460.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BNDz6MjJGH4XQVMy-image-1639709740460.png)

- 索引增加讀的效能，減低寫的效能
- 建議每張表的索引&lt; 10
- 新增資料會做索引異動，更新不會(但更新欄位會)

[![image-1639710154313.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/p9ksssT9foFnXp7U-image-1639710154313.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/p9ksssT9foFnXp7U-image-1639710154313.png)

索引越大，記憶體越大

[![image-1639710316926.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4zOuCSYrDtGgfnf8-image-1639710316926.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4zOuCSYrDtGgfnf8-image-1639710316926.png)

### 前綴壓縮

- <span style="color: #ff0000;">App</span> le
- <span style="color: #ff0000;">App</span> le iphon
- <span style="color: #ff0000;">App</span> le store

[![image-1639710384084.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/rrhpi4p8v1TQjucP-image-1639710384084.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/rrhpi4p8v1TQjucP-image-1639710384084.png)

### Multikey indexs

建在array上的index (是不是由系統判斷)

[![image-1639710538365.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4jvxLsF5Zv6B6JrQ-image-1639710538365.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4jvxLsF5Zv6B6JrQ-image-1639710538365.png)

[![image-1639710636236.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/xbwrPXNGHcrCzpeC-image-1639710636236.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/xbwrPXNGHcrCzpeC-image-1639710636236.png)

通常使用"comments.rating" : 1 這種索引

[![image-1639710866463.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/kQG5DV39z1YWECuY-image-1639710866463.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/kQG5DV39z1YWECuY-image-1639710866463.png)

[![image-1639711096453.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/EMgtxyUFyCFNrcC0-image-1639711096453.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/EMgtxyUFyCFNrcC0-image-1639711096453.png)

### Compound符合索引

多欄位索引 (欄位A &amp; 欄位B 查詢適用 )

[![image-1639711185981.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/1fStpr6XArU4nsF0-image-1639711185981.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/1fStpr6XArU4nsF0-image-1639711185981.png)

### Compound Indexes 複合索引

- MongoDB 通常使用<span style="color: #ff0000;">一個</span>索引作查詢
- 最多可以做32個
- 順序很重要

[![image-1639711281834.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bFZZMWa76Rs0PgBT-image-1639711281834.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bFZZMWa76Rs0PgBT-image-1639711281834.png)

[![image-1639711459539.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/gWtjM19sgBjXu1vs-image-1639711459539.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/gWtjM19sgBjXu1vs-image-1639711459539.png)

欄位順序必須相同

[![image-1639711603003.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FPft7ie7e0KvO9md-image-1639711603003.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FPft7ie7e0KvO9md-image-1639711603003.png)

[![image-1639711744724.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/EbJ0MfYoAaSPFIRg-image-1639711744724.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/EbJ0MfYoAaSPFIRg-image-1639711744724.png)

[![image-1639711763345.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/V53vAeoi5saMlNUs-image-1639711763345.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/V53vAeoi5saMlNUs-image-1639711763345.png)

[![image-1639711833183.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/NXkdKdxjZW2po5PQ-image-1639711833183.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/NXkdKdxjZW2po5PQ-image-1639711833183.png)

[![image-1639711867625.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/MiTyDRZgKHj8WhZM-image-1639711867625.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/MiTyDRZgKHj8WhZM-image-1639711867625.png)

[![image-1639711980197.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PO3Xs0ElcRvrZxDH-image-1639711980197.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PO3Xs0ElcRvrZxDH-image-1639711980197.png)

[![image-1639712284049.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/omFQETIWY1NDNVfY-image-1639712284049.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/omFQETIWY1NDNVfY-image-1639712284049.png)

[![image-1639712401606.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PjRvnyiSx4W82DeT-image-1639712401606.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PjRvnyiSx4W82DeT-image-1639712401606.png)

### ＥＳＲ原則

等值 排序 範圍

10000以內不算數據量大

[![image-1639712527794.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/X2qvWrX7SXv1CsYz-image-1639712527794.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/X2qvWrX7SXv1CsYz-image-1639712527794.png)

兩欄位都是陣列，禁止使用，因為所有數量是陣列的乘積

[![image-1639712629336.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7gfZXdRRZGTKJXQj-image-1639712629336.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7gfZXdRRZGTKJXQj-image-1639712629336.png)

###  

#### index 完全覆蓋查詢(資料都從index返回，不用直接查詢document)

\_id 不要出現, multikey 不適用

[![image-1639712718185.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/U5rt5J6oLLv4cPWC-image-1639712718185.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/U5rt5J6oLLv4cPWC-image-1639712718185.png)

[![image-1639712827734.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/QwW6VDciHMXheDrv-image-1639712827734.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/QwW6VDciHMXheDrv-image-1639712827734.png)

```shell
db.listingsAndReviews.find({amenities:"Waterfront","bed_type":{$in:["Futon","Real Bed"]}})
.projection({bedrooms:1, price:1,_id:0,"address.country":1}).explain("executionStats")


db.listingsAndReviews.createIndex({amenities:1, bedrooms:-1,price:11,bed_type:1,first_review:1,last_review:1})
```

# MongoDB教育訓練-08

[![image-1639720047048.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/LnCs1c7WApqvzvv1-image-1639720047048.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/LnCs1c7WApqvzvv1-image-1639720047048.png)

[![image-1639720141995.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/zm9dCzrCLAkNJpCj-image-1639720141995.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/zm9dCzrCLAkNJpCj-image-1639720141995.png)

[![image-1639720365185.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/hyEj4KfssItHj9Aj-image-1639720365185.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/hyEj4KfssItHj9Aj-image-1639720365185.png)

[![image-1639720514211.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/pgvR3xwhVR7f2OIz-image-1639720514211.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/pgvR3xwhVR7f2OIz-image-1639720514211.png)

### Ｗildcard Indexes

- 可以加入所有欄位，但只能命中一個欄位
- 儲存空間的最大
- 不知道要對什麼做搜索的時候適用

[![image-1639720611175.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/uk7pdMVZCU8dEJHi-image-1639720611175.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/uk7pdMVZCU8dEJHi-image-1639720611175.png)

- 使用hints指定索引
- 支援regular (只有前綴固定 ^開頭支援，範圍查詢)
- 可以設定collation (定序)

[![image-1639720820601.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/qngw3DEJxWRlMizI-image-1639720820601.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/qngw3DEJxWRlMizI-image-1639720820601.png)

- 增加一個索引增加10%消耗
- 記憶體最好容納最常需要的索引
- $indexStarts() 可以統計索引的命中率(server 服務以來)
- {a: 1} {a: 1,b:1} 前面這個與後面重複可以刪除

[![image-1639721057745.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/cDmnKgZn3z1Wn1F0-image-1639721057745.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/cDmnKgZn3z1Wn1F0-image-1639721057745.png)

[![image-1639721231592.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/C5FewfIzF4rxbhSq-image-1639721231592.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/C5FewfIzF4rxbhSq-image-1639721231592.png)

### 滾動索引

- 暫停一個結點建立索引，再加入叢集
- 重複兩次後將建好索引主機升為master，下master建索引

[![image-1639721445903.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Eam5oeYY7MU6DDJ3-image-1639721445903.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Eam5oeYY7MU6DDJ3-image-1639721445903.png)

index 先隱藏再刪除

[![image-1639721675102.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Hmqy7FhWbDJgRoft-image-1639721675102.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Hmqy7FhWbDJgRoft-image-1639721675102.png)

[![image-1639721774327.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/a4ldxj17EbY8rRDq-image-1639721774327.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/a4ldxj17EbY8rRDq-image-1639721774327.png)

# MongoDB教育訓練-09

查詢緩慢

- 沒有使用index

解決

- 查看database log
- 開啟 profiling

[![image-1639721837613.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/VxB6quNoXw59D2qD-image-1639721837613.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/VxB6quNoXw59D2qD-image-1639721837613.png)

[![image-1639721953030.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/eX8GGpQ2wpDf116l-image-1639721953030.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/eX8GGpQ2wpDf116l-image-1639721953030.png)

[![image-1639722038224.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ufhJqUKSnXZ7SEHc-image-1639722038224.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ufhJqUKSnXZ7SEHc-image-1639722038224.png)

level 2 性能會影響

[![image-1639722277872.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/QGO3hYQQM0zAyXZ3-image-1639722277872.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/QGO3hYQQM0zAyXZ3-image-1639722277872.png)

db.setProfilingLevel(1,5) #level 1, 查詢大於5毫秒

[![image-1639722367579.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/QI2cwOiCMNzCUrUw-image-1639722367579.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/QI2cwOiCMNzCUrUw-image-1639722367579.png)

---

### 慢查詢的原因

- 缺少index
- 寫入資料超出disk 性能
- 高CPU使用
- 巢狀join

[![image-1639722562542.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/0lef2dE3IDxIl9Fm-image-1639722562542.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/0lef2dE3IDxIl9Fm-image-1639722562542.png)

---

[![image-1639723682887.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/UC4AiylyoipWvR8X-image-1639723682887.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/UC4AiylyoipWvR8X-image-1639723682887.png)

[![image-1639723703346.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Qr9kRrZQbcNhThL5-image-1639723703346.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Qr9kRrZQbcNhThL5-image-1639723703346.png)

[![image-1639723975695.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/H3NOG4rJDGcaqBlb-image-1639723975695.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/H3NOG4rJDGcaqBlb-image-1639723975695.png)

[![image-1639724124142.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ybJiSLF4TbQmBTzK-image-1639724124142.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ybJiSLF4TbQmBTzK-image-1639724124142.png)

[![image-1639723989853.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Yqt1hEruRAL7S0xe-image-1639723989853.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Yqt1hEruRAL7S0xe-image-1639723989853.png)

[![image-1639724147258.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ejdOvHO91KXtbi4P-image-1639724147258.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ejdOvHO91KXtbi4P-image-1639724147258.png)

[![image-1639724475480.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/5zO0nzrtHpYPxbMo-image-1639724475480.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/5zO0nzrtHpYPxbMo-image-1639724475480.png)

[MongoDB Compass Download | MongoDB](https://www.mongodb.com/try/download/compass?tck=docs_compass)

[![image-1639724538158.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ce4cJzb96maJr7T6-image-1639724538158.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ce4cJzb96maJr7T6-image-1639724538158.png)

[![image-1639724837767.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7lGIDr1EPdAigO6W-image-1639724837767.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7lGIDr1EPdAigO6W-image-1639724837767.png)

[![image-1639725028397.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/XLwTmGbYiHbsTS7i-image-1639725028397.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/XLwTmGbYiHbsTS7i-image-1639725028397.png)

[![image-1639725068655.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/hEnDhowWWilE3kL4-image-1639725068655.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/hEnDhowWWilE3kL4-image-1639725068655.png)

[![image-1639725233859.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/DhOg54QUXLFGGxDZ-image-1639725233859.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/DhOg54QUXLFGGxDZ-image-1639725233859.png)

[![image-1639725262005.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/g1j8D8AgfsVG7tgj-image-1639725262005.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/g1j8D8AgfsVG7tgj-image-1639725262005.png)

[![image-1639725411915.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/vmxyYKJqoxaybrWB-image-1639725411915.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/vmxyYKJqoxaybrWB-image-1639725411915.png)

[![image-1639726437813.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/cxzxIMca9fhL52lF-image-1639726437813.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/cxzxIMca9fhL52lF-image-1639726437813.png)

[![image-1639726650401.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Jmp44ML3bOBXP0O5-image-1639726650401.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Jmp44ML3bOBXP0O5-image-1639726650401.png)

[![image-1639726779203.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/1TEZigJ62aqgiaud-image-1639726779203.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/1TEZigJ62aqgiaud-image-1639726779203.png)

[![image-1639727675464.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/H4GJTeQ67U3GZcEW-image-1639727675464.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/H4GJTeQ67U3GZcEW-image-1639727675464.png)

[![image-1639727685022.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/GnGQRtLft0j6ZNNU-image-1639727685022.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/GnGQRtLft0j6ZNNU-image-1639727685022.png)

[![image-1639727814455.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/kdGOflVBf1uVssfs-image-1639727814455.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/kdGOflVBf1uVssfs-image-1639727814455.png)

[![image-1639727848116.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/IJN81R5wMLmxxQ0m-image-1639727848116.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/IJN81R5wMLmxxQ0m-image-1639727848116.png)

[![image-1639729086371.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ULXwTEx0uINo8kEN-image-1639729086371.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ULXwTEx0uINo8kEN-image-1639729086371.png)

[![image-1639728235333.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/QyhN1K9QMu771Exp-image-1639728235333.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/QyhN1K9QMu771Exp-image-1639728235333.png)

# MongoDB教育訓練-20211223-01

Hideden Replica Set Member 隱藏結點，不太使用（歷史，所有請求不會訪問）

Delayed Replica Set Member 不太使用，先將資料取回不同步(延遲一段時間，保持舊資料，備援用)

[![image-1640224635457.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/XOGkmJFnD85OvMkq-image-1640224635457.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/XOGkmJFnD85OvMkq-image-1640224635457.png)

[![image-1640229898504.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/W9QymtxKIAMTIkvu-image-1640229898504.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/W9QymtxKIAMTIkvu-image-1640229898504.png)

- 如何知道主結點？ Driver 告訴你
- Driver 幫你追蹤網路架構，不需知道主從節點
- 為截點點打上標籤，通過driver，告訴你

[![image-1640225138949.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/mkaD8FGMUONjNWvt-image-1640225138949.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/mkaD8FGMUONjNWvt-image-1640225138949.png)

Driver 版本匹配

[![image-1640225409514.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/XUV6UBCZHoCy7WiL-image-1640225409514.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/XUV6UBCZHoCy7WiL-image-1640225409514.png)

[![image-1640225581875.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ljScMKeKsnerrfZT-image-1640225581875.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ljScMKeKsnerrfZT-image-1640225581875.png)

[![image-1640225640585.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/kjE3XuBBGE2pWXg7-image-1640225640585.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/kjE3XuBBGE2pWXg7-image-1640225640585.png)

[![image-1640225672880.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/K36gksltsXNU7Sp1-image-1640225672880.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/K36gksltsXNU7Sp1-image-1640225672880.png)

Oplog 會紀錄所有變更，增刪改查

[![image-1640225826529.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/VkuI6ZzjAZYi0Qy0-image-1640225826529.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/VkuI6ZzjAZYi0Qy0-image-1640225826529.png)

[![image-1640226097101.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/uoM1F32LgN72bL9r-image-1640226097101.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/uoM1F32LgN72bL9r-image-1640226097101.png)

[![image-1640226197262.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/MXHz4iiUS9dvzxIq-image-1640226197262.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/MXHz4iiUS9dvzxIq-image-1640226197262.png)

[![image-1640226289283.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/rFNNUT6zOP9kDniK-image-1640226289283.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/rFNNUT6zOP9kDniK-image-1640226289283.png)

節點從最近節點複製

[![image-1640226546216.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/xt0a7FrsUW8oVGJh-image-1640226546216.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/xt0a7FrsUW8oVGJh-image-1640226546216.png)

我們可以直接連線至從節點

[![image-1640226730042.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/6U4q1S3mIsM220jG-image-1640226730042.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/6U4q1S3mIsM220jG-image-1640226730042.png)

[![image-1640226806230.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ukiGN9JWAOhYyvMD-image-1640226806230.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ukiGN9JWAOhYyvMD-image-1640226806230.png)

[![image-1640226630457.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/AVf1EvlPoUtMYUx5-image-1640226630457.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/AVf1EvlPoUtMYUx5-image-1640226630457.png)

從節點查詢資料會出現告警，需下以下指令

db.getMongo().setReadPref("secondaryPreferred")

[![image-1640227049033.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/RBSiuFMGfU7k2rjQ-image-1640227049033.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/RBSiuFMGfU7k2rjQ-image-1640227049033.png)

[![image-1640227181462.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/UJowcNEosUMKYFZI-image-1640227181462.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/UJowcNEosUMKYFZI-image-1640227181462.png)

[![image-1640228152411.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/leh8xE0qxe8pz8C6-image-1640228152411.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/leh8xE0qxe8pz8C6-image-1640228152411.png)

[![image-1640228847213.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/CdrXbc5ZKv9PmB5a-image-1640228847213.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/CdrXbc5ZKv9PmB5a-image-1640228847213.png)

[![image-1640229625178.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/joQKDZO1k9gti5tX-image-1640229625178.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/joQKDZO1k9gti5tX-image-1640229625178.png)

[![image-1640229774613.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Te9XUXyWYniIPvDR-image-1640229774613.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Te9XUXyWYniIPvDR-image-1640229774613.png)

[![image-1640229875125.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/LnCVMxTrZg0zSxNT-image-1640229875125.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/LnCVMxTrZg0zSxNT-image-1640229875125.png)

總節點是奇數個才能選出主節點，剩下最新的會變成主節點

[![image-1640229994266.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/01gyMSjgcs4D3EFd-image-1640229994266.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/01gyMSjgcs4D3EFd-image-1640229994266.png)

網路斷開主節點還是存在

[![image-1640230063320.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/otVqDPjheDK2ZNF7-image-1640230063320.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/otVqDPjheDK2ZNF7-image-1640230063320.png)

需要有大多數才能投票，此主節點失效

[![image-1640230096608.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/GWTdbxzxI27wdAxk-image-1640230096608.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/GWTdbxzxI27wdAxk-image-1640230096608.png)

會可以投票主節點，DC1有機會成為主節點(資料較新？)

[![image-1640230277213.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Wlcc8tGbUqUerXZc-image-1640230277213.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Wlcc8tGbUqUerXZc-image-1640230277213.png)

[![image-1640230905006.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/m9u6CRTUvwLEjSbz-image-1640230905006.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/m9u6CRTUvwLEjSbz-image-1640230905006.png)

# MongoDB教育訓練-20211223-02

- 資料先寫入記憶體 ，
- checkpoint =&gt; 一分鐘( or 資料到達大小) 髒數據寫入DB
- journal log 60ms 寫入 db
- db 最多錯失60 ms 資料
- "OK - committed" =&gt; 寫到哪裡？ write concerns 決定
- 資料遺失的時候？我會知曉？
- knowingly log =&gt; 已知的寫入失敗
- unknowingly log =&gt; 未知的寫入失敗

[![image-1640237795605.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/51ki1KDJEyWkz1kV-image-1640237795605.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/51ki1KDJEyWkz1kV-image-1640237795605.png)

[![image-1640238366642.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/48LANg6oh5A8V3c4-image-1640238366642.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/48LANg6oh5A8V3c4-image-1640238366642.png)

[![image-1640238415549.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/LLjDAcWlqBdgN3Vs-image-1640238415549.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/LLjDAcWlqBdgN3Vs-image-1640238415549.png)

[![image-1640238497608.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/mamGXqUk0sI8jwSt-image-1640238497608.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/mamGXqUk0sI8jwSt-image-1640238497608.png)

當資訊不同步，server 必須 rollback 確保資訊一致 =&gt; { x: 100} (移除)

[![image-1640238562747.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/0mRZdFeDnlJE5hoo-image-1640238562747.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/0mRZdFeDnlJE5hoo-image-1640238562747.png)

[![image-1640238622753.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BZdfu2R1oiKJs0dI-image-1640238622753.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BZdfu2R1oiKJs0dI-image-1640238622753.png)

The case for Majority writes

in the previeous scenario:

date is writtten to the primary

primary acknowledges write to application

primary dies before secondary reads data

必須所以資料到達大多數節點(majoruty commit point)，才能稱之為持久化[![image-1640238761360.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ztBPvEq8CD8CwrRx-image-1640238761360.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ztBPvEq8CD8CwrRx-image-1640238761360.png)

[![image-1640238911776.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bHKAnFuE7H5KNaY8-image-1640238911776.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bHKAnFuE7H5KNaY8-image-1640238911776.png)

### 寫的關注度Write Concern

- OK, commitresd 意義
- (w: 到達多少個節點)(j: 如何才算寫成功 0:記憶體, 1:硬碟)
- w:0 不推薦
- w:1, j:1
- w: "majority" (j: 1)
- w:3 無意義(假設叢集有3節點，萬一1節點丟失，無法滿足)

[![image-1640239010111.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/UKzSJDUyJPnSJ7I8-image-1640239010111.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/UKzSJDUyJPnSJ7I8-image-1640239010111.png)

```shell
var wc = { w: 0 }
totalrecs = 10000
batchsize = 1
nbatches = totalrecs / batchsize
var start = new Date()
for(x=0;x<nbatches;x++) {
	recs = []
	for(y=0;y<batchsize;y++) {recs.push({a:1,b:"hello"})}
	db.test.insertMany(recs,{writeConcern: wc})
}

var end=new Date()
print(`${end-start} milliseconds`)
```

[![image-1640239636788.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/D2tSUBqCfLK9n7Dt-image-1640239636788.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/D2tSUBqCfLK9n7Dt-image-1640239636788.png)

### 讀的關注度 Read Concerns 

RDBS 隔離等級概念

Read local 主節點損毀才是髒讀

Read majority 讀舊一點的資料

Read snapshot :

Read Linearizable: 讀取時會確認資料是否到達大多數節點

[![image-1640240583240.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/iJFj4Cs5o6NUaKKk-image-1640240583240.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/iJFj4Cs5o6NUaKKk-image-1640240583240.png)

[![image-1640239853753.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/2kPdvY1LGbZGS2Ae-image-1640239853753.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/2kPdvY1LGbZGS2Ae-image-1640239853753.png)

### 讀取資料會從哪個節點讀？

- Read from nearest Geographically (從最近的節點 ping 值，最小的)
- Read from a specific set of servers (設定標記好的 tag server )

[![image-1640240657381.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FaHVQBXp6O5EAO0M-image-1640240657381.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FaHVQBXp6O5EAO0M-image-1640240657381.png)

### <span style="color: #ff0000;">非必要不使用</span>

[![image-1640240968077.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7GlKL03YZFWX7du3-image-1640240968077.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7GlKL03YZFWX7du3-image-1640240968077.png)

[![image-1640241039739.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BjmFLZk7hGaHyjg9-image-1640241039739.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BjmFLZk7hGaHyjg9-image-1640241039739.png)

# MongoDB教育訓練-20211223-03

vertical scaling 垂直擴展(升級到某個程度，會很貴並有極限)

horizontal sacling 水平擴展 (比較省成本)

[![image-1640242289465.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/WNPaHJCxNDV7y5ek-image-1640242289465.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/WNPaHJCxNDV7y5ek-image-1640242289465.png)

沒有一定要shard(如果資料不多不需要，管理複雜度高)

[![image-1640242412103.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/9wbUHEyWdAtGiHWa-image-1640242412103.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/9wbUHEyWdAtGiHWa-image-1640242412103.png)

[![image-1640242564943.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/k4B2vYQF9vFVLMHI-image-1640242564943.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/k4B2vYQF9vFVLMHI-image-1640242564943.png)

- why learn about sharding now?
- you might not need to shard for years
- but decisions you make about schema design befoe that matter
- start planning for sharding on day one

[![image-1640242581364.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/lwlqelUGcUlyNd54-image-1640242581364.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/lwlqelUGcUlyNd54-image-1640242581364.png)

[![image-1640242682522.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/nbXWY4DLAOypwauJ-image-1640242682522.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/nbXWY4DLAOypwauJ-image-1640242682522.png)

[![image-1640242841645.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/WA1BuRXuhph6yNSj-image-1640242841645.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/WA1BuRXuhph6yNSj-image-1640242841645.png)

mongos 屏蔽所有細節

[![image-1640242859417.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4pjQ98bgVwvRhQhI-image-1640242859417.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4pjQ98bgVwvRhQhI-image-1640242859417.png)

configuration server 決定資料分布

[![image-1640242920382.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/8vpdhQPhwezSA16S-image-1640242920382.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/8vpdhQPhwezSA16S-image-1640242920382.png)

[![image-1640242991219.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/udIFf8TK6WQ86Vat-image-1640242991219.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/udIFf8TK6WQ86Vat-image-1640242991219.png)

[![image-1640243006562.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/TciPkWgEPCb2dWeW-image-1640243006562.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/TciPkWgEPCb2dWeW-image-1640243006562.png)

[![image-1640243117106.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/eJGjPxByWF0mq62G-image-1640243117106.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/eJGjPxByWF0mq62G-image-1640243117106.png)

修改分片鍵

[![image-1640243247361.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/9t1Rsl2zJwqR6fvs-image-1640243247361.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/9t1Rsl2zJwqR6fvs-image-1640243247361.png)

選擇分片鍵原則

shard keys 不一定是要一樣的，重複的不可超過64MB

[![image-1640243397277.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/yJvQFYh4JP9ch7gx-image-1640243397277.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/yJvQFYh4JP9ch7gx-image-1640243397277.png)

[![image-1640243910637.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/pXt9gNUTgm7f9JOY-image-1640243910637.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/pXt9gNUTgm7f9JOY-image-1640243910637.png)

[![image-1640243925444.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/9ORq0VI5ydsIsIEs-image-1640243925444.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/9ORq0VI5ydsIsIEs-image-1640243925444.png)

[![image-1640244070167.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ti8OKD2bA7Om04oj-image-1640244070167.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ti8OKD2bA7Om04oj-image-1640244070167.png)

[![image-1640244228199.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/RGrDhrhpMgfLQqdY-image-1640244228199.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/RGrDhrhpMgfLQqdY-image-1640244228199.png)

[![image-1640244431054.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/m3MrqhOquZ4HBcS1-image-1640244431054.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/m3MrqhOquZ4HBcS1-image-1640244431054.png)

[![image-1640245827455.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/EvsLc9SiqIkkdFmv-image-1640245827455.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/EvsLc9SiqIkkdFmv-image-1640245827455.png)

[![image-1640246122904.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/aF9XYZxnA83gyNy7-image-1640246122904.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/aF9XYZxnA83gyNy7-image-1640246122904.png)

[![image-1640246580335.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/3vkpAb7OMpoNmKs1-image-1640246580335.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/3vkpAb7OMpoNmKs1-image-1640246580335.png)

[![image-1640246143964.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/6bGqPLLgJrVFwyv6-image-1640246143964.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/6bGqPLLgJrVFwyv6-image-1640246143964.png)

[![image-1640246907569.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/6n07LgrqZTiv1aXi-image-1640246907569.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/6n07LgrqZTiv1aXi-image-1640246907569.png)

#### 預分片

[![image-1640247049023.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/1XBWLTnq72Gwwj38-image-1640247049023.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/1XBWLTnq72Gwwj38-image-1640247049023.png)

範例 1000億筆資料

[![image-1640247152836.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FJ9Mgvm0w06JRLp6-image-1640247152836.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FJ9Mgvm0w06JRLp6-image-1640247152836.png)

ＹＣＳＢ壓測工具

[![image-1640247396813.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/MX2RzMWGwFipMVK8-image-1640247396813.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/MX2RzMWGwFipMVK8-image-1640247396813.png)

[![image-1640247645818.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/I4xvjQ0YY6zvvmfU-image-1640247645818.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/I4xvjQ0YY6zvvmfU-image-1640247645818.png)

[![image-1640247664050.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/8byDycHQAHcmPwcA-image-1640247664050.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/8byDycHQAHcmPwcA-image-1640247664050.png)

[![image-1640247809556.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4Y3SYEpqiGFJixD1-image-1640247809556.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4Y3SYEpqiGFJixD1-image-1640247809556.png)

primary shared 最忙

[![image-1640247884753.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7PxUBnmsctBGBQMm-image-1640247884753.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7PxUBnmsctBGBQMm-image-1640247884753.png)

完全停止

[![image-1640248229678.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/2fxzL3fhexEFquxI-image-1640248229678.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/2fxzL3fhexEFquxI-image-1640248229678.png)

[![image-1640248345321.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/vLTpEPOwoTqLepl9-image-1640248345321.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/vLTpEPOwoTqLepl9-image-1640248345321.png)

分散多筆sharding ，利用換取ＣＰＵ計算

[![image-1640248651952.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PlArjNlZLfbdB1gX-image-1640248651952.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PlArjNlZLfbdB1gX-image-1640248651952.png)

# MongoDB教育訓練-20211223-04-關於安全

[![image-1640248761008.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ySQYEekObyv3QadV-image-1640248761008.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ySQYEekObyv3QadV-image-1640248761008.png)

#### 對稱加密 &amp;&amp; 非對稱加密

[![image-1640248839685.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Q8XvluDNeNNpytsW-image-1640248839685.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Q8XvluDNeNNpytsW-image-1640248839685.png)

[![image-1640248938126.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/It7SCKEtnipwfTr0-image-1640248938126.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/It7SCKEtnipwfTr0-image-1640248938126.png)

[![image-1640248975046.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/osit9OM9JSbWq2MX-image-1640248975046.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/osit9OM9JSbWq2MX-image-1640248975046.png)

[![image-1640249034932.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/JacBAaOpvBvfiSV7-image-1640249034932.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/JacBAaOpvBvfiSV7-image-1640249034932.png)

[![image-1640249067545.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/id9uXrE6WW9cuzzT-image-1640249067545.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/id9uXrE6WW9cuzzT-image-1640249067545.png)

[![image-1640249240963.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/NfI2UR7LAaHd9367-image-1640249240963.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/NfI2UR7LAaHd9367-image-1640249240963.png)

[![image-1640249310207.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bkl9fBQIVdO3xUNP-image-1640249310207.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bkl9fBQIVdO3xUNP-image-1640249310207.png)

[![image-1640249547717.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/MjKSfKiciXWufhpe-image-1640249547717.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/MjKSfKiciXWufhpe-image-1640249547717.png)

[![image-1640249574633.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Dv7NW6q2ic340V2v-image-1640249574633.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Dv7NW6q2ic340V2v-image-1640249574633.png)

[![image-1640249605046.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/Ts7kmUxz4kLb0jv4-image-1640249605046.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/Ts7kmUxz4kLb0jv4-image-1640249605046.png)

[![image-1640249683383.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/pcAor5Heq8Gj4Uzh-image-1640249683383.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/pcAor5Heq8Gj4Uzh-image-1640249683383.png)

[![image-1640249745767.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/vk2opfSRIIjBFhuA-image-1640249745767.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/vk2opfSRIIjBFhuA-image-1640249745767.png)

[![image-1640249753776.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/KKcHEp38w17qGvd4-image-1640249753776.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/KKcHEp38w17qGvd4-image-1640249753776.png)

[![image-1640249776866.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/x2lJM33ncrFjEyGp-image-1640249776866.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/x2lJM33ncrFjEyGp-image-1640249776866.png)

[![image-1640249808115.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/nqJk2PVnPyCt5goV-image-1640249808115.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/nqJk2PVnPyCt5goV-image-1640249808115.png)

[![image-1640249932583.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/XbsiFAvbAgKPTXoG-image-1640249932583.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/XbsiFAvbAgKPTXoG-image-1640249932583.png)

[![image-1640249992603.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/VU4U1bSs9zQrp5TO-image-1640249992603.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/VU4U1bSs9zQrp5TO-image-1640249992603.png)

[![image-1640250016804.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/gyGSRS3eNoKiMP9G-image-1640250016804.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/gyGSRS3eNoKiMP9G-image-1640250016804.png)

[![image-1640250154038.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/ob2lCOzhtWWRoUv2-image-1640250154038.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/ob2lCOzhtWWRoUv2-image-1640250154038.png)

[![image-1640250302044.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/dOnK0REsGmhhsako-image-1640250302044.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/dOnK0REsGmhhsako-image-1640250302044.png)

[![image-1640250446209.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/sWIVjFXtfE1HBFgb-image-1640250446209.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/sWIVjFXtfE1HBFgb-image-1640250446209.png)

[![image-1640250681849.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/8LHaIdePG0IXgFMb-image-1640250681849.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/8LHaIdePG0IXgFMb-image-1640250681849.png)

[![image-1640250846041.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/jzeUQLcn6iJrCA32-image-1640250846041.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/jzeUQLcn6iJrCA32-image-1640250846041.png)