# 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)