SlideShare a Scribd company logo
4
 is passed, the operation will be executed immediately with the results passed to the callback.
 is not passed, an instance of Query is returned, which provides a
special QueryBuilder interface for you.
Let's take a look at what happens when passing a callback:
var Person = mongoose.model('Person', yourSchema);
// find each person with a last name matching 'Ghost', selecting the `name` and
`occupation` fields
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last,
person.occupation) // Space Ghost is a talk show host.
})
Here we see that the query was executed immediately and the results passed to our callback. All
callbacks in Mongoose use the pattern: callback(error, result). If an error occurs executing the
query, the errorparameter will contain an error document, and result will be null. If the query is
successful, the errorparameter will be null, and the result will be populated with the results of the
query.
Anywhere a callback is passed to a query in Mongoose, the callback follows the
patterncallback(error, results). What results is depends on the operation: For findOne() it is
a potentially-null single document, find() a list of documents, count() the number of
documents, update() thenumber of documents affected, etc. The API docs for Models provide more
detail on what is passed to the callbacks.
Now let's look at what happens when no callback is passed:
// find each person with a last name matching 'Ghost'
var query = Person.findOne({ 'name.last': 'Ghost' });
// selecting the `name` and `occupation` fields
query.select('name occupation');
// execute the query at a later time
query.exec(function (err, person) {
if (err) return handleError(err);
Most read
10
Updating refs
Now that we have a story we realized that the _creator was incorrect. We can update refs the
same as any other property through Mongoose's internal casting:
var guille = new Person({ name: 'Guillermo' });
guille.save(function (err) {
if (err) return handleError(err);
story._creator = guille;
console.log(story._creator.name);
// prints "Guillermo" in mongoose >= 3.6
// see https://github.com/LearnBoost/mongoose/wiki/3.6-release-notes
story.save(function (err) {
if (err) return handleError(err);
Story
.findOne({ title: /timex/i })
.populate({ path: '_creator', select: 'name' })
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name)
// prints "The creator is Guillermo"
})
})
})
The documents returned from query population become fully functional, removeable, saveable
documents unless the lean option is specified. Do not confuse them with sub docs. Take caution
when calling its remove method because you'll be removing it from the database, not just the array.
Populating an existing document
If we have an existing mongoose document and want to populate some of its paths, mongoose >=
3.6supports the document#populate() method.
Most read
12
living: Boolean,
updated: { type: Date, default: Date.now }
age: { type: Number, min: 18, max: 65 }
mixed: Schema.Types.Mixed,
_someId: Schema.Types.ObjectId,
array: [],
ofString: [String],
ofNumber: [Number],
ofDates: [Date],
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [Schema.Types.Mixed],
ofObjectId: [Schema.Types.ObjectId],
nested: {
stuff: { type: String, lowercase: true, trim: true }
}
})
// example use
var Thing = mongoose.model('Thing', schema);
var m = new Thing;
m.name = 'Statue of Liberty'
m.age = 125;
m.updated = new Date;
m.binary = new Buffer(0);
m.living = false;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
m._someId = new mongoose.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDate.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';
Most read
Mongoose
http://mongoosejs.com/docs/index.html
http://fredkschott.com/post/2014/06/require-and-the-module-system/
http://nodeguide.com/style.html
Mongoose#Schema()
The Mongoose Schema constructor
Example:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CatSchema = new Schema(..);
//other eg of declaring schema//
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var UserSchema = new Schema({
email: String
, hash: String
});
Schema.reserved
Reserved document keys.
show code
Keys in this object are names that are rejected in schema declarations b/c they conflict with
mongoose functionality. Using these key name will throw an error.
on, emit, _events, db, get, set, init, isNew, errors, schema, options, modelName,
collection, _pres, _posts, toObject
NOTE: Use of these terms as method names is permitted, but play at your own risk, as they may be
existing mongoose document methods you are stomping on.
var schema = new Schema(..);
schema.methods.init = function () {} // potentially breaking
Models
Models are fancy constructors compiled from our Schema definitions. Instances of these models
representdocuments which can be saved and retreived from our database. All document creation
and retreival from the database is handled by these models.
Compiling your first model
var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var UserSchema = new Schema({
email: String
, hash: String
});
var User = mongoose.model('User', UserSchema);
Constructing documents
Documents are instances of our model. Creating them and saving to the database is easy:
var Tank = mongoose.model('Tank', yourSchema);
var small = new Tank({ size: 'small' });
small.save(function (err) {
if (err) return handleError(err);
// saved!
})
// or
Tank.create({ size: 'small' }, function (err, small) {
if (err) return handleError(err);
// saved!
})
Note that no tanks will be created/removed until the connection your model uses is open. In this
case we are using mongoose.model() so let's open the default mongoose connection:
mongoose.connect('localhost', 'gettingstarted');
Querying
Finding documents is easy with Mongoose, which supports the rich query syntax of MongoDB.
Documents can be retreived using each models find, findById, findOne, or where static methods.
Tank.find({ size: 'small' }).where('createdDate').gt(oneYearAgo).exec(callback);
Removing
Models have a static remove method available for removing all documents matching conditions.
Tank.remove({ size: 'large' }, function (err) {
if (err) return handleError(err);
// removed!
});
Queries
Documents can be retrieved through several static helper methods of models.
Any model method which involves specifying query conditions can be executed two ways:
When a callback function:
 is passed, the operation will be executed immediately with the results passed to the callback.
 is not passed, an instance of Query is returned, which provides a
