Revision 08d9ab37543b09a3032a8fb4350592325032210f authored by Valeri Karpov on 17 August 2014, 22:25:49 UTC, committed by Valeri Karpov on 17 August 2014, 22:25:49 UTC
1 parent 6657f2d
Raw File
models.html
<!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 Models 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"></head><body><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><ul><li class="double"><a href="./guide.html">schemas</a><ul><li class="schematypes"><a href="./schematypes.html"><span>schema</span>types</a></li></ul></li><li><a href="./models.html">models</a></li><li class="double"><a href="./documents.html">documents</a><ul><li class="subdocs"><a href="./subdocs.html">sub docs</a></li></ul></li><li><a href="./queries.html">queries</a></li><li><a href="./validation.html">validation</a></li><li><a href="./middleware.html">middleware</a></li><li><a href="./populate.html">population</a></li><li><a href="./connections.html">connections</a></li><li><a href="./plugins.html">plugins</a></li><li><a href="https://github.com/LearnBoost/mongoose/blob/master/CONTRIBUTING.md">contributing</a></li><li><a href="./migration.html">migrating from 2.x</a></li></ul></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>Models</h2><p><a href="./api.html#model-js">Models</a> are fancy constructors compiled from our <code>Schema</code> definitions. Instances of these models represent <a href="./documents.html">documents</a> which can be saved and retreived from our database. All document creation and retreival from the database is handled by these models.</p><h3>Compiling your first model</h3><pre><code class="javascript"><span class="keyword">var</span> schema = <span class="keyword">new</span> Schema({ name: <span class="string">'string'</span>, size: <span class="string">'string'</span> });
<span class="keyword">var</span> Tank = mongoose.model(<span class="string">'Tank'</span>, schema);

<span class="comment">// or, if you are using separate connections</span>
<span class="keyword">var</span> db = mongoose.createConnection(..);
<span class="keyword">var</span> Tank = db.model(<span class="string">'Tank'</span>, schema);
</code></pre><h3>Constructing documents</h3><p><a href="./documents.html">Documents</a> are instances of our model. Creating them and saving to the database is easy:</p><pre><code class="javascript"><span class="keyword">var</span> Tank = db.model(<span class="string">'Tank'</span>, yourSchema);

<span class="keyword">var</span> small = <span class="keyword">new</span> Tank({ size: <span class="string">'small'</span> });
small.save(<span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
  <span class="keyword">if</span> (err) <span class="keyword">return</span> handleError(err);
  <span class="comment">// saved!</span>
})

<span class="comment">// or</span>

Tank.create({ size: <span class="string">'small'</span> }, <span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
  <span class="keyword">if</span> (err) <span class="keyword">return</span> handleError(err);
  <span class="comment">// saved!</span>
})
</code></pre><h3>Querying</h3><p>Finding documents is easy with Mongoose, which supports the <a href="http://www.mongodb.org/display/DOCS/Advanced+Queries">rich</a> query syntax of MongoDB. Documents can be retreived using each <code>models</code> <a href="./api.html#model_Model-find">find</a>, <a href="./api.html#model_Model-findById">findById</a>, <a href="./api.html#model_Model-findOne">findOne</a>, or <a href="./api.html#model_Model-where">where</a> static methods.</p><pre><code class="javascript">Tank.find({ type: <span class="string">'small'</span> }).where(<span class="string">'createdDate'</span>).gt(oneYearAgo).exec(callback);</code></pre><p>See the chapter on <a href="./queries.html">querying</a> for more details on how to use the <a href="./api.html#query-js">Query</a> api.</p><h3>Removing</h3><p>Models have a static <code>remove</code> method available for removing all documents matching <code>conditions</code>.</p><pre><code class="javascript">Tank.remove({ size: <span class="string">'large'</span> }, <span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
  <span class="keyword">if</span> (err) <span class="keyword">return</span> handleError(err);
  <span class="comment">// removed!</span>
});</code></pre><h3>Updating</h3><p>Each <code>model</code> has its own <code>update</code> method for modifying documents in the database without returning them to your application. See the <a href="./api.html#model_Model-update">API</a> docs for more detail.</p><h3>Yet more</h3><p>The <a href="./api.html#model_Model">API docs</a> cover many additional methods available like <a href="./api.html#model_Model-count">count</a>, <a href="./api.html#model_Model-mapReduce">mapReduce</a>, <a href="./api.html#model_Model-aggregate">aggregate</a>, and more.</p><h3 id="next">Next Up</h3><p>Now that we&#39;ve covered <code>Models</code>, let&#39;s take a look at <a href="/docs/documents.html">Documents</a>.</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>
back to top