# 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"}]})
```