special QueryBuilder interface for you.
Let's take a look at what happens when passing a callback:
var Person = mongoose.model('Person', yourSchema);
// find each person with a last name matching 'Ghost', selecting the `name` and
`occupation` fields
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last,
person.occupation) // Space Ghost is a talk show host.
})
Here we see that the query was executed immediately and the results passed to our callback. All
callbacks in Mongoose use the pattern: callback(error, result). If an error occurs executing the
query, the errorparameter will contain an error document, and result will be null. If the query is
successful, the errorparameter will be null, and the result will be populated with the results of the
query.
Anywhere a callback is passed to a query in Mongoose, the callback follows the
patterncallback(error, results). What results is depends on the operation: For findOne() it is
a potentially-null single document, find() a list of documents, count() the number of
documents, update() thenumber of documents affected, etc. The API docs for Models provide more
detail on what is passed to the callbacks.
Now let's look at what happens when no callback is passed:
// find each person with a last name matching 'Ghost'
var query = Person.findOne({ 'name.last': 'Ghost' });
// selecting the `name` and `occupation` fields
query.select('name occupation');
// execute the query at a later time
query.exec(function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last,
person.occupation) // Space Ghost is a talk show host.
})
An instance of Query was returned which allows us to build up our query. Taking this example
further:
Person
.find({ occupation: /host/ })
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
.sort('-occupation')
.select('name occupation')
.exec(callback);
References to other documents
There are no joins in MongoDB but sometimes we still want references to documents in other
collections. This is where population comes in. Read more about how to include documents from
other collections in your query results here.
Streaming
Queries can be streamed from MongoDB to your application as well. Simply call the
query's stream method instead of exec to return an instance of QueryStream. Note: QueryStreams
are Node.js 0.8 style read streams not Node.js 0.10 style.
Population
There are no joins in MongoDB but sometimes we still want references to documents in other
collections. This is where population comes in.
Population is the process of automatically replacing the specified paths in the document with
document(s) from other collection(s). We may populate a single document, multiple documents,
plain object, multiple plain objects, or all objects returned from a query. Let's look at some examples.
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
So far we've created two Models. Our Person model has it's stories field set to an array
of ObjectIds. The refoption is what tells Mongoose which model to use during population, in our
case the Story model. All _ids we store here must be document _ids from the Story model. We
also declared the Story _creator property as aNumber, the same type as the _id used in
the personSchema. It is important to match the type of _id to the type of ref.
Note: ObjectId, Number, String, and Buffer are valid for use as refs.
Saving refs
Saving refs to other documents works the same way you normally save properties, just assign
the _id value:
var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });
aaron.save(function (err) {
if (err) return handleError(err);
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
story1.save(function (err) {
if (err) return handleError(err);
// thats it!
});
})
Population
So far we haven't done anything much different. We've merely created a Person and a Story. Now
let's take a look at populating our story's _creator using the query builder:
Story
.findOne({ title: 'Once upon a timex.' })
.populate('_creator')
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
})
Populated paths are no longer set to their original _id , their value is replaced with the mongoose
document returned from the database by performing a separate query before returning the results.
Arrays of refs work the same way. Just call the populate method on the query and an array of
documents will be returned in place of the original _ids.
Note: mongoose >= 3.6 exposes the original _ids used during population through
thedocument#populated() method.
Field selection
What if we only want a few specific fields returned for the populated documents? This can be
accomplished by passing the usual field name syntax as the second argument to the populate
method:
Story
.findOne({ title: /timex/i })
.populate('_creator', 'name') // only return the Persons name
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
console.log('The creators age is %s', story._creator.age);
// prints "The creators age is null'
})
Populating multiple paths
What if we wanted to populate multiple paths at the same time?
Story
.find(...)
.populate('fans author') // space delimited path names
.exec()
In mongoose >= 3.6, we can pass a space delimited string of path names to populate. Before 3.6
you must execute the populate() method multiple times.
Story
.find(...)
.populate('fans')
.populate('author')
.exec()
Queryconditions and other options
What if we wanted to populate our fans array based on their age, select just their names, and return
at most, any 5 of them?
Story
.find(...)
.populate({
path: 'fans',
match: { age: { $gte: 21 }},
select: 'name -_id',
options: { limit: 5 }
})
.exec()
Refs to children
We may find however, if we use the aaron object, we are unable to get a list of the stories. This is
because nostory objects were ever 'pushed' onto aaron.stories.
There are two perspectives here. First, it's nice to have aaron know which stories are his.
aaron.stories.push(story1);
aaron.save(callback);
This allows us to perform a find and populate combo:
Person
.findOne({ name: 'Aaron' })
.populate('stories') // only works if we pushed refs to children
.exec(function (err, person) {
if (err) return handleError(err);
console.log(person);
})
It is debatable that we really want two sets of pointers as they may get out of sync. Instead we could
skip populating and directly find() the stories we are interested in.
Story
.find({ _creator: aaron._id })
.exec(function (err, stories) {
if (err) return handleError(err);
console.log('The stories are an array: ', stories);
})
Updating refs
Now that we have a story we realized that the _creator was incorrect. We can update refs the
same as any other property through Mongoose's internal casting:
var guille = new Person({ name: 'Guillermo' });
guille.save(function (err) {
if (err) return handleError(err);
story._creator = guille;
console.log(story._creator.name);
// prints "Guillermo" in mongoose >= 3.6
// see https://github.com/LearnBoost/mongoose/wiki/3.6-release-notes
story.save(function (err) {
if (err) return handleError(err);
Story
.findOne({ title: /timex/i })
.populate({ path: '_creator', select: 'name' })
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name)
// prints "The creator is Guillermo"
})
})
})
The documents returned from query population become fully functional, removeable, saveable
documents unless the lean option is specified. Do not confuse them with sub docs. Take caution
when calling its remove method because you'll be removing it from the database, not just the array.
Populating an existing document
If we have an existing mongoose document and want to populate some of its paths, mongoose >=
3.6supports the document#populate() method.
Populating multiple existing documents
If we have one or many mongoose documents or even plain objects (like mapReduce output), we
may populate them using the Model.populate() method available in mongoose >= 3.6. This is
whatdocument#populate() and query#populate() use to populate documents.
Field selection difference fromv2 to v3
Field selection in v3 is slightly different than v2. Arrays of fields are no longer accepted. See
themigration guide and the example below for more detail.
// this works in v3
Story.findOne(..).populate('_creator', 'name age').exec(..);
// this doesn't
Story.findOne(..).populate('_creator', ['name', 'age']).exec(..);
SchemaTypes
SchemaTypes handle definition of path defaults, validation, getters, setters, field selection
defaults for queriesand other general characteristics for Strings and Numbers. Check out their
respective API documentation for more detail.
Following are all valid Schema Types.
 String
 Number
 Date
 Buffer
 Boolean
 Mixed
 Objectid
 Array
