23
ODM By: Komal Bajaj www.komalbajaj.com MongoDB With Mongoose

Mongoose ODM

Embed Size (px)

Citation preview

Page 1: Mongoose ODM

ODMBy: Komal Bajaj

www.komalbajaj.com

MongoDB With Mongoose

Page 2: Mongoose ODM

Recap MongoDB

⦿ It is No SQL DB⦿ It is OOP instead of relational DB⦿ It is BASE instead of ACID transactions⦿ Highly Scalable⦿ High Performance / No Joins⦿ Geospatial, Aggregation Frameworks

and FLEXIBLE SCHEMA

Page 3: Mongoose ODM

FLEXIBLE SCHEMA⦿What is meant by flexible schema?⦿ Do we need a flexible schema for

development?⦿ Do we need a flexible schema for

production?

Page 4: Mongoose ODM

Basic Schema

District

School1 School2

Users Courses UsersCourses

GradeHistory GradeHistory

Page 5: Mongoose ODM

Questions during design?⦿ What kind of relationship is between District

and Schools? One to many / One to One?⦿ Is there a relationship between Courses in the

School and district?⦿ Is there a relationship between Courses in the

School 1 and School 2?⦿ Should there be relationship between User in

School and District?⦿ Should there be a relationship between User in

School 1 and School 2?⦿ …

Page 6: Mongoose ODM

How does Flexible Schema help?

⦿ District {List<School> schools

}⦿ School {List<User> usersList<Course> courses

}⦿ User{List<Grade> gradeHistory

}

Page 7: Mongoose ODM

Schema Option 2⦿ District {

List<School> schoolsList<SharedCourseInfo> sharedCourseInfoList<SharedUserInfo> sharedUserInfo

}⦿ School {List<User> usersList<Course> coursesList<OtherSchoolUserInfo> otherSchoolUsers

}⦿ User{List<Grade> gradeHistoryList<School> pastSchoolsAttended

}

Page 8: Mongoose ODM

Schema Option 3⦿ District {

List<School> schoolsList<SemiCourseInfo> semiCourseInfoList<SemiUserInfo> semiUserInfo

}⦿ User {UserInfo userInfoSemiDistrictInfo districtSemiCourseInfo course

}⦿ Course {CourseInfo courseInfo

}

Page 9: Mongoose ODM

Is Schema really flexible?⦿ Flexible schema great for development

cycles⦿ Faster Development⦿ Try various schema variations to see which

one fits model perfectly⦿ But once it goes on Production you need

schema ⦿ You have to be sure the data you are

querying on is present in DB / validation / verification

Page 10: Mongoose ODM

ODM⦿ Object Document Mapper⦿ ODM == ORM⦿ Maps Structure of DB Objects to application

Objects⦿ It provides an abstraction for the basic

CRUD and other operations on documents⦿ Provides connection pooling mechanism⦿ This definitely reduces performance due to

abstraction just like ORM

Page 11: Mongoose ODM

Different ODM for MongoDB

⦿Mongoose -> Node.js⦿Mandango -> PHP⦿ Doctrine -> Any Language [Mapping in

XML / YAML]⦿MongoLink -> Java⦿MongoODM -> Ruby⦿…

Page 12: Mongoose ODM

Mongoose ODM

⦿Modeling application data ⦿ Built-in type casting⦿ Validation⦿ Query building⦿ Business logic hooks and more

Page 13: Mongoose ODM

MongoDB & ODM docs

Page 14: Mongoose ODM

School Schema

⦿ var schoolSchema = new Schema({ name: String, id: ObjectId, creationDate: Date, active: Boolean, noOfLicenses: Number

});

Page 15: Mongoose ODM

Model application data

⦿ var districtSchema = new Schema({name: String, id: ObjectId, school: [schoolSchema]admin: {

name: String, id: Number }

});

Page 16: Mongoose ODM

Permitted SchemaTypes

⦿ String⦿ Number⦿ Date⦿ Buffer⦿ Boolean⦿Mixed⦿ ObjectId⦿ Array

Page 17: Mongoose ODM

Mongoose Document

⦿ Compile ModelVar School =

mongoose.model(‘School’, schoolSchema)

⦿ Construct DocumentVar school1 = new School()

Page 18: Mongoose ODM

Operations on Document

⦿ Basic CRUD:-●Save()●Find()●Update()●Remove()

⦿ Advanced Operations as available on MongoDB docs

Page 19: Mongoose ODM

Additional feature in these operations⦿ CallBacks⦿ School.create(schoolObject,

function (err, schoolObject) { if (err) return handleError(err); // saved schoolObject

})⦿ schoolObject.save(function(err, schoolObject) { if (err) return console.error(err); console.dir(schoolObject);

})

Page 20: Mongoose ODM

QueryBuilder

⦿ School.find({ name: /school$/ })⦿ .where(‘state').equals(‘AZ') ⦿ .where(‘licenses').gt(17).lt(100) ⦿ .where(‘district').in([‘dist1', ' dist2'])⦿ .limit(10) ⦿ .sort(‘name') ⦿ .select(‘id name state') ⦿ .exec(callback);

Page 21: Mongoose ODM

Validations⦿ All Schema types have required validator

var s = new Schema({ name: { type: String, required: true })⦿ Number have min and max validators. var s = new Schema({ n: { type: Number, min: 10, max: 60 })⦿ Strings have enum and match validators var s = new Schema({ name: { type: String, match: /^a/ }})

Page 22: Mongoose ODM

Custom Validator

⦿ // make sure every value is equal to "something"

⦿ function validator (val) { return val == 'something'; }

⦿ new Schema({ name: { type: String, validate: validator }});

Page 23: Mongoose ODM

A lot more to explore in Mongoose & Mongo DB