Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.
Defining a model is as easy as:
var Comments =title : Stringbody : Stringdate : Date;var BlogPost =author : ObjectIdtitle : Stringbody : Stringdate : Datecomments : Commentsmeta :votes : Numberfavs : Number;mongoose;
The recommended way is through the excellent NPM:
$ npm install mongoose
Otherwise, you can check it in your repository and then expose it:
$ git clone email@example.com:LearnBoost/mongoose.git support/mongoose/
// in your coderequirepaths
Then you can
First, we need to define a connection. If your app uses only one database, you
mongose.connect. If you need to create additional connections, use
createConnection take a
mongodb:// URI, or the parameters
host, database, port.
var mongoose = ;mongoose;
Once connected, the
open event is fired on the
Connection instance. If
mongoose.createConnection return value is a
Important! Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.
Models are defined through the
var Schema = mongooseSchemaObjectId = SchemaObjectId;var BlogPost =author : ObjectIdtitle : Stringbody : Stringdate : Date;
Aside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:
The following example shows some of these features:
var Comment =name : type: String default: 'hahaha'age : type: Number min: 18 index: truebio : type: String match: /[a-z]/date : type: Date default: Datenow;// a setterComment;// middlewareComment;
Take a look at the example in
examples/schema.js for an end-to-end example of
(almost) all the functionality available.
Once we define a model through
mongoose.model('ModelName', mySchema), we can
access it through the same function
var myModel = mongoose;
We can then instantiate it, and save it:
var instance = ;instancemykey = 'hello';instance;
Or we can find documents from the same collection
You can also
update, etc. For more details check out
the API docs.
In the first example snippet, we defined a key in the Schema that looks like:
Comments is a
Schema we created. This means that creating embedded
documents is as simple as:
// retrieve my modelvar BlogPost = mongoose;// create a blog postvar post = ;// create a commentpostcomments;post;
The same goes for removing them:
Embedded documents enjoy all the same features as your models. Defaults,
validators, middleware. Whenever an error occurs, it's bubbled to the
error callback, so error handling is a snap!
Mongoose interacts with your embedded documents in arrays atomically, out of the box.
Middleware is one of the most exciting features about Mongoose 1.0. Middleware takes away all the pain of nested callbacks.
Middleware are defined at the Schema level and are applied when the methods
init (when a document is initialized with data from MongoDB),
a document or embedded document is saved).
There's two types of middleware:
They're executed one after the other, when each middleware calls
You can also intercept the
method's incoming arguments via your middleware --
methodArg2, etc in the
pre definition above. See
section "Intercepting and mutating method arguments" below.
Parallel middleware can
next() immediately, but the final argument will be
called when all the parallel middleware have called
If any middleware calls
done with an
Error instance, the flow is
interrupted, and the error is passed to the function passed as an argument.
You can intercept method arguments via middleware.
For example, this would allow you to broadcast changes about your Documents
every time someone
sets a path in your Document to a new value:
Moreover, you can mutate the incoming
method arguments so that subsequent
middleware see different values for those arguments. To do so, just pass the
new values to
// pre declaration is chainable
Please subscribe to the Google Groups mailing list.
The following plugins are currently available for use with mongoose:
Make a fork of
mongoose, then clone it in your computer. The
contains the current stable release, and the
develop branch the next upcoming
master is at
develop will contain the upcoming
1 branch is nearing its completion).
Copyright (c) 2010 LearnBoost <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.