Example
var schema = new Schema({
name: String,
binary: Buffer,
living: Boolean,
updated: { type: Date, default: Date.now }
age: { type: Number, min: 18, max: 65 }
mixed: Schema.Types.Mixed,
_someId: Schema.Types.ObjectId,
array: [],
ofString: [String],
ofNumber: [Number],
ofDates: [Date],
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [Schema.Types.Mixed],
ofObjectId: [Schema.Types.ObjectId],
nested: {
stuff: { type: String, lowercase: true, trim: true }
}
})
// example use
var Thing = mongoose.model('Thing', schema);
var m = new Thing;
m.name = 'Statue of Liberty'
m.age = 125;
m.updated = new Date;
m.binary = new Buffer(0);
m.living = false;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
m._someId = new mongoose.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDate.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';
m.save(callback);
Getting Started
First be sure you have MongoDB and Node.js installed.
Next install Mongoose from the command line using npm:
$ npm install mongoose
Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first
thing we need to do is include mongoose in our project and open a connection to the test database
on our locally running instance of MongoDB.
// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
We have a pending connection to the test database running on localhost. We now need to get
notified if we connect successfully or if a connection error occurs:
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
// yay!
});
Once our connection opens, our callback will be called. For brevity, let's assume that all following
code is within this callback.
With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our
kittens.
var kittySchema = mongoose.Schema({
name: String
})
So far so good. We've got a schema with one property, name, which will be a String. The next step
is compiling our schema into a Model.
var Kitten = mongoose.model('Kitten', kittySchema)
A model is a class with which we construct documents. In this case, each document will be a kitten
with properties and behaviors as declared in our schema. Let's create a kitten document
representing the little guy we just met on the sidewalk outside:
var silence = new Kitten({ name: 'Silence' })
console.log(silence.name) // 'Silence'
Kittens can meow, so let's take a look at how to add "speak" functionality to our documents:
// NOTE: methods must be added to the schema before compiling it with
mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name"
console.log(greeting);
}
var Kitten = mongoose.model('Kitten', kittySchema)
Functions added to the methods property of a schema get compiled into the Model prototype and
exposed on each document instance:
var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak() // "Meow name is fluffy"
We have talking kittens! But we still haven't saved anything to MongoDB. Each document can be
saved to the database by calling its save method. The first argument to the callback will be an error if
any occured.
fluffy.save(function (err, fluffy) {
if (err) return console.error(err);
fluffy.speak();
});
Say time goes by and we want to display all the kittens we've seen. We can access all of the kitten
documents through our Kitten model.
Kitten.find(function (err, kittens) {
if (err) return console.error(err);
console.log(kittens)
})
We just logged all of the kittens in our db to the console. If we want to filter our kittens by name,
Mongoose supports MongoDBs rich querying syntax.
Kitten.find({ name: /^Fluff/ }, callback)
This performs a search for all documents with a name property that begins with "Fluff" and returns
the results to the callback.

