-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
index.html
36 lines (34 loc) · 7.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!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.3.1</title><link href="http://fonts.googleapis.com/css?family=Anonymous+Pro:400,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="http://github.com/learnboost/mongoose"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" 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="faq"><a href="./faq.html">FAQ</a></li><li class="plugins"><a href="http://plugins.mongoosejs.com">plugins</a></li><li class="changelog"><a href="http://github.com/learnboost/mongoose/tree/master/History.md">change log</a></li><li class="support"><a href="../index.html#support">support</a></li><li class="fork"><a href="http://github.com/learnboost/mongoose">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="http://github.com/learnboost/mongoose/contributors">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="http://www.mongodb.org/downloads">MongoDB</a> and <a href="http://nodejs.org/">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(silence.name) <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 = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> greeting = <span class="keyword">this</span>.name
? <span class="string">"Meow name is "</span> + <span class="keyword">this</span>.name
: <span class="string">"I don't have a name"</span>
console.log(greeting);
}
<span class="keyword">var</span> Kitten = db.model(<span class="string">'Kitten'</span>, 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">fluffy.save(<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>
console.log(kittens)
})</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']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();</script></body></html>