六角學院 - 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
    • Invite by email
      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 Versions and GitHub Sync Sharing URL Help
Menu
Options
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
  • Invite by email
    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: Node.js 直播班 - 2022 春季班 --- # 🏅 Day 6 ## Mongoose、schema ### Mongoose Mongoose 是 MongoDB 的 ODM(Object Data Modeling) 套件,Mongoose 套件會藉由 MongoDB driver 操作資料庫的資料 使用 ODM 通常可以降低開發和維護成本,因 ODM 會使用 JavaScript 的物件反映出資料庫中的資料,相對於使用資料庫原生的查詢語言 (SQL),用 ODM 的方式操作資料庫會更好上手 - 在專案中安裝 Mongoose ```javascript npm install mongoose --save ``` - 引入 mongoose 套件並連線 MongoDB 資料庫 ```javascript= const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/test'); // test 為資料庫的名稱,可以改為自己的資料庫名稱 ``` ### Schema 安裝 Mongoose 並連線至資料庫後,接著可以開始建立 Schema(資料庫綱要),定義需要哪些資料、型別、是否顯示、預設值...等等 例如:[文件](https://mongoosejs.com/docs/guide.html)中列出 Blog 的 Schema ```javascript= const blogSchema = new mongoose.Schema({ title: String, // String is shorthand for {type: String} author: String, body: String, comments: [{ body: String, date: Date }], date: { type: Date, default: Date.now }, hidden: Boolean, meta: { votes: Number, favs: Number } }); ``` 當接收到資料,就可以依此 Schema 把關資料是否帶入正確 Schema 中會有需要帶入的資料屬性,title、author 等等,並會針對此資料加入相關設定,例如:型別、必填、預設...等等 #### type ```javascript title: {type: String} // 代表 title 需為一個字串 ``` 若只有設定型別可以使用簡寫 `title: String` 若 title 是一個**物件**,可以針對裡面的屬性設定型別 ```javascript title: { chinese: {type: String}, english: {type: String} } ``` 若 title 為一個**陣列**,也可指定陣列中資料型別 ```javascript title: [{type: String}] // 若只有設定型別可以使用簡寫 [String] ``` #### required 若此資料需為必填項目,可以設定 required,並且可客製化錯誤訊息,格式如下: ```javascript title: { type: String, required: [true, 'title 為必填'] } ``` #### default 若有資料未填寫,也可以設定此資料的預設值 ```javascript title: { type: String, default: '未命名的文章' } ``` 通常 required 與 default 不會同時使用 #### select 若有資料欄位希望可以被保護,不顯示出來,可以加入 select 設定 例如:若有涉及使用者相關個資 password、email ...等等,都會建議加入 `select: false` 不將資料回傳給前端 ```javascript password: { type: String, select: false } ``` #### enum 若此資料設定型別為 `String` 或 `Number`,可以設定 `enum` 指定需為哪些值 以字串為例,若 author 需為 Amy、Bob 或 Cody 其中之一,可以設定為以下: ```javascript author: { type: String, enum: ['Amy','Bob','Cody'] } ``` ### 參考資源 [Mongoose v6.2.10: Schemas](https://mongoosejs.com/docs/guide.html) [Mongoose v6.2.10: SchemaTypes](https://mongoosejs.com/docs/schematypes.html) [Mongoose NPM 教學(可看到 Mongoose 新增 Model 流程)](https://courses.hexschool.com/courses/1670869/lectures/39252168)(章節影片) ### 題目(將答案寫在 CodePen 並提交至回報區) 請參考以下需求,設計手搖飲的 Schema ``` - 產品名稱(product): 需為字串,必填,若未填寫,錯誤訊息為「產品名稱未填寫」 - 價錢(price): 需為數字, 必填,若未填寫,錯誤訊息為「價錢未填寫」 - 冰塊(ice): 需為字串, 若未填寫預設為 '正常冰' - 甜度(sugar):需為字串,若未填寫預設為 '全糖' - 配料(toppings):為陣列,內容需為字串 ``` ```javascript= const drinkSchema = new mongoose.Schema({ /* 請在此填入答案 */ }); ``` 回報流程 --- 請同學依照下圖教學觀看解答、回報答案: ![](https://i.imgur.com/QtL8zEW.png) 回報格式:請在「回報區」貼上連結回報答案 (為了統計人數,請同學依序加上「報數」) <!-- 解答 ```javascript const drinkSchema = new mongoose.Schema({ product: { type: String, required: [true, '產品名稱未填寫'] }, price: { type: Number, required: [true, '價錢未填寫'] }, ice: { type: String, default: "正常冰" }, sugar: { type: String, default: "全糖" }, toppings: [{type: String}] }); ``` --> 回報區 --- | 報數 | 組別 / 名字 | codepen / hackMD / 其他回饋 | | ---- | ----------------------- | ------------------------------------------------------------------------------- | | 1 | 第 6 組 / Wendy | [Codepen](https://codepen.io/wendy03/pen/MWrPPpR) | | 2 | 第 2 組 / Vic | [Codepen](https://codepen.io/hsuan333/pen/QWaZZvZ) | | 3 | 第 4 組 / 小宥 | [Codepen](https://codepen.io/yosontw21/pen/VwyEbyW) | | 4 | 第 3 組 / HedgehogKU | [Codepen](https://codepen.io/hedgehogkucc/pen/MWrPPZW) | | 5 | 第 2 組 / Jin | [Codepen](https://codepen.io/Jin-L/pen/bGammyz) | | 6 | 第 2 組 / Genos | [Codepen](https://codepen.io/pb220416/pen/RwxeemM) | | 7 | 第 3 組 / Hobby | [Codepen](https://codepen.io/hobbyling/pen/ZEvqqZG) | | 8 | 第 1 組 / Phoebe | [Codepen](https://codepen.io/phoebe26/pen/xxpyQQj) | | 9 | 第 8 組 / Hank | [Codepen](https://codepen.io/hank-hsiao/pen/jOYeQJM) | | 10 | 第 9 組 / 黃士桓 | [Codepen](https://codepen.io/shr-huan-huang/pen/yLpRGJz) | | 11 | 第 6 組 / Ruta | [Codepen](https://codepen.io/rue503/pen/bGamOgB) | | 12 | Naiky | [Codepen @Naiky](https://codepen.io/naiky/pen/dyJgwey) | | 13 | 第 14 組|East | [Codepen](https://codepen.io/EastM/pen/LYegMrw?editors=0010) | | 14 | 第 4 組 / sihle | [Codepen](https://codepen.io/bugbug777/pen/KKZGJVG) | | 15 | 第 2 組 / joe.chang | [Codepen](https://codepen.io/t7552175/pen/KKZGJdx) | | 16 | 0 / Aya | [Codepen](https://codepen.io/NoNameNote/pen/Exodrvj) | | 17 | 第 4 組 / 苡安 | [Codepen](https://codepen.io/yi-an-yang/pen/MWrPRYE) | | 18 | 第 5 組 / HazelWu | [Codepen HazelWu](https://codepen.io/wualnz/pen/ZEvqZZm) | | 19 | 第 9 組 / konstante | [Hackmd](https://hackmd.io/66Yel55aSSKSaaNG3d-7-Q) | | 20 | 第 1 組 / Claire | [Codepen](https://codepen.io/claire-chang-the-bashful/pen/OJzaJOY?editors=1010) | | 21 | 第13組/KFC | [Codepen](https://codepen.io/kuan-fu-chen/pen/RwxqNNz?editors=1010) | | 22 | 第11組 / Han Lai | [Codepen](https://codepen.io/x94han/pen/JjMeoMz) | | 23 | 第2組 / wendy.li | [Codepen](https://codepen.io/rockayumitw/pen/bGaQvbQ?editors=0011) | | 24 | 第11組 / mandy | [Codepen](https://codepen.io/manyu1225/pen/bGYqwyM?editors=1011) | | 25 | 第3組 / larry | [Codepen](https://codepen.io/larrylinr5/pen/NWXeNoB?editors=1010) | | 26 | 第4組 / mischa | [codepen_day6_mischa](https://codepen.io/Dorothy_0528/pen/zYpyqVx?editors=0010) | | 27 | 第7組 / 程翔 | [Codepen](https://codepen.io/xxxiang/pen/JjMwKoR?editors=1010) | | 28 | 第5組 / sihyun | [Codepen](https://codepen.io/siyuch/pen/NWXerRo) | | 29 | 第3組 / hiYifang | [Codepen](https://codepen.io/hiYifang/pen/qBpLqWG) | | 30 | 第3組 / Justin | [Codepen](https://codepen.io/justin3737/pen/GRyPNxm?editors=0010) | | 31 | 第16組 / 皓皓 | [Codepen](https://codepen.io/cutecat8110/pen/GRyPWNb) | | 32 | 第14組 / 涼二 | [Codepen](https://codepen.io/Araytsai/pen/GRyPvja) | | 33 | 第14組 / yolala | [Codepen](https://codepen.io/chiakilalala/pen/bGaOoGq) | | 34 | 第2組 / Rikkubook | [Codepen](https://codepen.io/rikkubook/pen/rNpoYaN?editors=0010) | | 35 | 第14組 /uniza | [Codepen](https://codepen.io/unizalin/pen/jOYXYqq) | | 36 | 第 9 組 / Reynold | [Codepen](https://codepen.io/chasel1020/pen/KKZbZEv) | | 37 | 第 1 組 / Ed Huang | [Codepen](https://codepen.io/yide1986/pen/gOoZveW?editors=0011) | | 38 | 第 6 組 / 馨云 | [Codepen](https://codepen.io/bigheadyun/pen/OJzrQrO) | | 39 | 第 3 組 / Adam Hsu | [Codepen](https://codepen.io/Adam-Hsu/pen/RwxEvmV) | | 40 | 第 5 組 / Nap | [Codepen](https://codepen.io/o0o0o1o0/pen/dyJwrgJ) | 41 | 第 8 組 / JackLee | [Codepen](https://codepen.io/yxzzktmb/pen/popqEKr?editors=0010) | 42 | 第 12 組 / Jimmy | [Codepen](https://codepen.io/JimmyChangWenChi/pen/QWazRvO?editors=0010) | 43 | 第 2 組 / peter | [Codepen](https://codepen.io/JIAN-RONG/pen/VwyqOQv?editors=0012) | 44 | 第 8 組 / Jordan Tseng | [Codepen](https://codepen.io/jordan-ttc-design/pen/QWazXRL?editors=1010) | 45 | 第 15 組 / Tofu | [Codepen](https://codepen.io/Tofutseng/pen/KKZJpjG) | 46 | 第 1 組 / Jerry Huang | [Codepen](https://codepen.io/sun31483/pen/yLpZJzd?editors=0010) | 47 | 第 8 組 / Albert | [HackMD](https://hackmd.io/@Albertnotes/r1zV8ga4c) | 48 | 第 3 組 / 小葉 | [HackMD](https://hackmd.io/@FyKv37KcRSWqAO_e336w8g/HJNHB5UV9) | | 49 | 第 15 組 / CHIU | [Codepen](https://codepen.io/awdx8326/pen/popYJWd?editors=1112) | | 49 | 第 1 組 / snow | [Codepen](https://hackmd.io/@snowsuika/BJ7_e60Vq) | | 50 | nick6303 | [Codepen](https://codepen.io/nick6303/pen/ZEvZpqO) | | 50 | 第 7 組 / jason06286 | [HackMD](https://hackmd.io/DKqtftCkShGg9a-jRPao_w) | | 51 | 第 10 組 / 橘子 | [HackMD](https://codepen.io/joanne-wei/pen/bGaypJe?editors=0010) | | 52 | 第 5 組 / AmberCYT | [HackMD](https://hackmd.io/@amber871023/HkzRYKzB5/%2Fxiet4CBlRiOwqyU0vAWfyw) | | 53 | 第 10 組 / Otis | [HackMD](https://codepen.io/humming74/pen/zYpVpax?editors=0010) | | 54 | 第 1 組 / Emily Hsi | [HackMD](https://hackmd.io/@EmilyHsi/rJ-bVhvrc) | | 55 | 第15組 / Castiel | [Codepen](https://codepen.io/everydaycodedog/pen/xxpvyyd?editors=1010) | | 56 | 第7組 / TracyChien | [HackMD](https://hackmd.io/@TracyChien/ryVMuQYBq) | | 56 | 第10組 / Butters | [HackMD](https://hackmd.io/Lh6Ko7lcRPaGBomdYptg1w) | | 57 | 第 12 組 / Sylvia-H | [HackMD](https://codepen.io/Cosmosheart/pen/LYQGxEw?editors=0010) | | 58 | 第 14 組 / 皮皮 | [HackMD](https://codepen.io/super-shrimp/pen/zYReeNx?editors=0010) |

    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