36
Relationships MONGOOSE

Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

RelationshipsMONGOOSE

Page 2: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Using References (Normalization)let author = {

name: "Usman Akram"

}

let course = {

title: 'Web Technologies',

author: 'id'

}

Usman Akram CUI LAHORE 2

Page 3: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Using Embedded Documents (Denormalization)let course1 = {

title: 'Web Technologies',

author: {

name: "Usman Akram"

}

}

Usman Akram CUI LAHORE 3

Page 4: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Trade Off between Query Performance Vs Consistency NORMALIZATION

A change in author would reflect every where

More Consistent but need extra query to get child records

DE NORMALIZATION

If you need to change the author you will have to modify in multiple records

Not Consistent but More Performance

Usman Akram CUI LAHORE 4

Page 5: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Hybrid Approachlet author = {

name: "Usman Akram"

// 50 More properties

}

let course = {

title: 'Web Technologies',

author: {

name: "Usman Akram",

id: 'reference to author'

}

}

// Copy id and some of specific properties. Like facebook top comment should be beside post

Usman Akram CUI LAHORE 5

Page 6: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Mongoose Recap and Code for This sectionhttps://1drv.ms/f/s!AtGKdbMmNBGd0ia66DfXulxFIM6m

const mongoose = require('mongoose');

//npm init –yes

//npm install mongoose

mongoose.connect('mongodb://localhost/playground')

.then(() => console.log('Connected to MongoDB...'))

.catch(err => console.error('Could not connect to MongoDB...', err));

Usman Akram CUI LAHORE 6

Page 7: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Authorconst Author = mongoose.model('Author', newmongoose.Schema({

name: String,

bio: String,

website: String

}));

Usman Akram CUI LAHORE 7

Page 8: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Courseconst Course = mongoose.model('Course', newmongoose.Schema({

name: String,

author: {

type: mongoose.Schema.Types.ObjectId,

ref: "Author"

}

}));

Usman Akram CUI LAHORE 8

Page 9: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Create Authorasync function createAuthor(name, bio, website) {

const author = new Author({

name,

bio,

website

});

const result = await author.save();

console.log(result);

}

Usman Akram CUI LAHORE 9

Page 10: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Create Courseasync function createCourse(name, author) {

const course = new Course({

name,

author

});

const result = await course.save();

console.log(result);

}

Usman Akram CUI LAHORE 10

Page 11: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

List Coursesasync function listCourses() {

const courses = await Course

.find()

.select('name');

console.log(courses);

}

Usman Akram CUI LAHORE 11

Page 12: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Actually Create an AuthorcreateAuthor('Mosh', 'My bio', 'My Website');

createCourse('Web Technologies', '5c3e1c1d8b3a1c1d0c1ac03d')

// whatever id the create course has given to new doc put that in create Course

Usman Akram CUI LAHORE 12

Page 13: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Author Doc{

"_id":"5c3e1e0734ea7010842c96df",

"name":"Web Technologies",

"author":"5c3e1c1d8b3a1c1d0c1ac03d",

"__v":0

}

Usman Akram CUI LAHORE 13

Page 14: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Populationasync function listCourses() {

const courses = await Course

.find()

.populate('author')

.select('name author');

console.log(courses);

}

Usman Akram CUI LAHORE 14

Page 15: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Author populated{ _id: 5c3e1e0734ea7010842c96df,

name: 'Web Technologies',

author:

{ _id: 5c3e1c1d8b3a1c1d0c1ac03d,

name: 'Usman',

bio: 'My bio',

website: 'usmanlive.com',

__v: 0 }

}

Usman Akram CUI LAHORE 15

Page 16: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Population only get author nameasync function listCourses() {

const courses = await Course

.find()

.populate('author', 'name')

.select('name author');

console.log(courses);

}

Usman Akram CUI LAHORE 16

Page 17: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Population only get author name -idasync function listCourses() {

const courses = await Course

.find()

.populate('author', 'name -_id')

.select('name author');

console.log(courses);

}

Usman Akram CUI LAHORE 17

Page 18: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Multiple Populationsasync function listCourses() {

const courses = await Course

.find()

.populate('author', 'name -_id')

.populate('category', 'name')

.select('name author');

console.log(courses);

}

Usman Akram CUI LAHORE 18

