--- tags: Node.js 直播班 - 2023 春季班 --- # 🏅 Day 3 - MongoDB 基本操作:新增、查詢 ### 前言 MongoDB 是一個 NoSQL 資料庫(Not only SQL,針對不同於傳統的關聯式資料庫的資料庫管理系統的統稱),一套以文件(`document`)導向的資料庫管理系統,相較於傳統的關聯式資料庫,非關聯式資料庫的特性讓 MongoDB 在處理巨量資料有更大的支援 MongoDB 會以 **BSON** 的形式儲存資料(Binary JSON),相較於 JSON 可以儲存的資料類型更多 MongoDB 整體資料結構為:`db` -> `collection` -> `document` 我們可以透過指令(The `mongo` Shell)的方式用終端機來操作資料庫 ### 新增 #### 新增單筆資料 `collection` 需替換為資料庫中 collection 的名稱,例如:`db.users.insertOne()` ```javascript= db.collection.insertOne( { example: "text" } ) ``` #### 新增多筆資料 ```javascript= db.collection.insertMany( { example: "text" }, { example: "text1" }, { example: "text2" } ... ) ``` 執行新增成功後會為每一筆 document 都新增不同的 `_id` 例如執行新增多筆資料後會出現 ```javascript= { "acknowledged" : true, "insertedIds" : [ ObjectId("61ec1cf32fecb74092ce1463"), ObjectId("61ec1cf32fecb74092ce1464"), ObjectId("61ec1cf32fecb74092ce1465") ] } ``` ### 查詢 ``` db.collection.find() ``` 直接執行此指令會將此 collection 中的資料全部列出 若是要查詢特定資料可以在 `find()` 帶入`{ 屬性: 值 }` 尋找符合條件的資料 例如: ``` db.collection.find({ example: "text"}) ``` 屬性值可以搭配運算子設定條件: 使用**比較運算子**設定篩選條件 | $eq | 等於 | | ---- | ------------ | | $ne | 不等於 | | $gt | 大於 | | $lt | 小於 | | $gte | 大於等於 | | $lte | 小於等於 | | $in | 存在某個值 | | $nin | 不存在某個值 | 例:找出此 colletion 中符合 example 屬性值等於 `"text"` 的資料 ``` db.collection.find({ example: { $eq: "text"}}) ``` `使用以下邏輯運算子` | $and | 全部條件皆符合 | | ---- | ----------- | | $or | 符合其中一項條件| | $nor | 全部條件皆不符合| | $not | 與條件相反 | 例:找出此 colletion 中符合以下條件之一的資料: status 屬性為 `A`,或 qty 值小於 30 ``` db.collection.find({ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] }) ``` 也可以帶入**正規表達式**篩選有符合的文字 例:查詢 example 有包含 `text` 的資料 ``` db.collection.find({ example: /text/}) ``` ### 參考資源 - [insertOne()]( https://docs.mongodb.com/v4.4/reference/method/db.collection.insertOne/#db.collection.insertone--) - [insertMany()](https://docs.mongodb.com/v4.4/reference/method/db.collection.insertMany/#db.collection.insertmany--) - 課程影音「新增資料:insertOne、insertMany」 - [db.collection.find()](https://www.mongodb.com/docs/v4.4/reference/method/db.collection.find/#db.collection.find--) - [Query and Projection Operators — MongoDB Manual](https://www.mongodb.com/docs/v4.4/reference/operator/query/#comparison) - 課程影音「MongoDB 簡介」 ## 題目(將答案寫在 HackMD 並提交至回報區) 請建立一個 database(名稱可自定義),並建立一個 `students` collection 將答案依序列在 HackMD 並將連結貼至回報區 ``` 範例: 1. ... 2. ... 3. ... 4. ... 5. ... 6. ... ``` 1. 依以下格式新增一筆 document 到 `students` collection ```jsonld= { "studentName": "Riley Parker", "group": "A", "score": 83, "isPaid": false } ``` 範例: ```javascript= db.students.insertOne({ "studentName": "Riley Parker", "group": "A", "score": 83, "isPaid": false }) ``` 2. 依以下格式一次新增多筆 document 到 `students` collection ```jsonld= { "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 } ``` 3. 查詢 `students` collection 中的所有資料 4. 查詢 `students` collection 中符合 group 屬性為 B 的資料 `使用 { <field>: <value> } 設定符合的項目` 5. 查詢 `students` collection 中符合分數在 60 分以上的的資料 6. 查詢 `students` collection 中符合分數在 60 分以下**或是** group 為 B 的資料 ## 回報流程 將答案連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/vftL5i0.png) <!-- 解答: 1. db.students.insertOne({ "studentName": "Riley Parker", "group": "A", "score": 83, "isPaid": false }) 2. db.students.insertMany([ { "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 } ]) 3. db.students.find() 4. db.students.find({ group: "B" }) 5. db.students.find({ score: {$gte: 60} }) 6. db.students.find({ $or: [ { score: {$lte: 60}, { group: "B"} ] }) --> 回報區 --- | 報數 | 組別/Discord 名字 | Codepen/HackMD/其他回饋 | |:----:|:-------------------:|:-----------------------------------------------------------------------------------------------------------------------------------:| | 1 | 北 1 組 / PittWu | [Blog Post - Day 3](https://pitt-wu-blog.vercel.app/docs/day3-mongodb-add-find) | | 2 | 北 10 組 / Benson | [Github Benson - Day 3](https://github.com/ioveasdkre/HexschoolOperation/tree/main/NodejsEnterpriseClass/day40-tasks/day3/index.md) | | 3 | 北 5 組 / 圈圈 | [HackMD - Day 3](https://hackmd.io/xa1QI1G8R2qqmMpsbQeAYg?view) | | 4 | 北 6 組 / pamiya | [HackMD - Day 3](https://hackmd.io/3DttxlVMQeKV_r_VAuy-yQ?view) | | 5 | 北 1 組 / MayYi | [HackMD - Day 3](https://hackmd.io/@ykVObTPdRK6pITZSrbY59w/H1plZT-lh) | | 6 | 北 8 組 / Zhao Chen | [GitHub - Day 3](https://github.com/zhao1995chen/NodejsEnterpriseClass/blob/master/daily-task/day3/README.md) | | 7 | 北 11 組 / Eddie | [Notion - Day 3](https://olive-amethyst-a70.notion.site/day3-d9b8ed4c11ef46c7a0629d6a76d11872) | | 8 | 南 2 組 / fengyao | [GitHub - Day 3](https://github.com/fengyao33/sixfeet/blob/main/pratice3-Standard%20Operator%20MongoDB.ts) | | 9 | 中 3 組 / Wendy | [Notion - Day 3](https://jewel-cellar-80e.notion.site/Day-3-MongoDB-d32528e1a80a4c45aa392d42ddcef7cd) | | 10 | 中 4 組 / jimkk159 | [HackMD - Day 3](https://hackmd.io/5LPG5uMiRP2zQzcgTN1xCA) | | 11 | 北 12 組 / Sam Lin | [GitHub - Day 3](https://github.com/samlin1122/hex-school/blob/main/daily-challenges/day3.md) | | 12 | 北 8 組 / Ryder | [HackMD - Day 3](https://hackmd.io/pIvqpyoDTyaA3z5xRP0O1g) | | 13 | 南 2 組 / Kai | [HackMD - Day 3](https://hackmd.io/@gHMwdmliTqejhyvnHgVoEw/S1gqstxZ3) | | 14 | 北 8 組 / Ryan | [HackMD - Day 3](https://hackmd.io/@GJBsptBnTfmBiYxd24VliA/rJIFDTpPo) | | 15 | 北 13 組 / Louisa | [GitHub - Day 3](https://github.com/louisa0416/NodejsEnterpriseClass/tree/master/daily-task/day03) | | 16 | 南 1 組 / hiYifang | [HackMD - Day 3](https://hackmd.io/@gPeowpvtQX2Om6AmD-s3xw/rJGVhWXEq) | | 17 | 北 16 組 Zoe Wu | [ GitHub - Day 3 ](https://github.com/Zoe561/hexschool/blob/master/nodejs-enterprise-class/daily-task/day3/mongo-shell-day3.md) | | 18 | 北5組 albee | [GitHub-Day 3](https://github.com/albee-chang/hexschool-node-dailytask/blob/main/day3/app.md)| | 19 | 北 1 組 / Asher | [Notion - Day 3](https://wh-lin.notion.site/Day3-MongoDB-a1aa01496fa74e92a172cb46997389a8)| | 20 | 北 16 組 / 啊培培 | [GitHub - Day 3](https://github.com/LABIBI-LG/Courses/tree/main/hexschool/nodeJS/Live_Course/Daily_Tasks/day3)|