Date post: | 16-Apr-2017 |
Category: |
Technology |
Upload: | komal-bajaj |
View: | 75 times |
Download: | 1 times |
ODMBy: Komal Bajaj
www.komalbajaj.com
MongoDB With Mongoose
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
FLEXIBLE SCHEMA⦿What is meant by flexible schema?⦿ Do we need a flexible schema for
development?⦿ Do we need a flexible schema for
production?
Basic Schema
District
School1 School2
Users Courses UsersCourses
GradeHistory GradeHistory
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?⦿ …
How does Flexible Schema help?
⦿ District {List<School> schools
}⦿ School {List<User> usersList<Course> courses
}⦿ User{List<Grade> gradeHistory
}
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
}
Schema Option 3⦿ District {
List<School> schoolsList<SemiCourseInfo> semiCourseInfoList<SemiUserInfo> semiUserInfo
}⦿ User {UserInfo userInfoSemiDistrictInfo districtSemiCourseInfo course
}⦿ Course {CourseInfo courseInfo
}
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
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
Different ODM for MongoDB
⦿Mongoose -> Node.js⦿Mandango -> PHP⦿ Doctrine -> Any Language [Mapping in
XML / YAML]⦿MongoLink -> Java⦿MongoODM -> Ruby⦿…
Mongoose ODM
⦿Modeling application data ⦿ Built-in type casting⦿ Validation⦿ Query building⦿ Business logic hooks and more
MongoDB & ODM docs
School Schema
⦿ var schoolSchema = new Schema({ name: String, id: ObjectId, creationDate: Date, active: Boolean, noOfLicenses: Number
});
Model application data
⦿ var districtSchema = new Schema({name: String, id: ObjectId, school: [schoolSchema]admin: {
name: String, id: Number }
});
Permitted SchemaTypes
⦿ String⦿ Number⦿ Date⦿ Buffer⦿ Boolean⦿Mixed⦿ ObjectId⦿ Array
Mongoose Document
⦿ Compile ModelVar School =
mongoose.model(‘School’, schoolSchema)
⦿ Construct DocumentVar school1 = new School()
Operations on Document
⦿ Basic CRUD:-●Save()●Find()●Update()●Remove()
⦿ Advanced Operations as available on MongoDB docs
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);
})
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);
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/ }})
Custom Validator
⦿ // make sure every value is equal to "something"
⦿ function validator (val) { return val == 'something'; }
⦿ new Schema({ name: { type: String, validate: validator }});
A lot more to explore in Mongoose & Mongo DB