--- tags: Node.js 直播班 - 2022 春季班 --- # 🏅 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](https://courses.hexschool.com/courses/1670869/lectures/38579072)(章節影片) [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 簡介](https://courses.hexschool.com/courses/1670869/lectures/38565205)(章節影片) ### 題目(將答案寫在 HackMD 並提交至回報區) 請建立一個 database(名稱可自定義),並建立一個 `students` collection 將答案依序列在 HackMD 並將連結貼至回報區 ``` 範例: 1. ... 2. ... 3. ... 4. ... 5. ... 6. ... ``` 1. 依以下格式新增一筆 document 到 `students` collection ```json { "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 ```json { "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 中的所有資料 ``` db.students.find() ``` ![](https://i.imgur.com/eenn1Fa.png) 4. 查詢 `students` collection 中符合 group 屬性為 B 的資料 `使用 { <field>: <value> } 設定符合的項目` ``` db.students.find({'group':'B'}) ``` ![](https://i.imgur.com/8oNf1sS.png) 5. 查詢 `students` collection 中符合分數在 60 分以上的的資料 ``` db.students.find({'score':{'$gte':60}}) ``` ![](https://i.imgur.com/hiISzhn.png) 6. 查詢 `students` collection 中符合分數在 60 分以下**或是** group 為 B 的資料 ``` db.students.find({'$or':[{'group':'B'},{'score':{'$gte':60}}]}) ``` ![](https://i.imgur.com/T3OXxPx.png)