Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
NTU CCSP 2012Fall - Node.js #3 - Mongoose
Search
Steven Su
October 31, 2012
Programming
740
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
330
NTU CCSP 2012Fall - Facebook
xpsteven
1
220
NTU CCSP 2012Fall - Node.js #2.1 - Simple File Host Service
xpsteven
2
460
NTU CCSP 2012Fall - Node.js #2 - express.js
xpsteven
3
580
Y! Open Hack 2012 Taiwan
xpsteven
0
210
NTU CCSP 2012Fall - Node.js #2 - Advanced Javascript and Node.js
xpsteven
1
560
Other Decks in Programming
See All in Programming
Android CLI
fornewid
0
210
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
220
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
120
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
160
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
1
530
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
740
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
550
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
450
継続モナドとリアクティブプログラミング
yukikurage
3
680
React本体のコードリーディング
high_g_engineer
1
130
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
800
yield再入門 #phpcon
o0h
PRO
0
920
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
450
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
230
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
Code Reviewing Like a Champion
maltzj
528
40k
New Earth Scene 8
popppiees
3
2.5k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.7k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
200
Amusing Abliteration
ianozsvald
1
240
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
45k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.2k
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