More Related Content

What's hot (20)

Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
Amit Baghel
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
Akshay Ballarpure
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
Dmitry Buzdin
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Session7-XSS & CSRF
Session7-XSS & CSRFSession7-XSS & CSRF
Session7-XSS & CSRF
zakieh alizadeh
 
API Design- Best Practices
API Design-   Best PracticesAPI Design-   Best Practices
API Design- Best Practices
Prakash Bhandari
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
ArezooKmn
 
What is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | EdurekaWhat is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | Edureka
Edureka!
 
Angular Lifecycle Hooks
Angular Lifecycle HooksAngular Lifecycle Hooks
Angular Lifecycle Hooks
Squash Apps Pvt Ltd
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
Flipkart
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
L&T Technology Services Limited
 
Vue.js Getting Started
Vue.js Getting StartedVue.js Getting Started
Vue.js Getting Started
Murat Doğan
 
Docker 101: Introduction to Docker
Docker 101: Introduction to DockerDocker 101: Introduction to Docker
Docker 101: Introduction to Docker
Docker, Inc.
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
David Parsons
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
Wojciech Dzikowski
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
Halil Burak Cetinkaya
 
Web api
Web apiWeb api
Web api
Sudhakar Sharma
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
Imtiyaz Ahmad Khan
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
ArezooKmn
 
What is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | EdurekaWhat is REST API? REST API Concepts and Examples | Edureka
What is REST API? REST API Concepts and Examples | Edureka
Edureka!
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
Flipkart
 
Vue.js Getting Started
Vue.js Getting StartedVue.js Getting Started
Vue.js Getting Started
Murat Doğan
 
Docker 101: Introduction to Docker
Docker 101: Introduction to DockerDocker 101: Introduction to Docker
Docker 101: Introduction to Docker
Docker, Inc.
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
David Parsons
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
Wojciech Dzikowski
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 

Viewers also liked (14)

Getting Started With MongoDB and Mongoose
Getting Started With MongoDB and MongooseGetting Started With MongoDB and Mongoose
Getting Started With MongoDB and Mongoose
Ynon Perek
 
Mongoose: MongoDB object modelling for Node.js
Mongoose: MongoDB object modelling for Node.jsMongoose: MongoDB object modelling for Node.js
Mongoose: MongoDB object modelling for Node.js
Yuriy Bogomolov
 
Node-IL Meetup 12/2
Node-IL Meetup 12/2Node-IL Meetup 12/2
Node-IL Meetup 12/2
Ynon Perek
 
Nodejs mongoose
Nodejs mongooseNodejs mongoose
Nodejs mongoose
Fin Chen
 
Express JS
Express JSExpress JS
Express JS
Alok Guha
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express js
Ahmed Assaf
 
Trends und Anwendungsbeispiele im Life Science Bereich
Trends und Anwendungsbeispiele im Life Science BereichTrends und Anwendungsbeispiele im Life Science Bereich
Trends und Anwendungsbeispiele im Life Science Bereich
AWS Germany
 
Mongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is BrightMongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is Bright
aaronheckmann
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
Edureka!
 
3 Facts & Insights to Master the Work Ahead in Insurance
3 Facts & Insights to Master the Work Ahead in Insurance3 Facts & Insights to Master the Work Ahead in Insurance
3 Facts & Insights to Master the Work Ahead in Insurance
Cognizant
 
Cognizant's HCM Capabilities
Cognizant's HCM CapabilitiesCognizant's HCM Capabilities
Cognizant's HCM Capabilities
Arlene DeMita
 
50 Ways To Understand The Digital Customer Experience
50 Ways To Understand The Digital Customer Experience50 Ways To Understand The Digital Customer Experience
50 Ways To Understand The Digital Customer Experience
Cognizant
 
