Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

67 lines (63 sloc) 9.289 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 SchemaTypes 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>SchemaTypes</h2><p>SchemaTypes handle definition of path <a href="./api.html#schematype_SchemaType-default">defaults</a>, <a href="./api.html#schematype_SchemaType-validate">validation</a>, <a href="./api.html#schematype_SchemaType-get">getters</a>, <a href="./api.html#schematype_SchemaType-set">setters</a>, <a href="./api.html#schematype_SchemaType-select">field selection defaults</a> for <a href="./api.html#query-js">queries</a> and other general characteristics for <a href="./api.html#schema-string-js">Strings</a> and <a href="./api.html#schema-number-js">Numbers</a>. Check out their respective API documentation for more detail.</p><p>Following are all valid <a href="./api.html#schema_Schema-Types">Schema Types</a>.</p><ul><li>String</li><li>Number</li><li>Date</li><li>Buffer</li><li>Boolean</li><li>Mixed</li><li>ObjectId</li><li>Array</li></ul><h4>Example</h4><pre><code class="javascript"><span class="keyword">var</span> schema = <span class="keyword">new</span> Schema({
name: String,
binary: Buffer,
living: Boolean,
updated: { type: Date, <span class="keyword">default</span>: Date.now }
age: { type: Number, min: <span class="number">18</span>, max: <span class="number">65</span> }
mixed: Schema.Types.Mixed,
_someId: Schema.Types.ObjectId,
array: [],
ofString: [String],
ofNumber: [Number],
ofDates: [Date],
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [Schema.Types.Mixed],
ofObjectId: [Schema.Types.ObjectId],
nested: {
stuff: { type: String, lowercase: <span class="literal">true</span>, trim: <span class="literal">true</span> }
}
})
<span class="comment">// example use</span>
<span class="keyword">var</span> Thing = mongoose.model(<span class="string">'Thing'</span>, schema);
<span class="keyword">var</span> m = <span class="keyword">new</span> Thing;
m.name = <span class="string">'Statue of Liberty'</span>
m.age = <span class="number">125</span>;
m.updated = <span class="keyword">new</span> Date;
m.binary = <span class="keyword">new</span> Buffer(<span class="number">0</span>);
m.living = <span class="literal">false</span>;
m.mixed = {[ any: { thing: <span class="string">'i want'</span> } ]};
m.markModified(<span class="string">'mixed'</span>);
m._someId = <span class="keyword">new</span> mongoose.Types.ObjectId;
m.array.push(<span class="number">1</span>);
m.ofString.push(<span class="string">"strings!"</span>);
m.ofNumber.unshift(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>);
m.ofDate.addToSet(<span class="keyword">new</span> Date);
m.ofBuffer.pop();
m.ofMixed = [<span class="number">1</span>, [], <span class="string">'three'</span>, { four: <span class="number">5</span> }];
m.nested.stuff = <span class="string">'good'</span>;
m.save(callback);
</code></pre><h3>Usage notes:</h3><h4 id="mixed">Mixed</h4><p>An "anything goes" SchemaType, its flexibility comes at a trade-off of it being harder to maintain. Mixed is available either through Schema.Types.Mixed or by passing an empty object literal. The following are equivalent:</p><pre><code class="javascript"><span class="keyword">var</span> Any = <span class="keyword">new</span> Schema({ any: {} });
<span class="keyword">var</span> Any = <span class="keyword">new</span> Schema({ any: Schema.Types.Mixed });</code></pre><p>Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the <code>.markModified(path)</code> method of the document passing the path to the Mixed type you just changed.</p><pre><code class="javascript">person.anything = { x: [<span class="number">3</span>, <span class="number">4</span>, { y: <span class="string">"changed"</span> }] };
person.markModified(<span class="string">'anything'</span>);
person.save(); <span class="comment">// anything will now get saved</span></code></pre><h4 id="objectids">ObjectIds</h4><p>To specify a type of ObjectId, use <code>Schema.Types.ObjectId</code> in your declaration.</p><pre><code class="javascript"><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
<span class="keyword">var</span> ObjectId = mongoose.Schema.Types.ObjectId;
<span class="keyword">var</span> Car = <span class="keyword">new</span> Schema({ driver: ObjectId })
<span class="comment">// or just Schema.ObjectId for backwards compatibility with v2</span></code></pre><h4 id="arrays">Arrays</h4><p>Provide creation of arrays of <a href="./api.html#schema_Schema-Types">SchemaTypes</a> or <a href="./subdocs.html">Sub-Documents</a>.</p><pre><code class="javascript"><span class="keyword">var</span> ToySchema = <span class="keyword">new</span> Schema({ name: String });
<span class="keyword">var</span> ToyBox = <span class="keyword">new</span> Schema({
toys: [ToySchema],
buffers: [Buffer],
string: [String],
numbers: [Number]
<span class="comment">// ... etc</span>
});</code></pre><p>Note: specifying an empty array is equivalent to <code>Mixed</code>. The following all create arrays of <code>Mixed</code>:</p><pre><code class="javascript"><span class="keyword">var</span> Empty1 = <span class="keyword">new</span> Schema({ any: [] });
<span class="keyword">var</span> Empty2 = <span class="keyword">new</span> Schema({ any: Array });
<span class="keyword">var</span> Empty3 = <span class="keyword">new</span> Schema({ any: [Schema.Types.Mixed] });
<span class="keyword">var</span> Empty4 = <span class="keyword">new</span> Schema({ any: [{}] });</code></pre><h3 id="customtypes">Creating Custom Types</h3><p>Mongoose can also be extended with custom SchemaTypes. Search the <a href="http://plugins.mongoosejs.com">plugins</a> site for compatible types like <a href="https://github.com/aheckmann/mongoose-long">mongoose-long</a> and <a href="https://github.com/aheckmann/mongoose-number">other</a> <a href="https://github.com/bnoguchi/mongoose-types">types</a>.</p><h3 id="next">Next Up</h3><p>Now that we&#39;ve covered <code>SchemaTypes</code>, let&#39;s take a look at <a href="/docs/models.html">Models</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>
Jump to Line
Something went wrong with that request. Please try again.