--- tags: 110-1 --- # 12/17 部課 ## Todo - [ ] Database - [ ] MongoDB ## Database 大部分的人提到「資料庫」的時候,通常指的其實是資料庫管理系統(database management system,簡稱 DBMS):一套讓我們能方便使用資料庫的軟體,作為使用者(或是使用資料庫的應用程式)與資料庫之間溝通的平台。 ### 關聯式資料庫(Relational Database Menagement System, RDBMS) - 以一個或是多個資料表 (table) 的方式存放 - 資料之間有明確的關聯 - 關聯式資料庫以 SQL 語言操作  ### NoSQL 非關聯式資料庫 - Not Only SQL - 分散式  ### 與資料庫溝通:ORM 若需要使用資料庫的功能,比如查詢、新增、修改或刪除資料等,就必須使用資料庫管理系統 (DBMS) 提供的原生查詢語言,其中最著名的是SQL (structured query language),也就是是專門用來操作關聯式資料庫的語言。 但在實務上,當我們用 Express.js 開發應用程式時,會希望能直接用 JavaScript 操作資料庫 ->物件映射 (object mapping)  如果我們要在 Todo 資料庫尋找一筆名為「買蘋果」,但還沒被完成的待辦事項 (todo),透過 ORM 的語法: ```javascript= Todo.find ({ name: '買蘋果', done: { false }}) ``` 若使用 SQL 的指令的話,會是: ```sql= SELECT * FROM Todos WHERE name='買蘋果' AND done=FALSE; ``` ## MongoDB - 一種 NoSQL database - 結合 relational database/key-value database - 儲存方式:json  ### MongoDB Atlas https://lyunotes.blogspot.com/2020/11/day12-mongodb-atlas.html https://www.mongodb.com/atlas/database ### Mongoose: JS ORM for MongoDB - 如何使用:  - 你不會想把你的 userID 跟 password直接放在 code 裡:  - 配合你的 server  - Define Schema create `models/user.js` ```javascript= const mongoose = require('mongoose') const Schema = mongoose.Schema const UserSchema = new Schema({ id: { type: Number, required: [true, 'Id field is required.'] }, name: { type: String, required: [true, 'Name field is required.'] } }) // Creating a table within database with the defined schema const User = mongoose.model('User', UserSchema) // Exporting table for querying and mutating module.exports = User ``` - Define Mutation function 
×
Sign in
Email
Password
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