Cognizant organizational culture and structure
Cognizant organizational culture and structureCognizant organizational culture and structure
Cognizant organizational culture and structure
SOuvagya Kumar Jena
 
Express js
Express jsExpress js
Express js
Manav Prasad
 
Getting Started With MongoDB and Mongoose
Getting Started With MongoDB and MongooseGetting Started With MongoDB and Mongoose
Getting Started With MongoDB and Mongoose
Ynon Perek
 
Mongoose: MongoDB object modelling for Node.js
Mongoose: MongoDB object modelling for Node.jsMongoose: MongoDB object modelling for Node.js
Mongoose: MongoDB object modelling for Node.js
Yuriy Bogomolov
 
Node-IL Meetup 12/2
Node-IL Meetup 12/2Node-IL Meetup 12/2
Node-IL Meetup 12/2
Ynon Perek
 
Nodejs mongoose
Nodejs mongooseNodejs mongoose
Nodejs mongoose
Fin Chen
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express js
Ahmed Assaf
 
Trends und Anwendungsbeispiele im Life Science Bereich
Trends und Anwendungsbeispiele im Life Science BereichTrends und Anwendungsbeispiele im Life Science Bereich
Trends und Anwendungsbeispiele im Life Science Bereich
AWS Germany
 
Mongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is BrightMongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is Bright
aaronheckmann
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
Edureka!
 
3 Facts & Insights to Master the Work Ahead in Insurance
3 Facts & Insights to Master the Work Ahead in Insurance3 Facts & Insights to Master the Work Ahead in Insurance
3 Facts & Insights to Master the Work Ahead in Insurance
Cognizant
 
Cognizant's HCM Capabilities
Cognizant's HCM CapabilitiesCognizant's HCM Capabilities
Cognizant's HCM Capabilities
Arlene DeMita
 
50 Ways To Understand The Digital Customer Experience
50 Ways To Understand The Digital Customer Experience50 Ways To Understand The Digital Customer Experience
50 Ways To Understand The Digital Customer Experience
Cognizant
 
Cognizant organizational culture and structure
Cognizant organizational culture and structureCognizant organizational culture and structure
Cognizant organizational culture and structure
SOuvagya Kumar Jena
 
Ad

Similar to Mongoose getting started-Mongo Db with Node js (20)

Node js crash course session 5
Node js crash course   session 5Node js crash course   session 5
Node js crash course session 5
Abdul Rahman Masri Attal
 
Introduction to MongoDB – A NoSQL Database
Introduction to MongoDB – A NoSQL DatabaseIntroduction to MongoDB – A NoSQL Database
Introduction to MongoDB – A NoSQL Database
manikgupta2k04
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
Jackson Tian
 
Mongoose and MongoDB 101
Mongoose and MongoDB 101Mongoose and MongoDB 101
Mongoose and MongoDB 101
Will Button
 
Mongo db basics
Mongo db basicsMongo db basics
Mongo db basics
Harischandra M K
 
Mongo db queries
Mongo db queriesMongo db queries
Mongo db queries
ssuser6d5faa
 
Mongo DB
Mongo DBMongo DB
Mongo DB
SRM University Delhi-NCR sonepat
 
Mongo db basic installation
Mongo db basic installationMongo db basic installation
Mongo db basic installation
Kishor Parkhe
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
joergreichert
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
S.Shayan Daneshvar
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
chriskite
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
Jaya Naresh Kovela
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operations
anujaggarwal49
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
Norberto Leite
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
MongoDB
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
MongoDB
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016
Mukesh Tilokani
 
Mongo db
Mongo dbMongo db
Mongo db
Ramakrishna kapa
 
MongoDB_ppt.pptx
MongoDB_ppt.pptxMongoDB_ppt.pptx
MongoDB_ppt.pptx
1AP18CS037ShirishKul
 
Introduction to MongoDB – A NoSQL Database
Introduction to MongoDB – A NoSQL DatabaseIntroduction to MongoDB – A NoSQL Database
Introduction to MongoDB – A NoSQL Database
manikgupta2k04
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
Jackson Tian
 
Mongoose and MongoDB 101
Mongoose and MongoDB 101Mongoose and MongoDB 101
Mongoose and MongoDB 101
Will Button
 
Mongo db basic installation
Mongo db basic installationMongo db basic installation
Mongo db basic installation
Kishor Parkhe
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
joergreichert
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
chriskite
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operations
anujaggarwal49
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
Norberto Leite
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
MongoDB
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
MongoDB
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016
Mukesh Tilokani
 
Ad

More from Pallavi Srivastava (8)

