6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/webday4_mongo...you have a lot of...

37
MongoDB 6.148 Eric Chen

Upload: others

Post on 11-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

MongoDB6.148

Eric Chen

Page 2: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Installing MongoDBhttps://docs.mongodb.org/v3.0/installation/

Page 3: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

What is MongoDB?

Most popular NoSQL database program.

Page 4: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Why use MongoDB?

Page 5: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Why use MongoDB?

● Very efficient when you need to do a lot of writes

Page 6: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Why use MongoDB?

● Very efficient when you need to do a lot of writes● You have a lot of data (partitioning and sharding is easy)

Page 7: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Why use MongoDB?

● Very efficient when you need to do a lot of writes● You have a lot of data (partitioning and sharding is easy)● Schema is very prone to change

Page 8: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Structure

● MongoDB Instance

Page 9: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Structure

● MongoDB Instance○ Database

Page 10: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Structure

● MongoDB Instance○ Database

■ Collections

Page 11: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Structure

● MongoDB Instance○ Database

■ Collections● Documents

[{“name”: “David”, “age”: 12},{“name”: “Hunter”, “age”: 14},{“name”: “Aashish”, “age”: 10}

]

Page 12: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

MongoDB Shell

● Interactive interface to MongoDB● Can read and write to your database● Uses ‘test’ database by default

Page 13: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Start a MongoDB Instance

● A MongoDB instance exists within a directory● In your terminal:

○ mkdir data (only directory doesn’t exist)○ mongod --dbpath path/to/data

● OR○ mongod (will use the default directory)

Page 14: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Mongo Shell

● To start the Mongo shell (in your terminal):○ mongo

● (If you are a Windows user, call mongo from within the bin folder where MongoDB is installed)

Page 15: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Mongo Shell Commands

● show collections - list all the collections in the database● db.<collection name>.find() - list all the documents in

the collection● db.<collection name>.find(obj) - list all the documents

in the collection which match the key value pairs in obj

Page 16: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Mongo Shell Commands (cont’d)

● db.<collection name>.insert(obj) - inserts obj into the collection○ If the collection doesn’t exist, also creates the collection

● db.<collection name>.remove(obj) - removes all documents in the collection matching the fields in obj

● db.<collection name>.drop() - deletes the collection from the database

Page 17: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Demo!

Page 18: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Mongoose

Page 19: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

What is Mongoose?

● A NodeJS library ● Allows you to integrate MongoDB with your app

Page 20: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Installing Mongoose for Your App

npm install --save mongoose

Installs in node_modules/ and saves to package.json!

Page 21: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Setting up the DB with Mongoose(app.js)

Page 22: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Schemas

● Structure for your documents● Each collection should have a schema

Page 23: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Schemas

● Structure for your documents● Each collection should have a schema

[{“name”: “David”, “age”: 12},{“name”: “Hunter”, “age”: 14},{“name”: “Aashish”, “age”: 10}

]

Page 24: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Schemas

● Structure for your documents● Each collection should have a schema

Schema: {“name”: string, “age”: number}

[{“name”: “David”, “age”: 12},{“name”: “Hunter”, “age”: 14},{“name”: “Aashish”, “age”: 10}

]

Page 25: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Schemas(schemas/student.js)

Page 26: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Schemas

Specifies the collection: “students”

(schemas/student.js)

Page 27: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Schema Constraints(schemas/student.js)

Page 28: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Creating a Document

Page 29: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Finding documents

Page 30: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Finding documents (pt 2)

Page 31: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Finding documents (pt 3)

Page 32: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Editing documentsYou can first retrieve the document, and then update.

Page 33: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Editing documents (pt 2)Or you can update directly.

Page 34: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Removing documents

Page 35: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Mongoose Documentationhttps://mongoosejs.com/docs/guide.html

Page 36: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Demo!

Page 37: 6.148.scripts.mit.edu6.148.scripts.mit.edu/2017/pages/lectures/WEBday4_mongo...You have a lot of data (partitioning and sharding is easy) Schema is very prone to change Structure MongoD

Workshop: go.6148.io/mongodb