Page 19: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

What if the author ID is changedE.g. Course has an author ID which Actually doesn’t exist in author collection

-Will Mongo Complain –No

-What will return –Null

Usman Akram CUI LAHORE 19

Page 20: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Embedding Documents (Course has One Author)//Define a Schema

const authorSchema = new mongoose.Schema({

name: String,

bio: String,

website: String

});

Usman Akram CUI LAHORE 20

Page 21: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Embed Auth Schema in Course Schemaconst Course = mongoose.model(

"Course",

new mongoose.Schema({

name: String,

author: authorSchema

})

);

Usman Akram CUI LAHORE 21

Page 22: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Mongo Data Shape{

"_id":"5c51d826e53bf91d5c6c3560",

"name":"Node Course 1",

"author":

{"_id":"5c51d826e53bf91d5c6c355f","name":“Usman"},

"__v":0

}

Usman Akram CUI LAHORE 22

Page 23: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Updating Embedded Docasync function updateCourse(id) {

const course = await Course.findById(id);

course.author.name = "Hareem";

course.save();

//course.author.save();

}

Usman Akram CUI LAHORE 23

Page 24: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Update Embedded Doc with Single Queryasync function updateCourseSingleQuery(id) {

const course = await Course.update({ _id: id }, {

$set: {

"author.name": "Hareem Fatima"

}

});

course.save();

}

Usman Akram CUI LAHORE 24

Page 25: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Removing Embedded Docasync function removeAuthor(id) {

const course = await Course.update({ _id: id }, {

$unset: {

"author":""

}

});

course.save();

}

Usman Akram CUI LAHORE 25

Page 26: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Embedded Doc is a Doc new mongoose.Schema({

name: String,

author: {

type: authorSchema,

required: true

}

})

Usman Akram CUI LAHORE 26

Page 27: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

An Array of Sub Docs (Many Authors)new mongoose.Schema({

name: String,

authors: [authorSchema]

})

Usman Akram CUI LAHORE 27

Page 28: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Create Course With Many AuthorscreateCourse("Web Tech", [

new Author({ name: "Usman" }),

new Author({ name: "Hareem" })

]);

//course.authors.push(author) to add

//No Change in Create Course Function

Usman Akram CUI LAHORE 28

Page 29: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Add Authorasync function addAuthor(courseID,author){

const course = awaitCourse.findById (courseID);

course.authors.push(author);

course.save();

}

Usman Akram CUI LAHORE 29

Page 30: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Remove Authorasync function removeAuthor(courseID,authorID){

const course = await Course.findById(courseID);

const author = course.authors.id(authorID);

author.remove();

course.save();

}

Usman Akram CUI LAHORE 30

Page 31: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Project Movies API Exercisehttps://1drv.ms/f/s!AtGKdbMmNBGd0ja8VzM2bdZ43qtK

Usman Akram CUI LAHORE 31

Page 32: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Movies Rental API Exercisehttps://1drv.ms/f/s!AtGKdbMmNBGd0iWySSLjdDZA565v

Create a New Rental

// POST /api/rentals

Get the List of Rentals

//GET /api/rentals

Usman Akram CUI LAHORE 32

Page 33: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Transactions in Mongo DB ? Two Phase Commit

npm install fawn

Usman Akram CUI LAHORE 33

Page 34: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Object ID (24 char or 12 Bytes)5c3e1e0734ea7010842c96df

16 Million docs per millisecond per machine per process

4-bytes: timestamp

3-bytes: machine identifier

2-bytes: process identifier

3-bytes: counter

Usman Akram CUI LAHORE 34

Page 35: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

Performance ?Mongo DB Driver (mongoose) can generate the object ID

No need to consult Mongo DB for id

const id = mongoose.Types.ObjectId()

console.log(id.getTimestamp())

//mongoose.Types.ObjectId.isValid(456); ?

Usman Akram CUI LAHORE 35

Page 36: Relationships - UsmanLive€¦ · 06/02/2019  · Using Embedded Documents (Denormalization) let course1 = {title: 'Web Technologies', author: {name: "Usman Akram" }} Usman Akram

ReCAPhttps://1drv.ms/b/s!AtGKdbMmNBGd0wuTPreLe8KsrlNR

Usman Akram CUI LAHORE 36