11 schema design & crud

15

Click here to load reader

Upload: ahmed-elbassel

Post on 21-Apr-2017

14 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: 11 schema design & crud

Schema Design & CRUDAhmed Elbassel

Email: [email protected]

Skype: ahmed_elbassel

Page 2: 11 schema design & crud

Schema design & CRUD- MongoDB Vs MySql- Create database and collection- MongoDB client- CRUD operations- Schema Design- Denormalizing for schema design

Page 3: 11 schema design & crud

MongoDB Vs MySql

MySql MongoDB

Table Collection

Row Document

Column Field

Joins Embedded documents, linking

Page 4: 11 schema design & crud

Create Database- Connect to MongoDB, open terminal and run:

- > mongo- Then you’re using the mongo console, and you’re able to run mongo queries- To show all databases: > show dbs- To connect or create a database: > use myDB- To show collections: > show collections- To create a collection: > db.createCollection(‘collection name’)

Page 5: 11 schema design & crud

- RoboMongo- MongoBooster- MongoChef

MongoDB client

Page 6: 11 schema design & crud

CRUD Operations - Create

- Create a document:- db.users.insertOne({name:'ahmed',lname:'bassel'});- To insert many: db.users.insert([ {..........},{ ..........}, {...........}]);- If you tried to insert a document with existing _id, it will throw duplicate _id error- Insert using save: > db.users.save({......})

- If the inserted object does not have an id, then a new object will be inserted.- If the object has an id, it will replace the object that match the id in the

database.

Page 7: 11 schema design & crud

CRUD Operations - Read- Reading documents, use find({query}) method:

- > db.users.find()- To make it look pretty: > db.users.find().pretty()- To get users by name: db.users.find{'name':'ahmed'}

- And Operator: - Or operator

- Using And & Or together- Get by name and (email or username)

Page 8: 11 schema design & crud

CRUD Operations - Update- We can use save and update methods.- Save method is the same previous one.- Update:

- Query: The selection criteria for the update.- Update: The modifications to apply.

- Options:- upsert: Optional. If set to true, creates a new document when no document matches the

query criteria. The default value is false.- multi: Optional. If set to true, updates multiple documents that meet the query criteria. If

set to false, updates one document, The default value is false. - writeConcern and collation: advanced, will be discussed in a MongoDB course.

Page 9: 11 schema design & crud

CRUD Operations - Update- The update() method either modifies specific fields in existing documents or

replaces an existing document entirely.- Update Specific Fields: to use update operator such as, $set

- Update operators: https://docs.mongodb.com/manual/reference/operator/update/#id1

- Replace a document entirely:

Page 10: 11 schema design & crud

CRUD Operations - Delete- To delete a document, use this method:

- Query: The selection criteria for the update.- justOnce:To limit the deletion to just one document, set to true. Omit to use the default value

of false.- writeConcern and collation: advanced.

Page 11: 11 schema design & crud

Schema Design - One to One- It’s better to embed it one document into the other.- You should have a logical reason why you make them individually.

Page 12: 11 schema design & crud

Schema Design - One to Many- You should know how much Many:

- One to few(less than 100): then embed the few into the one

- One to Many(Thousands): then add array of foreign keys into the one

Page 13: 11 schema design & crud

Schema Design - One to Many- One to Too many:

- Then you add a foreign key into the many.

Page 14: 11 schema design & crud

Denormalization for Schema design- You can mix between the three previous methods, putting foreign keys in both

one and many.

- Trade off: Reading VS Writing:- If you need to read more than write, then denormalize your data.- In other words store what you query for.

Page 15: 11 schema design & crud

Questions