Slide 1

Slide 1 text

Cloud Computing-Based Service Design and Programming Andy Kuo @andikan 1

Slide 2

Slide 2 text

DB = DataBase (n.) 資料庫 A database is an organized collection of data. 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

Hello DB! 4

Slide 5

Slide 5 text

SQL Relational Database 5

Slide 6

Slide 6 text

NoSQL http://nosql-database.org/ Not Only SQL 6

Slide 7

Slide 7 text

Volume • Velocity • Variety 7

Slide 8

Slide 8 text

Key / Value Column Graph Document 8

Slide 9

Slide 9 text

MongoDB scalable, high-performance, open source NoSQL database • document store • full index support • replication • querying • map / reduce 9

Slide 10

Slide 10 text

MongoDB implemented in C++ drivers are available in many language http://docs.mongodb.org/manual/applications/drivers/ MongoDB Drivers and Client Libraries 10

Slide 11

Slide 11 text

BSON Binary JSON BSON = JSON + a few additions • ObjectID • Timestamp • Date 11

Slide 12

Slide 12 text

MongoDB Terminology Relational DBs MongoDB database database table collection row (tuple) document (BSON) column field index index primary key _id foreign key 12

Slide 13

Slide 13 text

Collection Document 13

Slide 14

Slide 14 text

Download : http://www.mongodb.org/downloads Installation : http://docs.mongodb.org/manual/installation/ Getting Started 14

Slide 15

Slide 15 text

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ https://speakerdeck.com/xpsteven/ntu-ccsp-2012fall-node-dot-js-number-3-mongoose Install MongoDB on Windows 15

Slide 16

Slide 16 text

mongod The primary daemon process for the MongoDB system It handles data requests, manages data format, and performs background management operations 16

Slide 17

Slide 17 text

Default dbpath /data/db Can use your custom directory 17

Slide 18

Slide 18 text

Listen on localhost : 27017 18

Slide 19

Slide 19 text

admin web console localhost : 28017 19

Slide 20

Slide 20 text

Connect to mongod By default, mongo looking for a database server listening on port 27017 on the localhost 20

Slide 21

Slide 21 text

Welcome to the MongoDB shell Session will use the test database by default Access with the Javascript Language 21

Slide 22

Slide 22 text

http://try.mongodb.org/ 22

Slide 23

Slide 23 text

Name of the current database Display the list of databases Switch to a new database named cooldb 23

Slide 24

Slide 24 text

Insert a document into a collection 24

Slide 25

Slide 25 text

Traverse documents in a collection 25

Slide 26

Slide 26 text

Methods to create documents 26

Slide 27

Slide 27 text

Methods to update documents 27

Slide 28

Slide 28 text

Methods to delete documents 28

Slide 29

Slide 29 text

Methods to query documents 29

Slide 30

Slide 30 text

Methods to query documents Query Operators Comparison: $gt, $gte, $in, $lt, $lte, $ne, $nin Logical: $or, $and, $not, $nor Array: $all, $size, $eleMatch More: http://docs.mongodb.org/manual/reference/operator/query/ 30

Slide 31

Slide 31 text

Create / Read / Update / Delete CRUD http://docs.mongodb.org/manual/ More about MongoDB 31

Slide 32

Slide 32 text

http://docs.mongodb.org/ecosystem/tools/administration-interfaces/ Admin UIs MongoHub RoboMongo Mac OS X Cross-platform 32

Slide 33

Slide 33 text

ORM Object-relational Mapper ( Relational DB ) ODM Object-document Mapper ( MongoDB ) validations, associations, and other high-level data modeling functions 33

Slide 34

Slide 34 text

Mongoose Officially supported ODM for Node.js Advanced schema-based features Object life-cycle management Mongoose http://mongoosejs.com/ 34

Slide 35

Slide 35 text

Install Mongoose Or install in your express project directory 35

Slide 36

Slide 36 text

Create Schema - models/user.js Schema Types - String - Number - Date - Buffer - Boolean - Mixed - ObjectId - Array 36

Slide 37

Slide 37 text

Connect DB - db.js 37

Slide 38

Slide 38 text

Require db.js - app.js 38

Slide 39

Slide 39 text

Routes - app.js 39

Slide 40

Slide 40 text

Require model - routes/user.js 40

Slide 41

Slide 41 text

Postman Chrome extension A powerful HTTP Client to test web services http://www.getpostman.com/ 41

Slide 42

Slide 42 text

Get all users - routes/user.js GET /users 42

Slide 43

Slide 43 text

Create a user - routes/user.js POST /users 43

Slide 44

Slide 44 text

Show a user - routes/user.js GET /users/:id 44

Slide 45

Slide 45 text

Update a user - routes/user.js POST /users/:id 45

Slide 46

Slide 46 text

Delete a user - routes/user.js Delete /users/:id 46

Slide 47

Slide 47 text

https://github.com/NTU-CCSP/mongoose-tutorial Sample Code 47

Slide 48

Slide 48 text

The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. __v http://mongoosejs.com/docs/guide.html#versionKey 48

Slide 49

Slide 49 text

Query with Mongoose 49

Slide 50

Slide 50 text

Query with Mongoose 50

Slide 51

Slide 51 text

Population Pseudo-joins Reference to documents in other collections 51

Slide 52

Slide 52 text

Population - Schema 52

Slide 53

Slide 53 text

Population - Saving refs 53

Slide 54

Slide 54 text

Population - Query 54

Slide 55

Slide 55 text

Population - Refs to children 55

Slide 56

Slide 56 text

Population - Query 56

Slide 57

Slide 57 text

page.fans ? CCSP Facebook Page https://www.facebook.com/ntu.ccsp 57

Slide 58

Slide 58 text

Mongoose Plugins Re-use schema functionality in your own project - Auto increment https://github.com/codetunnel/mongoose-auto-increment - Timestamp https://github.com/drudge/mongoose-timestamp 58

Slide 59

Slide 59 text

Callback Hell - Promises - Async.js https://github.com/caolan/async - Step.js https://github.com/creationix/step http://callbackhell.com/ 59

Slide 60

Slide 60 text

Cloud Computing-Based Service Design and Programming Andy Kuo @andikan Thanks 60