Various Types of Vendors that Exist in the Software Ecosystem
Various Types of Vendors that Exist in the Software EcosystemVarious Types of Vendors that Exist in the Software Ecosystem
Various Types of Vendors that Exist in the Software Ecosystem
Pallavi Srivastava
 
ISR Project - Education to underprivileged
ISR Project - Education to underprivilegedISR Project - Education to underprivileged
ISR Project - Education to underprivileged
Pallavi Srivastava
 
We like project
We like projectWe like project
We like project
Pallavi Srivastava
 
Java Docs
Java DocsJava Docs
Java Docs
Pallavi Srivastava
 
Node js getting started
Node js getting startedNode js getting started
Node js getting started
Pallavi Srivastava
 
Smart dust
Smart dustSmart dust
Smart dust
Pallavi Srivastava
 
Summer Training report at TATA CMC
Summer Training report at TATA CMCSummer Training report at TATA CMC
Summer Training report at TATA CMC
Pallavi Srivastava
 
Semantic web
Semantic web Semantic web
Semantic web
Pallavi Srivastava
 

Recently uploaded (20)

Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 

Mongoose getting started-Mongo Db with Node js

  • 1. Mongoose http://mongoosejs.com/docs/index.html http://fredkschott.com/post/2014/06/require-and-the-module-system/ http://nodeguide.com/style.html Mongoose#Schema() The Mongoose Schema constructor Example: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var CatSchema = new Schema(..); //other eg of declaring schema// var Schema = mongoose.Schema , ObjectId = Schema.ObjectId; var UserSchema = new Schema({ email: String , hash: String }); Schema.reserved Reserved document keys. show code Keys in this object are names that are rejected in schema declarations b/c they conflict with mongoose functionality. Using these key name will throw an error. on, emit, _events, db, get, set, init, isNew, errors, schema, options, modelName, collection, _pres, _posts, toObject
  • 2. NOTE: Use of these terms as method names is permitted, but play at your own risk, as they may be existing mongoose document methods you are stomping on. var schema = new Schema(..); schema.methods.init = function () {} // potentially breaking Models Models are fancy constructors compiled from our Schema definitions. Instances of these models representdocuments which can be saved and retreived from our database. All document creation and retreival from the database is handled by these models. Compiling your first model var schema = new mongoose.Schema({ name: 'string', size: 'string' }); var Tank = mongoose.model('Tank', schema); var Schema = mongoose.Schema , ObjectId = Schema.ObjectId; var UserSchema = new Schema({ email: String , hash: String }); var User = mongoose.model('User', UserSchema); Constructing documents Documents are instances of our model. Creating them and saving to the database is easy: var Tank = mongoose.model('Tank', yourSchema); var small = new Tank({ size: 'small' }); small.save(function (err) { if (err) return handleError(err); // saved!
  • 3. }) // or Tank.create({ size: 'small' }, function (err, small) { if (err) return handleError(err); // saved! }) Note that no tanks will be created/removed until the connection your model uses is open. In this case we are using mongoose.model() so let's open the default mongoose connection: mongoose.connect('localhost', 'gettingstarted'); Querying Finding documents is easy with Mongoose, which supports the rich query syntax of MongoDB. Documents can be retreived using each models find, findById, findOne, or where static methods. Tank.find({ size: 'small' }).where('createdDate').gt(oneYearAgo).exec(callback); Removing Models have a static remove method available for removing all documents matching conditions. Tank.remove({ size: 'large' }, function (err) { if (err) return handleError(err); // removed! }); Queries Documents can be retrieved through several static helper methods of models. Any model method which involves specifying query conditions can be executed two ways: When a callback function:
  • 4.  is passed, the operation will be executed immediately with the results passed to the callback.  is not passed, an instance of Query is returned, which provides a special QueryBuilder interface for you. Let's take a look at what happens when passing a callback: var Person = mongoose.model('Person', yourSchema); // find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fields Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) { if (err) return handleError(err); console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host. }) Here we see that the query was executed immediately and the results passed to our callback. All callbacks in Mongoose use the pattern: callback(error, result). If an error occurs executing the query, the errorparameter will contain an error document, and result will be null. If the query is successful, the errorparameter will be null, and the result will be populated with the results of the query. Anywhere a callback is passed to a query in Mongoose, the callback follows the patterncallback(error, results). What results is depends on the operation: For findOne() it is a potentially-null single document, find() a list of documents, count() the number of documents, update() thenumber of documents affected, etc. The API docs for Models provide more detail on what is passed to the callbacks. Now let's look at what happens when no callback is passed: // find each person with a last name matching 'Ghost' var query = Person.findOne({ 'name.last': 'Ghost' }); // selecting the `name` and `occupation` fields query.select('name occupation'); // execute the query at a later time query.exec(function (err, person) { if (err) return handleError(err);
  • 5. console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host. }) An instance of Query was returned which allows us to build up our query. Taking this example further: Person .find({ occupation: /host/ }) .where('name.last').equals('Ghost') .where('age').gt(17).lt(66) .where('likes').in(['vaporizing', 'talking']) .limit(10) .sort('-occupation') .select('name occupation') .exec(callback); References to other documents There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where population comes in. Read more about how to include documents from other collections in your query results here. Streaming Queries can be streamed from MongoDB to your application as well. Simply call the query's stream method instead of exec to return an instance of QueryStream. Note: QueryStreams are Node.js 0.8 style read streams not Node.js 0.10 style. Population There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where population comes in. Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple documents, plain object, multiple plain objects, or all objects returned from a query. Let's look at some examples. var mongoose = require('mongoose')
  • 6. , Schema = mongoose.Schema var personSchema = Schema({ _id : Number, name : String, age : Number, stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); var storySchema = Schema({ _creator : { type: Number, ref: 'Person' }, title : String, fans : [{ type: Number, ref: 'Person' }] }); var Story = mongoose.model('Story', storySchema); var Person = mongoose.model('Person', personSchema); So far we've created two Models. Our Person model has it's stories field set to an array of ObjectIds. The refoption is what tells Mongoose which model to use during population, in our case the Story model. All _ids we store here must be document _ids from the Story model. We also declared the Story _creator property as aNumber, the same type as the _id used in the personSchema. It is important to match the type of _id to the type of ref. Note: ObjectId, Number, String, and Buffer are valid for use as refs. Saving refs Saving refs to other documents works the same way you normally save properties, just assign the _id value: var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 }); aaron.save(function (err) { if (err) return handleError(err); var story1 = new Story({ title: "Once upon a timex.", _creator: aaron._id // assign the _id from the person
  • 7. }); story1.save(function (err) { if (err) return handleError(err); // thats it! }); }) Population So far we haven't done anything much different. We've merely created a Person and a Story. Now let's take a look at populating our story's _creator using the query builder: Story .findOne({ title: 'Once upon a timex.' }) .populate('_creator') .exec(function (err, story) { if (err) return handleError(err); console.log('The creator is %s', story._creator.name); // prints "The creator is Aaron" }) Populated paths are no longer set to their original _id , their value is replaced with the mongoose document returned from the database by performing a separate query before returning the results. Arrays of refs work the same way. Just call the populate method on the query and an array of documents will be returned in place of the original _ids. Note: mongoose >= 3.6 exposes the original _ids used during population through thedocument#populated() method. Field selection What if we only want a few specific fields returned for the populated documents? This can be accomplished by passing the usual field name syntax as the second argument to the populate method: Story .findOne({ title: /timex/i })
  • 8. .populate('_creator', 'name') // only return the Persons name .exec(function (err, story) { if (err) return handleError(err); console.log('The creator is %s', story._creator.name); // prints "The creator is Aaron" console.log('The creators age is %s', story._creator.age); // prints "The creators age is null' }) Populating multiple paths What if we wanted to populate multiple paths at the same time? Story .find(...) .populate('fans author') // space delimited path names .exec() In mongoose >= 3.6, we can pass a space delimited string of path names to populate. Before 3.6 you must execute the populate() method multiple times. Story .find(...) .populate('fans') .populate('author') .exec() Queryconditions and other options What if we wanted to populate our fans array based on their age, select just their names, and return at most, any 5 of them? Story .find(...) .populate({ path: 'fans',
  • 9. match: { age: { $gte: 21 }}, select: 'name -_id', options: { limit: 5 } }) .exec() Refs to children We may find however, if we use the aaron object, we are unable to get a list of the stories. This is because nostory objects were ever 'pushed' onto aaron.stories. There are two perspectives here. First, it's nice to have aaron know which stories are his. aaron.stories.push(story1); aaron.save(callback); This allows us to perform a find and populate combo: Person .findOne({ name: 'Aaron' }) .populate('stories') // only works if we pushed refs to children .exec(function (err, person) { if (err) return handleError(err); console.log(person); }) It is debatable that we really want two sets of pointers as they may get out of sync. Instead we could skip populating and directly find() the stories we are interested in. Story .find({ _creator: aaron._id }) .exec(function (err, stories) { if (err) return handleError(err); console.log('The stories are an array: ', stories); })
  • 10. Updating refs Now that we have a story we realized that the _creator was incorrect. We can update refs the same as any other property through Mongoose's internal casting: var guille = new Person({ name: 'Guillermo' }); guille.save(function (err) { if (err) return handleError(err); story._creator = guille; console.log(story._creator.name); // prints "Guillermo" in mongoose >= 3.6 // see https://github.com/LearnBoost/mongoose/wiki/3.6-release-notes story.save(function (err) { if (err) return handleError(err); Story .findOne({ title: /timex/i }) .populate({ path: '_creator', select: 'name' }) .exec(function (err, story) { if (err) return handleError(err); console.log('The creator is %s', story._creator.name) // prints "The creator is Guillermo" }) }) }) The documents returned from query population become fully functional, removeable, saveable documents unless the lean option is specified. Do not confuse them with sub docs. Take caution when calling its remove method because you'll be removing it from the database, not just the array. Populating an existing document If we have an existing mongoose document and want to populate some of its paths, mongoose >= 3.6supports the document#populate() method.
  • 11. Populating multiple existing documents If we have one or many mongoose documents or even plain objects (like mapReduce output), we may populate them using the Model.populate() method available in mongoose >= 3.6. This is whatdocument#populate() and query#populate() use to populate documents. Field selection difference fromv2 to v3 Field selection in v3 is slightly different than v2. Arrays of fields are no longer accepted. See themigration guide and the example below for more detail. // this works in v3 Story.findOne(..).populate('_creator', 'name age').exec(..); // this doesn't Story.findOne(..).populate('_creator', ['name', 'age']).exec(..); SchemaTypes SchemaTypes handle definition of path defaults, validation, getters, setters, field selection defaults for queriesand other general characteristics for Strings and Numbers. Check out their respective API documentation for more detail. Following are all valid Schema Types.  String  Number  Date  Buffer  Boolean  Mixed  Objectid  Array Example var schema = new Schema({ name: String, binary: Buffer,
  • 12. living: Boolean, updated: { type: Date, default: Date.now } age: { type: Number, min: 18, max: 65 } mixed: Schema.Types.Mixed, _someId: Schema.Types.ObjectId, array: [], ofString: [String], ofNumber: [Number], ofDates: [Date], ofBuffer: [Buffer], ofBoolean: [Boolean], ofMixed: [Schema.Types.Mixed], ofObjectId: [Schema.Types.ObjectId], nested: { stuff: { type: String, lowercase: true, trim: true } } }) // example use var Thing = mongoose.model('Thing', schema); var m = new Thing; m.name = 'Statue of Liberty' m.age = 125; m.updated = new Date; m.binary = new Buffer(0); m.living = false; m.mixed = { any: { thing: 'i want' } }; m.markModified('mixed'); m._someId = new mongoose.Types.ObjectId; m.array.push(1); m.ofString.push("strings!"); m.ofNumber.unshift(1,2,3,4); m.ofDate.addToSet(new Date); m.ofBuffer.pop(); m.ofMixed = [1, [], 'three', { four: 5 }]; m.nested.stuff = 'good';
  • 13. m.save(callback); Getting Started First be sure you have MongoDB and Node.js installed. Next install Mongoose from the command line using npm: $ npm install mongoose Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we need to do is include mongoose in our project and open a connection to the test database on our locally running instance of MongoDB. // getting-started.js var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); We have a pending connection to the test database running on localhost. We now need to get notified if we connect successfully or if a connection error occurs: var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function (callback) { // yay! }); Once our connection opens, our callback will be called. For brevity, let's assume that all following code is within this callback. With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our kittens. var kittySchema = mongoose.Schema({ name: String })
  • 14. So far so good. We've got a schema with one property, name, which will be a String. The next step is compiling our schema into a Model. var Kitten = mongoose.model('Kitten', kittySchema) A model is a class with which we construct documents. In this case, each document will be a kitten with properties and behaviors as declared in our schema. Let's create a kitten document representing the little guy we just met on the sidewalk outside: var silence = new Kitten({ name: 'Silence' }) console.log(silence.name) // 'Silence' Kittens can meow, so let's take a look at how to add "speak" functionality to our documents: // NOTE: methods must be added to the schema before compiling it with mongoose.model() kittySchema.methods.speak = function () { var greeting = this.name ? "Meow name is " + this.name : "I don't have a name" console.log(greeting); } var Kitten = mongoose.model('Kitten', kittySchema) Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance: var fluffy = new Kitten({ name: 'fluffy' }); fluffy.speak() // "Meow name is fluffy" We have talking kittens! But we still haven't saved anything to MongoDB. Each document can be saved to the database by calling its save method. The first argument to the callback will be an error if any occured. fluffy.save(function (err, fluffy) { if (err) return console.error(err); fluffy.speak();
  • 15. }); Say time goes by and we want to display all the kittens we've seen. We can access all of the kitten documents through our Kitten model. Kitten.find(function (err, kittens) { if (err) return console.error(err); console.log(kittens) }) We just logged all of the kittens in our db to the console. If we want to filter our kittens by name, Mongoose supports MongoDBs rich querying syntax. Kitten.find({ name: /^Fluff/ }, callback) This performs a search for all documents with a name property that begins with "Fluff" and returns the results to the callback.