Top Banner
Mongoose: MongoDB object modelling for Node.js Speaker: Yuriy Bogomolov Solution Engineer at Sitecore Ukraine @YuriyBogomolov | fb.me/yuriy.bogomolov
16

Mongoose: MongoDB object modelling for Node.js

May 11, 2015

Download

Technology

Yuriy Bogomolov

My presentation on first MongoDB User Group in Dnepropetrovsk.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mongoose: MongoDB object modelling for Node.js

Mongoose: MongoDB object modelling for Node.js

Speaker: Yuriy Bogomolov

Solution Engineer at Sitecore Ukraine

@YuriyBogomolov | fb.me/yuriy.bogomolov

Page 2: Mongoose: MongoDB object modelling for Node.js

Agenda1. Short intro: what is Node.js.

2. Main inconveniences of using the native MongoDB driver for Node.js.

3. Mongoose: what is it and what are its killer features:• Validation• Casting• Encapsulation• Middlewares (lifecycle management)• Population• Query building

4. Schema-Driven Design for your applications.

5. Useful links and Q&A

MongoDB Meetup Dnipropetrovsk 203.03.2014 /16

Page 3: Mongoose: MongoDB object modelling for Node.js

1. Intro to Node.js

MongoDB Meetup Dnipropetrovsk 303.03.2014

• Node.js is a server-side JavaScript execution environment.

• Uses asynchronous event-driven model.

• Major accent on non-blocking operations for I/O and DB access.

• Based on Google’s V8 Engine.

• Open-source

• Has own package management system — NPM.

• Fast, highly scalable, robust environment.

/16

Page 4: Mongoose: MongoDB object modelling for Node.js

2. Main inconveniences of native MongoDB driver

• No data validation

• No casting during inserts

• No encapsulation

• No references (joins)

MongoDB Meetup Dnipropetrovsk 403.03.2014 /16

Page 5: Mongoose: MongoDB object modelling for Node.js

3. What is Mongoose

• Object Data Modelling (ODM) for Node.js

• Officially supported by 10gen, Inc.

• Features:• Async and sync validation of models

• Model casting

• Object lifecycle management (middlewares)

• Pseudo-joins!

• Query builder

MongoDB Meetup Dnipropetrovsk 503.03.2014

npm install mongoose

/16

Page 6: Mongoose: MongoDB object modelling for Node.js

3. Mongoose setup

MongoDB Meetup Dnipropetrovsk 603.03.2014

var mongoose = require('mongoose');mongoose.connect('mongodb://localhost:27017/mycollection');

var Schema = mongoose.Schema;

var PostSchema = new Schema({title: { type: String, required: true },body: { type: String, required: true },author: { type: ObjectId, required: true, ref: 'User' },tags: [String],date: { type: Date, default: Date.now }

});

mongoose.model('Post', PostSchema);

Reference

Validation of presence

Default value

Simplified declaration

/16

Page 7: Mongoose: MongoDB object modelling for Node.js

3. Mongoose features: validationSimplest validation example: presencevar UserSchema = new Schema({ name: { type: String, required: true } });

var UserSchema = new Schema({ name: { type: String, required: 'Oh snap! Name is required.' } });

Passing validation function:var UserSchema = new Schema({ name: { type: String, validator: validate } });

var validate = function (value) { /*...*/ }; // synchronous validator

var validateAsync = function (value, respond) { respond(false); }; // async validator

Via .path() API call:UserSchema.path('email').validate(function (email, respond) {

var User = mongoose.model('User');

User.find({ email: email }).exec(function (err, users) {

return respond(!err && users.length === 0);

});

}, 'Such email is already registered');

MongoDB Meetup Dnipropetrovsk 703.03.2014 /16

Page 8: Mongoose: MongoDB object modelling for Node.js

3. Mongoose features: casting• Each property is cast to its

mapped SchemaType in the document, obtained by the query.

• Valid SchemaTypes are:• String• Number• Date• Buffer• Boolean• Mixed• ObjectId• Array

MongoDB Meetup Dnipropetrovsk 803.03.2014

var PostSchema = new Schema({_id: ObjectId, // implicitly existstitle: { type: String, required: true },body: { type: String, required: true },author: { type: ObjectId, required: true, ref: 'User' },tags: [String],date: { type: Date, default: Date.now },is_featured: { type: Boolean, default: false }

});

/16

Page 9: Mongoose: MongoDB object modelling for Node.js

3. Mongoose features: encapsulationModels can have static methods and instance methods:

MongoDB Meetup Dnipropetrovsk 903.03.2014

PostSchema.methods = {addComment: function (user, comment,

callback) {// add comment here

},removeComment: function (user,

comment, callback) {// remove comment here

}};

PostSchema.statics = {dropAllPosts: function (areYouSure) {// drop the posts!

}};

/16

Page 10: Mongoose: MongoDB object modelling for Node.js

3. Mongoose features: lifecycle management

Models have .pre() and .post() middlewares, which can hook custom functions to init, save, validate and remove model methods:

MongoDB Meetup Dnipropetrovsk 1003.03.2014

schema.pre('save', true, function (next, done) {// calling next kicks off the next middleware in parallelnext();doAsync(done);

});

schema.post('save', function (doc) {console.log('%s has been saved', doc._id);

});

/16

Page 11: Mongoose: MongoDB object modelling for Node.js

3. Mongoose features: population

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s):

MongoDB Meetup Dnipropetrovsk 1103.03.2014

PostSchema.statics = {load: function (permalink, callback) {this.findOne({ permalink: permalink })

.populate('author', 'username avatar_url')

.populate('comments', 'author body date')

.exec(callback);}

};

/16

Page 12: Mongoose: MongoDB object modelling for Node.js

3. Mongoose features: query building

Different methods could be stacked one upon the other, but the query itself will be generated and executed only after .exec():

MongoDB Meetup Dnipropetrovsk 1203.03.2014

var options = {perPage: 10,page: 1

};

this.find(criteria).populate('author', 'username').sort({'date_published': -1}).limit(options.perPage).skip(options.perPage * options.page).exec(callback);

/16

Page 13: Mongoose: MongoDB object modelling for Node.js

4. Schema-Driven Design for your applications

• Schemas should match data-access patterns of your application.

• You should pre-join data where it’s possible (and use Mongoose’s .populate() wisely!).

• You have no constraints and transactions — keep that in mind.

• Using Mongoose for designing your application’s Schemas is similar to OOP design of your code.

MongoDB Meetup Dnipropetrovsk 1303.03.2014 /16

Page 14: Mongoose: MongoDB object modelling for Node.js

Useful links

• Node.js GitHub repo: https://github.com/joyent/node

• Mongoose GitHub repo: https://github.com/learnboost/mongoose

• Mongoose API docs and tutorials: http://mongoosejs.com/

• MongoDB native Node.js driver docs: http://mongodb.github.io/node-mongodb-native/

MongoDB Meetup Dnipropetrovsk 1403.03.2014 /16

Page 15: Mongoose: MongoDB object modelling for Node.js

Any questions?

03.03.2014 MongoDB Meetup Dnipropetrovsk 15/16

Page 16: Mongoose: MongoDB object modelling for Node.js

MongoDB Meetup Dnipropetrovsk