---
tags: Node.js 直播班 - 2023 春季班
---
# 🏅 Day 4 - MongoDB 基本操作:修改、刪除
### 修改
#### 修改單筆資料
`updateOne()` 第一個參數透過設定符合的屬性找出要修改的資料,若第一個參數帶入空物件 `{}`,則會選取全部資料的第一筆資料,第二個參數放入要修改的屬性,需先加上 `$set` 物件,並在此物件中帶入要修改的物件屬性
第二個參數可選擇帶入 `$currentDate` 屬性,會再修改資料成功後顯示出最新修改的時間
```javascript=
db.collection.updateOne(
{
example: "text"
},
{
$set: {
example: "texttext"
},
$currentDate: {
lastModified: true
}
}
)
```
#### 修改多筆資料
```javascript=
db.collection.updateMany(
{
example: /text/
},
{
$set: {
example: "texttext"
},
$currentDate: {
lastModified: true
}
}
)
```
### 刪除
#### 刪除一筆資料
只刪除一筆符合條件的資料
```javascript=
db.collection.deleteOne(
{
example: /text/
}
)
```
#### 刪除多筆資料
```javascript=
db.collection.deleteMany(
{
example: /text/
}
)
```
#### 刪除全部資料
```javascript=
db.collection.deleteMany({})
```
### 參考資源
- [db.collection.updateOne() — MongoDB Manual](https://www.mongodb.com/docs/v4.4/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne)
- [db.collection.updateMany() — MongoDB Manual](https://www.mongodb.com/docs/v4.4/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany)
- [db.collection.deleteOne() — MongoDB Manual](https://www.mongodb.com/docs/v4.4/reference/method/db.collection.deleteOne/#mongodb-method-db.collection.deleteOne)
- [db.collection.deleteMany() — MongoDB Manual](https://www.mongodb.com/docs/v4.4/reference/method/db.collection.deleteMany/#mongodb-method-db.collection.deleteMany)
- 課程影音「更新資料 - updateOne、updateMany」
- 課程影音「刪除資料 - deleteOne、deleteMany」
- 課程影音「尋找資料 - find 關鍵字搜尋」
## 題目(將答案寫在 HackMD 並提交至回報區)
若尚未做前一天的每日任務,需先建立一個 database(名稱可自定義),並建立一個 `students` collection
將以下資料新增至 `students` collection(若已做完前一天的每日任務,可繼續沿用已建立的 `students` collection)
```jsonld=
{
"studentName": "Riley Parker",
"group": "A",
"score": 83,
"isPaid": false
},
{
"studentName": "Brennan Miles",
"group": "C",
"score": 72,
"isPaid": false
},
{
"studentName": "Mia Diaz",
"group": "B",
"score": 98,
"isPaid": true
},
{
"studentName": "Caroline morris",
"group": "B",
"score": 55,
"isPaid": false
},
{
"studentName": "Beverly Stewart",
"group": "B",
"score": 60,
"isPaid": false
}
```
將答案依序列在自己的 HackMD 並將連結貼至回報區
```
範例:
1. ...
2. ...
3. ...
4. ...
```
1. 指定其中一個 `_id` ,並將該筆 document 的 `group` 改為 `D`
2. 將 `group` 為 `B` 的多筆 document 的 `isPaid` 改為 `true`
3. 將 `studentName` 包含關鍵字 `Brennan` 的 document 刪除
4. 將 `isPaid` 為 `true` 的多筆 document 刪除
## 回報流程
將答案連結貼至底下回報就算完成了喔!
解答位置請參考下圖(需打開程式碼的部分觀看)

<!-- 解答:
1.
db.students.updateOne(
{_id:ObjectId("624b2c7b7753d6dfb817f2c5") },
{ $set: { group: "D" }}
)
2.
db.students.updateMany(
{ group: "B" },
{ $set: { isPaid: true } }
)
3.
db.students.deleteOne(
{ studentName: /Brennan/ }
)
4.
db.students.deleteMany(
{ isPaid: true }
)
-->
回報區
---
| 報數 | 組別/Discord 名字 | Codepen/HackMD/其他回饋 |
|:----:|:-------------------:|:-----------------------------------------------------------------------------------------------------------------------------------:|
| 1 | 北 10 組 / Benson | [Github Benson - Day 4](https://github.com/ioveasdkre/HexschoolOperation/tree/main/NodejsEnterpriseClass/day40-tasks/day4/index.md) |
| 2 | 北 1 組 / PittWu | [Blog Post - Day 4](https://pitt-wu-blog.vercel.app/docs/day4-mongodb-edit-delete) |
| 3 | 北 5 組 / 圈圈 | [HackMD - Day 4](https://hackmd.io/mQbwgBiGS4C1q5W4penG8A?view) |
| 4 | 北 8 組 / Zhao Chen | [GitHub - Day 4](https://github.com/zhao1995chen/NodejsEnterpriseClass/blob/master/daily-task/day4/README.md) |
| 5 | 北 1 組 / MayYi | [HackMD - Day 4](https://hackmd.io/mQkeskXPRWWWx8zDKK8BYg) |
| 6 | 北 6 組 / pamiya | [HackMD - Day 4](https://hackmd.io/e0Ag6DqnSpqy_ZqzcHildQ?view) |
| 7 | 南 2 組 / fengyao | [GitHub - Day 4](https://github.com/fengyao33/sixfeet/blob/main/pratice4-Modify%20MongoDB.ts) |
| 8 | 北 11 組 / Eddie | [Notion - Day 4](https://olive-amethyst-a70.notion.site/day4-mongosh-d2982c42e3914e1bb4e5bcbb6b488f35) |
| 9 | 中 3 組 / Wendy | [Notion - Day 4](https://jewel-cellar-80e.notion.site/Day-4-MongoDB-f4eb870ef4f94e358a236f65fcd66d14) |
| 10 | 中 4 組 / jimkk159 | [HackMD - Day 4](https://hackmd.io/cgONivgjSzeANjBYqaHupQ) |
| 11 | 北 12 組 / Sam Lin | [GitHub - Day 4](https://github.com/samlin1122/hex-school/blob/main/daily-challenges/day4.md) |
| 12 | 北 8 組 / Ryder | [HackMD - Day 4](https://hackmd.io/glZ8OvQ1Q06WA37iKfAi3A) |
| 13 | 南 2 組 / Kai | [HackMD - Day 4](https://hackmd.io/@gHMwdmliTqejhyvnHgVoEw/Sks8ecxbn) |
| 14 | 北 8 組 / Ryan | [HackMD - Day 4](https://hackmd.io/@GJBsptBnTfmBiYxd24VliA/H15hpUoSo) |
| 15 | 北 13 組 / Louisa | [GitHub - Day 4](https://github.com/louisa0416/NodejsEnterpriseClass/tree/master/daily-task/day04) |
| 16 | 南 1 組 / hiYifang | [HackMD - Day 4](https://hackmd.io/@gPeowpvtQX2Om6AmD-s3xw/rJGVhWXEq) |
| 17 | 北 16 組 / Zoe Wu | [GitHub - Day 4](https://github.com/Zoe561/hexschool/blob/master/nodejs-enterprise-class/daily-task/day4/mongo-shell-day4.md) |
|18|北5組 / albee|[GitHub-Day 4](https://github.com/albee-chang/hexschool-node-dailytask/blob/main/day4/app.md)|
| 19 | 北 1 組 / Asher | [Notion - Day 4](https://wh-lin.notion.site/Day4-MongoDB-Asher-19212e472fea4784aec53bb62c709718) |
|18|北16組 / 啊培培|[GitHub-Day 4](https://github.com/LABIBI-LG/Courses/tree/main/hexschool/nodeJS/Live_Course/Daily_Tasks/day4)|