六角學院 - HexSchool
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- 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 中的所有資料 4. 查詢 `students` collection 中符合 group 屬性為 B 的資料 `使用 { <field>: <value> } 設定符合的項目` 5. 查詢 `students` collection 中符合分數在 60 分以上的的資料 6. 查詢 `students` collection 中符合分數在 60 分以下**或是** group 為 B 的資料 回報流程 --- 請同學依照下圖教學觀看解答、回報答案: ![](https://i.imgur.com/QtL8zEW.png) 回報格式如下圖,請在「回報區」使用註解回報答案 (為了統計人數,請同學依序加上「報數」) ![](https://i.imgur.com/L7kyew8.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"} ] }) --> 回報區 --- | 報數 | 組別 / 名字 | codepen / hackMD / 其他回饋 | |:----:|:------------------------:|:-------------------------------------------------------------------------:| | 1 | 第 6 組 / Wendy | [hackMD](https://hackmd.io/@0k_MSPdgRPujozP6w_D-bA/HJ961UGE9) | | 2 | 第 2 組 / Jin | [hackMD](https://hackmd.io/WNpVVERdRD-cJ4ikeLEH8A) | | 3 | 第 4 組 / sihle | [HackMD](https://hackmd.io/@bugbug777/B1ibzIM4c) | | 4 | 第 4 組 / 小宥 | [HackMD](https://hackmd.io/5TsXYcZDRGK2ej3PhU_QIQ) | | 5 | 第 2 組 / Vic | [HackMD](https://hackmd.io/2o94NOCARrWXSLli2Ze0dg) | | 6 | 第 2 組 / Genos | [HackMD](https://hackmd.io/5A8AcAMeSSOA96WvjFH48g) | | 7 | 第 8 組 / Hank | [HackMD](https://hackmd.io/@TFOivyvXT-qpG6SieUTfgw/HyN36vMVq) | | 8 | 第 3 組 / Hobby | [HackMD](https://hackmd.io/@KsGqQaKHS6u944AvrPGpTA/BJkOqDf49) | | 9 | 第 2 組 / peter.chen1024 | [HackMD](https://hackmd.io/@peterchen1024/SJDITwfE9) | | 10 | 第 7 組 / 程翔 | [HackMD](https://hackmd.io/0kA2rze7RT-rf9tjRNuxwQ) | | 11 | 0 / Aya | [HackMD](https://hackmd.io/@NoName21/W1D3-MongoSH) | | 12 | 第 4 組 / 苡安 | [HackMD](https://hackmd.io/@L7K9-66lSeagS28AP0_GjQ/HyR9zjfNq) | | 13 | Naiky | [HackMD](https://hackmd.io/@UWBC7rrORiKaLSBg226mZg/rycN4ozV9) | | 14 | 第 6 組 / Ruta | [HackMD](https://hackmd.io/-dSuPCXuS3eCzpXVuW-W9A?view) | | 15 | 第 3 組 / Reynold | [HackMD](https://hackmd.io/@dL7AuQEMQ6KpCSRHaYElOA/HJXHn3zNq) | | 16 | 第 5 組 / sihyun | [HackMD](https://hackmd.io/qS-IZ5pmSYaWsurOsULuYQ) | | 17 | 第 7 組 / jason06286 | [HackMD](https://hackmd.io/nJ1B5YpzSGyWpk6qv_9GMQ) | | 18 | 第 9 組 / konstante | [HackMD](https://hackmd.io/x4p2JfiNTuKD_TlRRyxJdg?view) | | 19 | 第 3 組 / HedgehogKU | [HackMD](https://hackmd.io/qhFTumvMTxGpwsikeu4NMA) | | 20 | 第 2 組 / wendy.li | [HackMD](https://hackmd.io/UdTrCWGvSXKGJQmwlP9SJQ) | | 21 | 第 4 組 / Mischa | [HackMD_mischa_day3](https://hackmd.io/@93vGUSybS_OZz3597y4GLg/By22ol7Vc) | | 22 | 第 3 組 / 小葉 | [HackMD](https://hackmd.io/@FyKv37KcRSWqAO_e336w8g/HkdhRymV5) | | 23 | 第 1 組 / Jerry Huang | [HackMD](https://hackmd.io/WtyeHrQETkiw0wKI93k20w) | | 24 | 第 1 組 / Snow | [HackMD](https://hackmd.io/@snowsuika/rkYvUWQVq) | | 25 | 第 9 組 / 黃士桓 | [HackMD](https://hackmd.io/@huan5678/SkFxmZ7N5) | | 26 | 第 11 組 / Han Lai | [Codepen](https://codepen.io/x94han/pen/yLpqdRp) | | 27 | 第 14 組 / East | [HackMD](https://hackmd.io/BXoA17qRSMWtjUMxK3kMNQ) | | 28 | 第 3 組 / hiYifang | [HackMD](https://hackmd.io/@gPeowpvtQX2Om6AmD-s3xw/rJGVhWXEq) | | 29 | 第 14 組 / ChloeLo | [HackMD](https://hackmd.io/2VG4_puMRaKNk0kRXoja7A) | | 30 | 第 15 組 / Tofu | [HackMD](https://hackmd.io/T5auwvLRQL2ViH16vM0Gqw) | | 31 | 第 3 組 / Larry |[HackMD](https://hackmd.io/Y4CW-SY1Qxya9qyp7_JlSQ?view)| | 32 | 第 1 組 / Phoebe |[HackMD](https://hackmd.io/@Phoebe26/r1ZK6JNV5)| | 33 | 第 12 組 / Jimmy |[HackMD](https://hackmd.io/2tT5cjMdT7yWExn8R7bQOQ)| | 34 | 第 3 組 / Justin |[HackMD](https://hackmd.io/leJMEjaQSLqXsTFxEaNgjQ)| | 35 | 第 1 組 / Claire |[HackMD](https://hackmd.io/FAQ80N5tR-ui2iHn909tCw)| | 36 | 第 3 組 / Adam Hsu |[HackMD](https://hackmd.io/@S9gD__kVTSiQUupQNn_4FQ/H1RXu84E9) | | 36 | 第 3 組 / charlie ku |[HackMD](https://hackmd.io/@lbbU3USpSy-OqcOs9r4nUw/H11ej8VV5) | | 37 | 第 16 組 / 皓皓 |[Codepen](https://codepen.io/cutecat8110/pen/rNpZZGo) | | 38 | 海底藍 |[HackMD](https://hackmd.io/@CYndFsJLT7CA0N5Bx7tmcg/r1boSvEN5) | | 39 | 第14組 / 涼二 |[HackMD](https://hackmd.io/@Ryoni/B1CHwP4Nc) | |40|第13組/KFC|[HackMD](https://hackmd.io/nRmf_NrFQYSY9DS5Adfsaw?view) | 41 | 第15組 / Chiu |[HackMD](https://hackmd.io/8g0leXvWR8KrYuI5YEIkBw) | 42 | 第6組 / 謦麟 |[HackMD](https://hackmd.io/RQ6h3LrmQAeUut0__dyh8Q?view) | 43 | 第2組 / Rikkubook |[HackMD](https://hackmd.io/z36NsovaQzq5yl3W8F4f5Q?both) | 44 | 第15組 / yolala |[HackMD](https://hackmd.io/AsDiaUnSQzGoN0Oizrjecg?both) | 45 | 第 1 組 / Ed Huang |[HackMD](https://hackmd.io/xeIFhlBUTmGyVMY4U_RPAw) | 46 | 第 2 組 / joe chang |[HackMD](https://hackmd.io/aDoseBBBRVSxTY-Ke11BYQ) | 47 | nick6303 |[HackMD](https://hackmd.io/edpc0-9SRQ2l5G5rbJLozQ) | 48. | 第14組/Uniza | [HackMD](https://hackmd.io/WykVBYJoTOi2A9aYK43Cig)| | 49 | 第6組/馨云 | [HackMD](https://hackmd.io/@ceLocii_Tk-5B0mgomZ5Xw/ByEu1eDE5)| | 50| 第11組/mandy| [HackMD](https://hackmd.io/cbCNBY6cTZyeT-7P1SXOWA)| | 51| 第10組/Otis| [HackMD](https://hackmd.io/7kSCWx2uRcmBgy07mipL7Q?view)| | 52| 第5組/Nap| [HackMD](https://hackmd.io/GuRETK8lT0GcU_1i79orQg)| | 53| 第5組 Hazel | [HackMD@Hazel](https://hackmd.io/@hazelwu/day3)| | 54| 第10組 / 橘子 | [HackMD](https://hackmd.io/XTu-ACisTEuPwizi9vG1Tw?view)| | 55| 第10組 / Butters | [HackMD](https://hackmd.io/ALBNpaM7TnOOW3C81T6y7g?view)| | 56 | 第 1 組 / Emily Hsi | [HackMD](https://hackmd.io/@EmilyHsi/r1WmFjs4c)| | 57 | 第 8 組 / Jordan Tseng | [HackMD](https://hackmd.io/aV0MGvgHTC2G1yimQ3AcSA)| | 58 | 第 8 組 / Albert | [HackMD](https://hackmd.io/@Albertnotes/HyZm-U3Ec)| | 59 | 第 5 組 / AmberCYT | [HackMD](https://hackmd.io/@amber871023/HkzRYKzB5/%2F0r-JPfkZQD2u2kzXgkQEMw)| | 60 | 第 7 組 / TracyChien | [HackMD](https://hackmd.io/@H0MPUhcWR12nImVv1qJyLA/HySSeXYrc)| | 61 | 第 14 組 / 皮皮 | [HackMD](https://hackmd.io/Zff-hwY_T1aUexT7t3lzug)|

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully