Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

109 lines (105 sloc) 5.551 kb
doctype html
meta(name="viewport", content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no")
title Mongoose Quick Start v#{package.version}
link(href=',700|Droid+Sans+Mono|Open+Sans:400,700|Linden+Hill|Quattrocento:400,700|News+Cycle:400,700|Antic+Slab|Cabin+Condensed:400,700', rel='stylesheet', type='text/css')
link(href='/docs/css/default.css', rel='stylesheet', type='text/css')
link(href='/docs/css/guide.css', rel='stylesheet', type='text/css')
img(style="position: absolute; top: 0; right: 0; border: 0;", src="", alt="Fork me on GitHub")
.mongoose Mongoose
include includes/nav
h2 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');
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](/docs/guide.html). 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](/docs/models.html).
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'
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 =
? "Meow name is " +
: "I don't have a name"
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](/docs/api.html#model_Model-save) method. The first argument to the callback will be an error if any occured.
:js (err, fluffy) {
if (err) // TODO handle the error
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](/docs/models.html).
Kitten.find(function (err, kittens) {
if (err) // TODO handle err
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](/docs/queries.html) 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.
h3 Congratulations
That's the end of our quick start. We created a schema, added a custom document method, saved and queried kittens in MongoDB using Mongoose. Head over to the [guide](guide.html), or [API docs](api.html) for more.
document.body.className = 'load';
include includes/googleanalytics
Jump to Line
Something went wrong with that request. Please try again.