Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

41 lines (38 sloc) 6.94 KB
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Mongoose Quick Start v3.0.0rc0</title><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"><style></style></head><body class="api"><a id="forkbanner" href=""><img style="position: absolute; top: 0; right: 0; border: 0;" src="" alt="Fork me on GitHub"></a><div id="links"><div id="header"><h1><a href="../index.html"><div class="mongoose">Mongoose</div></a></h1></div><ul><li class="home"><a href="../index.html">home</a></li><li class="plugins"><a href="">plugins</a></li><li class="changelog"><a href="">change log</a></li><li class="support"><a href="../index.html#support">support</a></li><li class="fork"><a href="">fork</a></li><li class="guide"><a href="./guide.html">guide</a></li><li class="api"><a href="./api.html">api docs</a></li><li class="quickstart"><a href="./index.html">quick start</a></li><li class="contrib"><a href="">contributors</a></li><li class="prior"><a href="./prior.html">prior releases</a></li></ul></div><div id="content"><div class="module"><h2>Getting Started</h2><p><em>First be sure you have <a href="">MongoDB</a> and <a href="">Nodejs</a> installed.</em></p><p>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.</p><pre><code class="javascript"><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>)
, db = mongoose.createConnection(<span class="string">'localhost'</span>, <span class="string">'test'</span>);</code></pre><p>We have a pending connection object to the test database running on localhost. We now need to get notified if we connect successfully or if a connection error occurs:</p><pre><code class="javascript">db.on(<span class="string">'error'</span>, console.error.bind(console, <span class="string">'connection error:'</span>));
db.once(<span class="string">'open'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="comment">// yay!</span>
});</code></pre><p>Once our connection opens, our callback will be called. For brevity, let's assume that all following code is within this callback.</p><p>With Mongoose, everything is derived from a <a href="./api.html#schema_Schema">Schema</a>. Let's get a reference to it and define our kittens.</p><pre><code class="javascript"><span class="keyword">var</span> kittySchema = <span class="keyword">new</span> mongoose.Schema({
name: String
})</code></pre><p>So far so good. We've got a schema with one property, <code>name</code>, which will be a <code>String</code>. The next step is compiling our schema into a <a href="./api.html#model_Model">model</a>.</p><pre><code class="javascript"><span class="keyword">var</span> Kitten = db.model(<span class="string">'Kitten'</span>, kittySchema)</code></pre><p>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:</p><pre><code class="javascript"><span class="keyword">var</span> silence = <span class="keyword">new</span> Kitten({ name: <span class="string">'Silence'</span> })
console.log( <span class="comment">// 'Silence'</span></code></pre><p>Kittens can meow, so let's take a look at how to add "speak" functionality to our documents:</p><pre><code class="javascript">kittySchema.methods.speak = function () {
var greeting =
? 'Meow name is ' +
: 'I don't have a name'
var Kitten = db.model('Kitten', kittySchema)</code></pre><p>Functions added to the <code>methods</code> property of a schema get compiled into the Model prototype and exposed on each document instance:</p><pre><code class="javascript"><span class="keyword">var</span> fluffy = <span class="keyword">new</span> Kitten({ name: <span class="string">'fluffy'</span> });
fluffy.speak() <span class="comment">// "Meow name is fluffy"</span></code></pre><p>We have talking kittens! But we still haven't saved anything to MongoDB.
Each document can be saved to the database by calling its <code>save</code> method. The first argument to the callback will be an error if any occured.</p><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
<span class="keyword">if</span> (err) <span class="comment">// TODO handle the error</span>
console.log(<span class="string">'meow'</span>)
});</code></pre><p>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.</p><pre><code class="javascript">Kitten.find(<span class="function"><span class="keyword">function</span> <span class="params">(err, kittens)</span> {</span>
<span class="keyword">if</span> (err) <span class="comment">// TODO handle err</span>
})</code></pre><p>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.</p><pre><code class="javascript">Kitten.find({ name: <span class="regexp">/fluff/i</span> }, callback)</code></pre><p>This performs a case-insensitive search for all documents with a name property containing "fluff" and returns the results to the callback.</p><h3>Congratulations</h3><p>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 <a href="guide.html">guide</a>, or <a href="api.html">api docs</a> for more.</p></div></div><script>document.body.className = 'load';</script><script>/*
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1122274-9']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
// get number of groups members?
Jump to Line
Something went wrong with that request. Please try again.