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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Steven Su
October 31, 2012
Programming
730
2
Share
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
320
NTU CCSP 2012Fall - Facebook
xpsteven
1
210
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
200
NTU CCSP 2012Fall - Node.js #2 - Advanced Javascript and Node.js
xpsteven
1
550
Other Decks in Programming
See All in Programming
初めてのRubyKaigiはこう見えた
jellyfish700
0
300
分析エージェント精度向上における データアナリストの役割
oura_shoya
0
130
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
1
450
サーバーレスで作る、動画データ管理基盤
oyasumipants
0
310
横断組織出身のQAEがインプロセスQAEでつまずいたこと・活かせたこと
ty89
0
450
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
1
390
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
1.3k
Sans tests, vos agents ne sont pas fiables
nabondance
0
170
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
1.6k
要はバランスからの卒業 #yumemi_grow
kajitack
0
200
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
11
2.9k
RailsTokyo 2026#4: AI様があれば、 Hotwireの弱点は消えるか?
naofumi
5
1k
Featured
See All Featured
Paper Plane
katiecoart
PRO
1
50k
Believing is Seeing
oripsolob
1
130
4 Signs Your Business is Dying
shpigford
187
22k
How to Think Like a Performance Engineer
csswizardry
28
2.6k
The Limits of Empathy - UXLibs8
cassininazir
1
340
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
Git: the NoSQL Database
bkeepers
PRO
432
67k
How to build a perfect <img>
jonoalderson
1
5.5k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
210
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Ruling the World: When Life Gets Gamed
codingconduct
0
240
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
590
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