Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
NTU CCSP 2012Fall - Node.js #3 - Mongoose
Search
Steven Su
October 31, 2012
Programming
760
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
NTU CCSP 2012Fall - Node.js #3 - Mongoose
Data Layer / Mongodb / Mongoose
Steven Su
October 31, 2012
More Decks by Steven Su
See All by Steven Su
JSDC.TW 2013 Node.js佈署心得
xpsteven
14
1.2k
NTU CCSP 2012Fall - Mobile Web入門 回首網頁開發
xpsteven
3
350
NTU CCSP 2012Fall - Facebook
xpsteven
1
220
NTU CCSP 2012Fall - Node.js #2.1 - Simple File Host Service
xpsteven
2
470
NTU CCSP 2012Fall - Node.js #2 - express.js
xpsteven
3
590
Y! Open Hack 2012 Taiwan
xpsteven
0
240
NTU CCSP 2012Fall - Node.js #2 - Advanced Javascript and Node.js
xpsteven
1
560
Other Decks in Programming
See All in Programming
App Intentsのビルドプロセスを支える技術
kntkymt
0
390
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
340
スマート反転とウェブアクセシビリティ
camiha
0
210
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.1k
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
250
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
560
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
220
SREの越境 / SRE Collaboration
y0hgi
2
230
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
180
Heart of Swift Concurrency
koher
0
890
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
550
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
380
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
A better future with KSS
kneath
240
18k
First, design no harm
axbom
PRO
2
1.3k
Odyssey Design
rkendrick25
PRO
2
810
Navigating Team Friction
lara
192
16k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
610
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Ethics towards AI in product and experience design
skipperchong
2
380
30 Presentation Tips
portentint
PRO
1
400
Designing Experiences People Love
moore
143
24k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Transcript
資料層:Mongoose NTU CCSP 2012Fall
目標 • 安裝mongodb • 使用mongo shell • 簡易CRUD
環境 • Windows 7 • Sublime Text 2 • Node.js
0.8.x • Express.js 3.0 • Mongoose.js
MONGODB INSTALL
Step 0: 下載mongodb
Step 0: 下載mongodb
Step 1: 解壓縮移動到C 並命名為mongodb
Step 2: 設定環境
Step 3: 啟動mongod
Step 4: 進入mongo shell
MONGO SHELL
MongoDB • 是一種NoSQL • 不需要資料綱要(Schema-Free) • 允許巢狀資料 • 沒有JOIN 沒有TRANS
• 簡單說就是一個存放JSON的資料庫 所以才 會跟Node.js很合
MongoDB練習 • http://try.mongodb.org/
FIRST LOOK OF MONGOOSE
Step -1: 請先安裝mongoose npm install mongoose -g
Step 0: 產生綱要 1. 產生schema 2. 產生model instance.save(fn(err){}) 儲存 Model.find()
搜尋
Step 0: 產生綱要
Step 1: 更新與刪除 • Model.remove() 刪除所有文件 • instance.remove() 刪除一個文件
Step 2: 綱要進階 • 常用type String, Number, Date, ObjectId •
Array用法 • Embed Docs => Array嵌入物件
CRUD BY EXPRESS+MONGOOSE
Step 0: 建立express專案
Step 1: 修改package.json
Step 2: 安裝套件
Step3: 編輯models/User.js
Step 3: 編輯 app.js • mongoose.connect和 mongoose.createConnection是不 同的 • 前者是模組等級
後者是獨立連線 • 沒有指明連線的model會嘗試使用 並等待模組等級連線建立
Step 4: Create • 編輯route/user.js • 注意callback的例外
Step 4: Create • 編輯view/index.ejs
Step 4.5: 來試試看吧
Step 5: Read • Read包含兩種 • 讀取集合 • 讀取單一物件
Step 6: Update
Step 6: Update • Callback hell很痛苦 • 銀幕在寬都不夠
Step 8: step.js解決cb hell • 解決callback hell • Err可以很帥氣的throw
遺珠之憾 • Population http://mongoosejs.com/docs/populate.html • Middleware http://mongoosejs.com/docs/middleware.html • Advanced Query
http://mongoosejs.com/docs/queries.html • SQL to Mongo mapping http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Map ping+Chart • Step.js https://github.com/creationix/step