Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
6352 lines (5332 sloc) 663 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 API v3.0.2</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"><style>body {
background: #d8e2d8 url(/docs/images/square_bg.png) fixed;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
color: #333;
-webkit-font-smoothing: antialiased;
-webkit-text-size-adjust: 100%;
padding: 0;
margin: 0;
font-size: 14px;
line-height: 22px;
}
a {
color: #800;
-webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;
-webkit-transition-duration: 0.15s;
-webkit-transition-timing-function: ease-out;
}
a:hover {
opacity: 0.8;
}
#wrap {
}
h1 {
font-family: 'Helvetica Nueue', Helvetica, Arial, FreeSans, sans-serif;
text-rendering: geometricPrecision;
}
pre {
background: rgba(255,255,255,.8);
border: 1px solid #bbb;
padding:5px;
border-radius: 3px;
box-shadow: 1px 3px 6px #ddd;
}
code {
background: rgba(255,255,255,.8);
color: #333;
border-radius: 3px;
font-size: 13px;
font-family: Monaco;
}
pre code {
border: 0 none;
padding: 1.2em;
overflow-x: auto;
}
h3 {
padding: 20px 15px;
margin-left: -15px;
}
h3 code {
font-weight: normal;
}
hr {
height: 1px;
border: 0 none;
padding: 0;
margin: 60px 0;
background: -webkit-gradient(linear, left top, right top, from(rgba(57, 172, 57, 0.0)), color-stop(0.5, rgba(57, 172, 57, 0.33)), to(rgba(57, 172, 57, 0.0)))
}
.doclinks hr {
margin: 10px 0;
}
li {
list-style: square;
}
#header {
padding-top: 22px;
padding-bottom: 25px;
text-transform: lowercase;
}
#header h1 {
margin-top: 0;
margin-bottom: 0;
}
#header h1 a {
text-decoration: none;
}
#header .mongoose {
font-size: 50px;
font-weight: 100;
color: #fff;
text-shadow: 6px 4px 5px #777, -2px 0px 2px white;
letter-spacing: -7px;
}
#links {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 210px;
overflow-x: hidden;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
padding: 15px 0 30px 20px;
border-right: 1px solid #ddd;
background: -webkit-gradient(linear, left top, right top, from(transparent), color-stop(0.92, transparent), color-stop(0.9201, rgba(172,172,172, 0.0)), to(rgba(172,172,172, 0.4))), transparent;
}
#links hr {
margin: 32px 0;
}
#content {
padding: 0;
margin: 0 0 30px 230px;
overflow-x: hidden;
}
#content .controls {
padding: 5px 15px 5px 10px;
position: fixed;
background: #fff;
border: 3px solid #eee;
border-radius: 0 0 12px 0;
border-width: 0 3px 3px 10px;
width: 100%;
bottom: 0;
opacity: 0.75;
-webkit-transition-property: opacity;
-webkit-transition-duration: 0.15s;
-webkit-transition-timing-function: ease-out;
}
#content .controls:hover {
opacity: .9;
}
#content p {
word-wrap: break-word;
}
#content > ul {
margin: 0;
padding: 0;
}
.private {
display: none;
}
.section {
font-size: 15px;
}
.doclinks li.private a:before,
.doclinks .module.private a:before,
.doclinks item.private a:before {
content: "p";
background: #333;
color: #fff;
font-size: 11px;
line-height: 15px;
font-weight: normal;
padding: 0 2px;
border-radius: 3px;
border: 1px solid #333;
display: inline-block;
margin-right: 5px;
}
#content .private h3:after {
content: "private";
background: #333;
color: #fff;
font-size: 11px;
line-height: 15px;
font-weight: normal;
padding: 0 2px;
border-radius: 3px;
border: 1px solid #333;
display: inline-block;
margin-left: 5px;
}
.module {
list-style: none;
padding: 30px 0 30px 30px;
border-color: #eee;
border-width: 9px 10px;
border-style: solid;
background-color: #fff;
}
.module > * {
max-width: 700px;
}
.item:last-child {
margin-bottom: 90px;
}
.item:last-child > hr {
display: none;
}
.item h3 a {
color: #333;
text-decoration: none;
}
.property h3 span {
color: #444;
}
.description {
margin-top: 25px;
}
.sourcecode {
display: none;
}
.showcode {
font-size: 12px;
cursor: pointer;
display: none;
}
.load .showcode {
display: block;
}
.types a {
text-decoration: none;
}
@media only screen and (device-width: 768px) {
}
@media only screen and (max-width: 480px) {
#forkbanner { display: none }
#header .mongoose {
font-size: 65px;
text-align: center;
}
#links {
position: static;
width: auto;
border: 0 none;
border-right: 0 none;
border-bottom: 1px solid #ddd;
background: -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0.92, transparent), color-stop(0.9201, rgba(172,172,172, 0.0)), to(rgba(172,172,172, 0.4))), transparent;
padding: 15px 0;
}
#links hr {
display: none;
}
#links, #links ul, #links li { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
#links ul { padding: 0 10px 0 0; }
#links li {
list-style: none;
display: inline-block;
width: 25%;
text-align: center;
}
#links .home, #links .support, #links .fork {
display: none;
}
.doclinks {
display: none;
}
#content { margin-left: 0; }
.module {
padding-left: 5px;
border-width: 3px;
}
.controls { display: none; }
}</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><hr><div class="doclinks"><div class="file "><a href="#index-js" class="section">index.js</a><ul><li class=""><a href="#index_Mongoose">Mongoose</a></li><li class=""><a href="#index_Mongoose-set">set</a></li><li class=""><a href="#index_Mongoose-get">get</a></li><li class=""><a href="#index_Mongoose-createConnection">createConnection</a></li><li class=""><a href="#index_Mongoose-connect">connect</a></li><li class=""><a href="#index_Mongoose-disconnect">disconnect</a></li><li class=""><a href="#index_Mongoose-model">model</a></li><li class=""><a href="#index_Mongoose-plugin">plugin</a></li><li class=""><a href="#index_module-exports">exports</a></li><li class=""><a href="#index_mongoose-Collection">Collection</a></li><li class=""><a href="#index_mongoose-Connection">Connection</a></li><li class=""><a href="#index_mongoose-version">version</a></li><li class=""><a href="#index_mongoose-Mongoose">Mongoose</a></li><li class=""><a href="#index_mongoose-Schema">Schema</a></li><li class=""><a href="#index_mongoose-SchemaType">SchemaType</a></li><li class=""><a href="#index_mongoose-SchemaTypes">SchemaTypes</a></li><li class=""><a href="#index_mongoose-VirtualType">VirtualType</a></li><li class=""><a href="#index_mongoose-Types">Types</a></li><li class=""><a href="#index_mongoose-Query">Query</a></li><li class=""><a href="#index_mongoose-Promise">Promise</a></li><li class=""><a href="#index_mongoose-Model">Model</a></li><li class=""><a href="#index_mongoose-Document">Document</a></li><li class=""><a href="#index_mongoose-Error">Error</a></li><li class=""><a href="#index_mongoose-mongo">mongo</a></li><li class=""><a href="#index_Mongoose-connection">connection</a></li></ul></div><div class="file "><a href="#collection-js" class="section">collection.js</a><ul><li class=""><a href="#collection_Collection">Collection</a></li><li class="private"><a href="#collection_Collection-onOpen">onOpen</a></li><li class="private"><a href="#collection_Collection-onClose">onClose</a></li><li class="private"><a href="#collection_Collection-addQueue">addQueue</a></li><li class="private"><a href="#collection_Collection-doQueue">doQueue</a></li><li class=""><a href="#collection_Collection-ensureIndex">ensureIndex</a></li><li class=""><a href="#collection_Collection-findAndModify">findAndModify</a></li><li class=""><a href="#collection_Collection-findOne">findOne</a></li><li class=""><a href="#collection_Collection-find">find</a></li><li class=""><a href="#collection_Collection-insert">insert</a></li><li class=""><a href="#collection_Collection-save">save</a></li><li class=""><a href="#collection_Collection-update">update</a></li><li class=""><a href="#collection_Collection-getIndexes">getIndexes</a></li><li class=""><a href="#collection_Collection-mapReduce">mapReduce</a></li><li class=""><a href="#collection_Collection-conn">conn</a></li><li class=""><a href="#collection_Collection-name">name</a></li></ul></div><div class="file "><a href="#connection-js" class="section">connection.js</a><ul><li class=""><a href="#connection_Connection">Connection</a></li><li class=""><a href="#connection_Connection-open">open</a></li><li class=""><a href="#connection_Connection-openSet">openSet</a></li><li class="private"><a href="#connection_Connection-error">error</a></li><li class="private"><a href="#connection_Connection-_open">_open</a></li><li class="private"><a href="#connection_Connection-onOpen">onOpen</a></li><li class=""><a href="#connection_Connection-close">close</a></li><li class="private"><a href="#connection_Connection-onClose">onClose</a></li><li class=""><a href="#connection_Connection-collection">collection</a></li><li class=""><a href="#connection_Connection-model">model</a></li><li class=""><a href="#connection_Connection-setProfiling">setProfiling</a></li><li class="private"><a href="#connection_Connection-defaultOptions">defaultOptions</a></li><li class=""><a href="#connection_Connection-db">db</a></li><li class=""><a href="#connection_Connection-collections">collections</a></li><li class=""><a href="#connection_Connection-readyState">readyState</a></li></ul></div><div class="file "><a href="#document-js" class="section">document.js</a><ul><li class="private"><a href="#document_Document">Document</a></li><li class="private"><a href="#document_Document-_buildDoc">_buildDoc</a></li><li class="private"><a href="#document_Document-init">init</a></li><li class="private"><a href="#document_Document-_storeShard">_storeShard</a></li><li class=""><a href="#document_Document-update">update</a></li><li class=""><a href="#document_Document-set">set</a></li><li class="private"><a href="#document_Document-_set">_set</a></li><li class="private"><a href="#document_Document-getValue">getValue</a></li><li class="private"><a href="#document_Document-setValue">setValue</a></li><li class=""><a href="#document_Document-get">get</a></li><li class="private"><a href="#document_Document-_path">_path</a></li><li class=""><a href="#document_Document-markModified">markModified</a></li><li class="private"><a href="#document_Document-try">try</a></li><li class=""><a href="#document_Document-modifiedPaths">modifiedPaths</a></li><li class=""><a href="#document_Document-isModified">isModified</a></li><li class=""><a href="#document_Document-isDirectModified">isDirectModified</a></li><li class=""><a href="#document_Document-isInit">isInit</a></li><li class=""><a href="#document_Document-isSelected">isSelected</a></li><li class=""><a href="#document_Document-validate">validate</a></li><li class=""><a href="#document_Document-invalidate">invalidate</a></li><li class="private"><a href="#document_Document-_reset">_reset</a></li><li class="private"><a href="#document_Document-_dirty">_dirty</a></li><li class="private"><a href="#document_Document-_setSchema">_setSchema</a></li><li class="private"><a href="#document_Document-_registerHooks">_registerHooks</a></li><li class="private"><a href="#document_Document-_error">_error</a></li><li class="private"><a href="#document_Document-_doQueue">_doQueue</a></li><li class=""><a href="#document_Document-toObject">toObject</a></li><li class=""><a href="#document_Document-toJSON">toJSON</a></li><li class=""><a href="#document_Document-inspect">inspect</a></li><li class=""><a href="#document_Document-toString">toString</a></li><li class=""><a href="#document_Document-equals">equals</a></li><li class=""><a href="#document_Document-errors">errors</a></li><li class=""><a href="#document_Document-isNew">isNew</a></li><li class=""><a href="#document_Document-schema">schema</a></li></ul></div><div class="file "><a href="#drivers-node-mongodb-native-collection-js" class="section">drivers/node-mongodb-native/collection.js</a><ul><li class="private"><a href="#drivers_node-mongodb-native_collection_NativeCollection">NativeCollection</a></li><li class="private"><a href="#drivers_node-mongodb-native_collection_NativeCollection-onOpen">onOpen</a></li><li class="private"><a href="#drivers_node-mongodb-native_collection_NativeCollection-onClose">onClose</a></li><li class=""><a href="#drivers_node-mongodb-native_collection_NativeCollection-getIndexes">getIndexes</a></li></ul></div><div class="file private"><a href="#drivers-node-mongodb-native-connection-js" class="section">drivers/node-mongodb-native/connection.js</a><ul><li class="private"><a href="#drivers_node-mongodb-native_connection_NativeConnection">NativeConnection</a></li><li class="private"><a href="#drivers_node-mongodb-native_connection_NativeConnection-doOpen">doOpen</a></li><li class="private"><a href="#drivers_node-mongodb-native_connection_NativeConnection-doOpenSet">doOpenSet</a></li><li class="private"><a href="#drivers_node-mongodb-native_connection_NativeConnection-doClose">doClose</a></li></ul></div><div class="file private"><a href="#error-js" class="section">error.js</a><ul><li class="private"><a href="#error_MongooseError">MongooseError</a></li></ul></div><div class="file private"><a href="#errors-cast-js" class="section">errors/cast.js</a><ul><li class="private"><a href="#errors_cast_CastError">CastError</a></li></ul></div><div class="file private"><a href="#errors-document-js" class="section">errors/document.js</a><ul><li class="private"><a href="#errors_document_DocumentError">DocumentError</a></li></ul></div><div class="file "><a href="#errors-validation-js" class="section">errors/validation.js</a><ul><li class="private"><a href="#errors_validation_ValidationError">ValidationError</a></li><li class=""><a href="#errors_validation_ValidationError-toString">toString</a></li></ul></div><div class="file private"><a href="#errors-validator-js" class="section">errors/validator.js</a><ul><li class="private"><a href="#errors_validator_ValidatorError">ValidatorError</a></li></ul></div><div class="file "><a href="#model-js" class="section">model.js</a><ul><li class=""><a href="#model_Model">Model</a></li><li class="private"><a href="#model_Model-_getPopulationKeys">_getPopulationKeys</a></li><li class="private"><a href="#model_Model-_populate">_populate</a></li><li class="private"><a href="#model_Model-init">init</a></li><li class=""><a href="#model_Model-save">save</a></li><li class="private"><a href="#model_Model-_delta">_delta</a></li><li class="private"><a href="#model_Model-_version">_version</a></li><li class=""><a href="#model_Model-increment">increment</a></li><li class="private"><a href="#model_Model-_where">_where</a></li><li class=""><a href="#model_Model-remove">remove</a></li><li class="private"><a href="#model_Model-_registerHooks">_registerHooks</a></li><li class=""><a href="#model_Model-model">model</a></li><li class=""><a href="#model_Model-%24where">$where</a></li><li class="private"><a href="#model_Model-init">init</a></li><li class=""><a href="#model_Model-ensureIndexes">ensureIndexes</a></li><li class=""><a href="#model_Model-remove">remove</a></li><li class=""><a href="#model_Model-find">find</a></li><li class="private"><a href="#model_Model-_applyNamedScope">_applyNamedScope</a></li><li class=""><a href="#model_Model-findById">findById</a></li><li class=""><a href="#model_Model-findOne">findOne</a></li><li class=""><a href="#model_Model-count">count</a></li><li class=""><a href="#model_Model-distinct">distinct</a></li><li class=""><a href="#model_Model-where">where</a></li><li class=""><a href="#model_Model-findOneAndUpdate">findOneAndUpdate</a></li><li class=""><a href="#model_Model-findByIdAndUpdate">findByIdAndUpdate</a></li><li class=""><a href="#model_Model-findOneAndRemove">findOneAndRemove</a></li><li class=""><a href="#model_Model-findByIdAndRemove">findByIdAndRemove</a></li><li class=""><a href="#model_Model-create">create</a></li><li class=""><a href="#model_Model-update">update</a></li><li class=""><a href="#model_Model-mapReduce">mapReduce</a></li><li class=""><a href="#model_Model-base">base</a></li><li class=""><a href="#model_Model-collection">collection</a></li><li class=""><a href="#model_Model-db">db</a></li><li class=""><a href="#model_Model-schema">schema</a></li><li class=""><a href="#model_Model-modelName">modelName</a></li><li class=""><a href="#model_Model-collection">collection</a></li><li class=""><a href="#model_Model-db">db</a></li></ul></div><div class="file private"><a href="#namedscope-js" class="section">namedscope.js</a><ul><li class="private"><a href="#namedscope_NamedScope-decorate">decorate</a></li></ul></div><div class="file "><a href="#promise-js" class="section">promise.js</a><ul><li class=""><a href="#promise_Promise">Promise</a></li><li class=""><a href="#promise_Promise-on">on</a></li><li class="private"><a href="#promise_Promise-emit">emit</a></li><li class=""><a href="#promise_Promise-complete">complete</a></li><li class=""><a href="#promise_Promise-error">error</a></li><li class=""><a href="#promise_Promise-addCallback">addCallback</a></li><li class=""><a href="#promise_Promise-addErrback">addErrback</a></li><li class=""><a href="#promise_Promise-addBack">addBack</a></li><li class=""><a href="#promise_Promise-resolve">resolve</a></li></ul></div><div class="file "><a href="#query-js" class="section">query.js</a><ul><li class=""><a href="#query_Query">Query</a></li><li class=""><a href="#query_Query-setOptions">setOptions</a></li><li class="private"><a href="#query_Query-bind">bind</a></li><li class=""><a href="#query_Query-exec">exec</a></li><li class=""><a href="#query_Query-find">find</a></li><li class=""><a href="#query_Query-cast">cast</a></li><li class="private"><a href="#query_Query-_optionsForExec">_optionsForExec</a></li><li class="private"><a href="#query_Query-_applyPaths">_applyPaths</a></li><li class=""><a href="#query_Query-%24where">$where</a></li><li class=""><a href="#query_Query-where">where</a></li><li class=""><a href="#query_Query-equals">equals</a></li><li class=""><a href="#query_Query-or">or</a></li><li class=""><a href="#query_Query-nor">nor</a></li><li class=""><a href="#query_Query-gt">gt</a></li><li class=""><a href="#query_Query-gte">gte</a></li><li class=""><a href="#query_Query-lt">lt</a></li><li class=""><a href="#query_Query-lte">lte</a></li><li class=""><a href="#query_Query-ne">ne</a></li><li class=""><a href="#query_Query-in">in</a></li><li class=""><a href="#query_Query-nin">nin</a></li><li class=""><a href="#query_Query-all">all</a></li><li class=""><a href="#query_Query-size">size</a></li><li class=""><a href="#query_Query-regex">regex</a></li><li class=""><a href="#query_Query-maxDistance">maxDistance</a></li><li class=""><a href="#query_Query-near">near</a></li><li class=""><a href="#query_Query-nearSphere">nearSphere</a></li><li class=""><a href="#query_Query-mod">mod</a></li><li class=""><a href="#query_Query-exists">exists</a></li><li class=""><a href="#query_Query-elemMatch">elemMatch</a></li><li class=""><a href="#query_Query-box">box</a></li><li class=""><a href="#query_Query-center">center</a></li><li class=""><a href="#query_Query-centerSphere">centerSphere</a></li><li class=""><a href="#query_Query-polygon">polygon</a></li><li class=""><a href="#query_Query-select">select</a></li><li class=""><a href="#query_Query-slice">slice</a></li><li class=""><a href="#query_Query-sort">sort</a></li><li class=""><a href="#query_Query-limit">limit</a></li><li class=""><a href="#query_Query-skip">skip</a></li><li class=""><a href="#query_Query-maxscan">maxscan</a></li><li class=""><a href="#query_Query-batchSize">batchSize</a></li><li class=""><a href="#query_Query-comment">comment</a></li><li class=""><a href="#query_Query-snapshot">snapshot</a></li><li class=""><a href="#query_Query-hint">hint</a></li><li class=""><a href="#query_Query-slaveOk">slaveOk</a></li><li class=""><a href="#query_Query-lean">lean</a></li><li class=""><a href="#query_Query-tailable">tailable</a></li><li class="private"><a href="#query_Query-execFind">execFind</a></li><li class=""><a href="#query_Query-findOne">findOne</a></li><li class=""><a href="#query_Query-count">count</a></li><li class=""><a href="#query_Query-distinct">distinct</a></li><li class=""><a href="#query_Query-update">update</a></li><li class="private"><a href="#query_Query-_castUpdate">_castUpdate</a></li><li class="private"><a href="#query_Query-_walkUpdatePath">_walkUpdatePath</a></li><li class="private"><a href="#query_Query-_castUpdateVal">_castUpdateVal</a></li><li class="private"><a href="#query_Query-_getSchema">_getSchema</a></li><li class=""><a href="#query_Query-remove">remove</a></li><li class=""><a href="#query_Query-findOneAndUpdate">findOneAndUpdate</a></li><li class=""><a href="#query_Query-findOneAndRemove">findOneAndRemove</a></li><li class="private"><a href="#query_Query-_findAndModify">_findAndModify</a></li><li class=""><a href="#query_Query-populate">populate</a></li><li class=""><a href="#query_Query-stream">stream</a></li><li class=""><a href="#query_Query-within">within</a></li></ul></div><div class="file "><a href="#querystream-js" class="section">querystream.js</a><ul><li class=""><a href="#querystream_QueryStream">QueryStream</a></li><li class="private"><a href="#querystream_QueryStream-_init">_init</a></li><li class="private"><a href="#querystream_QueryStream-_next">_next</a></li><li class="private"><a href="#querystream_QueryStream-__next">__next</a></li><li class="private"><a href="#querystream_QueryStream-_onNextObject">_onNextObject</a></li><li class=""><a href="#querystream_QueryStream-pause">pause</a></li><li class=""><a href="#querystream_QueryStream-resume">resume</a></li><li class=""><a href="#querystream_QueryStream-destroy">destroy</a></li><li class=""><a href="#querystream_QueryStream-pipe">pipe</a></li><li class=""><a href="#querystream_QueryStream-paused">paused</a></li><li class=""><a href="#querystream_QueryStream-readable">readable</a></li></ul></div><div class="file private"><a href="#schema-array-js" class="section">schema/array.js</a><ul><li class="private"><a href="#schema_array_SchemaArray">SchemaArray</a></li><li class="private"><a href="#schema_array_SchemaArray-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_array_SchemaArray-applyGetters">applyGetters</a></li><li class="private"><a href="#schema_array_SchemaArray-cast">cast</a></li><li class="private"><a href="#schema_array_SchemaArray-castForQuery">castForQuery</a></li></ul></div><div class="file private"><a href="#schema-boolean-js" class="section">schema/boolean.js</a><ul><li class="private"><a href="#schema_boolean_SchemaBoolean">SchemaBoolean</a></li><li class="private"><a href="#schema_boolean_SchemaBoolean-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_boolean_SchemaBoolean-cast">cast</a></li><li class="private"><a href="#schema_boolean_SchemaBoolean-castForQuery">castForQuery</a></li></ul></div><div class="file private"><a href="#schema-buffer-js" class="section">schema/buffer.js</a><ul><li class="private"><a href="#schema_buffer_SchemaBuffer">SchemaBuffer</a></li><li class="private"><a href="#schema_buffer_SchemaBuffer-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_buffer_SchemaBuffer-cast">cast</a></li><li class="private"><a href="#schema_buffer_SchemaBuffer-castForQuery">castForQuery</a></li></ul></div><div class="file private"><a href="#schema-date-js" class="section">schema/date.js</a><ul><li class="private"><a href="#schema_date_SchemaDate">SchemaDate</a></li><li class="private"><a href="#schema_date_SchemaDate-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_date_SchemaDate-cast">cast</a></li><li class="private"><a href="#schema_date_SchemaDate-castForQuery">castForQuery</a></li></ul></div><div class="file private"><a href="#schema-documentarray-js" class="section">schema/documentarray.js</a><ul><li class="private"><a href="#schema_documentarray_DocumentArray">DocumentArray</a></li><li class="private"><a href="#schema_documentarray_DocumentArray-doValidate">doValidate</a></li><li class="private"><a href="#schema_documentarray_DocumentArray-cast">cast</a></li></ul></div><div class="file private"><a href="#schema-mixed-js" class="section">schema/mixed.js</a><ul><li class="private"><a href="#schema_mixed_Mixed">Mixed</a></li><li class="private"><a href="#schema_mixed_Mixed-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_mixed_Mixed-cast">cast</a></li><li class="private"><a href="#schema_mixed_Mixed-castForQuery">castForQuery</a></li></ul></div><div class="file "><a href="#schema-number-js" class="section">schema/number.js</a><ul><li class="private"><a href="#schema_number_SchemaNumber">SchemaNumber</a></li><li class="private"><a href="#schema_number_SchemaNumber-checkRequired">checkRequired</a></li><li class=""><a href="#schema_number_SchemaNumber-min">min</a></li><li class=""><a href="#schema_number_SchemaNumber-max">max</a></li><li class="private"><a href="#schema_number_SchemaNumber-cast">cast</a></li><li class="private"><a href="#schema_number_SchemaNumber-castForQuery">castForQuery</a></li></ul></div><div class="file private"><a href="#schema-objectid-js" class="section">schema/objectid.js</a><ul><li class="private"><a href="#schema_objectid_ObjectId">ObjectId</a></li><li class="private"><a href="#schema_objectid_ObjectId-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_objectid_ObjectId-cast">cast</a></li><li class="private"><a href="#schema_objectid_ObjectId-castForQuery">castForQuery</a></li><li class="private"><a href="#schema_objectid_ObjectId-auto">auto</a></li></ul></div><div class="file "><a href="#schema-string-js" class="section">schema/string.js</a><ul><li class="private"><a href="#schema_string_SchemaString">SchemaString</a></li><li class=""><a href="#schema_string_SchemaString-enum">enum</a></li><li class=""><a href="#schema_string_SchemaString-lowercase">lowercase</a></li><li class=""><a href="#schema_string_SchemaString-uppercase">uppercase</a></li><li class=""><a href="#schema_string_SchemaString-trim">trim</a></li><li class=""><a href="#schema_string_SchemaString-match">match</a></li><li class="private"><a href="#schema_string_SchemaString-checkRequired">checkRequired</a></li><li class="private"><a href="#schema_string_SchemaString-cast">cast</a></li><li class="private"><a href="#schema_string_SchemaString-castForQuery">castForQuery</a></li></ul></div><div class="file "><a href="#schema-js" class="section">schema.js</a><ul><li class=""><a href="#schema_Schema">Schema</a></li><li class=""><a href="#schema_Schema-add">add</a></li><li class=""><a href="#schema_Schema-path">path</a></li><li class=""><a href="#schema_Schema-eachPath">eachPath</a></li><li class=""><a href="#schema_Schema-requiredPaths">requiredPaths</a></li><li class=""><a href="#schema_Schema-pathType">pathType</a></li><li class="private"><a href="#schema_Schema-queue">queue</a></li><li class=""><a href="#schema_Schema-pre">pre</a></li><li class=""><a href="#schema_Schema-post">post</a></li><li class=""><a href="#schema_Schema-plugin">plugin</a></li><li class=""><a href="#schema_Schema-method">method</a></li><li class=""><a href="#schema_Schema-static">static</a></li><li class=""><a href="#schema_Schema-index">index</a></li><li class=""><a href="#schema_Schema-set">set</a></li><li class=""><a href="#schema_Schema-indexes">indexes</a></li><li class=""><a href="#schema_Schema-virtual">virtual</a></li><li class=""><a href="#schema_Schema-virtualpath">virtualpath</a></li><li class="private"><a href="#schema_Schema-namedScope">namedScope</a></li><li class=""><a href="#schema_Schema-reserved">reserved</a></li><li class="private"><a href="#schema_Schema-interpretAsType">interpretAsType</a></li><li class=""><a href="#schema_Schema-Types">Types</a></li><li class="private"><a href="#schema_Schema-tree">tree</a></li><li class="private"><a href="#schema_Schema-paths">paths</a></li></ul></div><div class="file private"><a href="#schemadefault-js" class="section">schemadefault.js</a><ul><li class="private"><a href="#schemadefault_exports-system-profile">system.profile</a></li></ul></div><div class="file "><a href="#schematype-js" class="section">schematype.js</a><ul><li class=""><a href="#schematype_SchemaType">SchemaType</a></li><li class=""><a href="#schematype_SchemaType-default">default</a></li><li class=""><a href="#schematype_SchemaType-index">index</a></li><li class=""><a href="#schematype_SchemaType-unique">unique</a></li><li class=""><a href="#schematype_SchemaType-sparse">sparse</a></li><li class=""><a href="#schematype_SchemaType-expires">expires</a></li><li class=""><a href="#schematype_SchemaType-set">set</a></li><li class=""><a href="#schematype_SchemaType-get">get</a></li><li class=""><a href="#schematype_SchemaType-validate">validate</a></li><li class=""><a href="#schematype_SchemaType-required">required</a></li><li class="private"><a href="#schematype_SchemaType-getDefault">getDefault</a></li><li class="private"><a href="#schematype_SchemaType-applySetters">applySetters</a></li><li class="private"><a href="#schematype_SchemaType-applyGetters">applyGetters</a></li><li class=""><a href="#schematype_SchemaType-select">select</a></li><li class="private"><a href="#schematype_SchemaType-doValidate">doValidate</a></li><li class="private"><a href="#schematype_SchemaType-_isRef">_isRef</a></li></ul></div><div class="file "><a href="#types-array-js" class="section">types/array.js</a><ul><li class="private"><a href="#types_array_MongooseArray">MongooseArray</a></li><li class="private"><a href="#types_array_MongooseArray-_cast">_cast</a></li><li class="private"><a href="#types_array_MongooseArray-_markModified">_markModified</a></li><li class="private"><a href="#types_array_MongooseArray-_registerAtomic">_registerAtomic</a></li><li class="private"><a href="#types_array_MongooseArray-hasAtomics">hasAtomics</a></li><li class=""><a href="#types_array_MongooseArray-push">push</a></li><li class=""><a href="#types_array_MongooseArray-nonAtomicPush">nonAtomicPush</a></li><li class=""><a href="#types_array_MongooseArray-%24pop">$pop</a></li><li class=""><a href="#types_array_MongooseArray-pop">pop</a></li><li class=""><a href="#types_array_MongooseArray-%24shift">$shift</a></li><li class=""><a href="#types_array_MongooseArray-shift">shift</a></li><li class=""><a href="#types_array_MongooseArray-remove">remove</a></li><li class=""><a href="#types_array_MongooseArray-pull">pull</a></li><li class=""><a href="#types_array_MongooseArray-splice">splice</a></li><li class=""><a href="#types_array_MongooseArray-unshift">unshift</a></li><li class=""><a href="#types_array_MongooseArray-sort">sort</a></li><li class=""><a href="#types_array_MongooseArray-addToSet">addToSet</a></li><li class=""><a href="#types_array_MongooseArray-toObject">toObject</a></li><li class=""><a href="#types_array_MongooseArray-inspect">inspect</a></li><li class=""><a href="#types_array_MongooseArray-indexOf">indexOf</a></li><li class="private"><a href="#types_array_MongooseArray-_parent">_parent</a></li><li class="private"><a href="#types_array_MongooseArray-_atomics">_atomics</a></li></ul></div><div class="file "><a href="#types-buffer-js" class="section">types/buffer.js</a><ul><li class="private"><a href="#types_buffer_MongooseBuffer">MongooseBuffer</a></li><li class="private"><a href="#types_buffer_MongooseBuffer-_markModified">_markModified</a></li><li class=""><a href="#types_buffer_MongooseBuffer-write">write</a></li><li class=""><a href="#types_buffer_MongooseBuffer-copy">copy</a></li><li class=""><a href="#types_buffer_MongooseBuffer-toObject">toObject</a></li><li class="private"><a href="#types_buffer_MongooseBuffer-_parent">_parent</a></li></ul></div><div class="file "><a href="#types-documentarray-js" class="section">types/documentarray.js</a><ul><li class="private"><a href="#types_documentarray_MongooseDocumentArray">MongooseDocumentArray</a></li><li class="private"><a href="#types_documentarray_MongooseDocumentArray-_cast">_cast</a></li><li class=""><a href="#types_documentarray_MongooseDocumentArray-id">id</a></li><li class=""><a href="#types_documentarray_MongooseDocumentArray-toObject">toObject</a></li><li class=""><a href="#types_documentarray_MongooseDocumentArray-inspect">inspect</a></li><li class=""><a href="#types_documentarray_MongooseDocumentArray-create">create</a></li><li class="private"><a href="#types_documentarray_MongooseDocumentArray-notify">notify</a></li></ul></div><div class="file "><a href="#types-embedded-js" class="section">types/embedded.js</a><ul><li class="private"><a href="#types_embedded_EmbeddedDocument">EmbeddedDocument</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-markModified">markModified</a></li><li class="private"><a href="#types_embedded_EmbeddedDocument-save">save</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-remove">remove</a></li><li class="private"><a href="#types_embedded_EmbeddedDocument-update">update</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-inspect">inspect</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-invalidate">invalidate</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-ownerDocument">ownerDocument</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-parent">parent</a></li><li class=""><a href="#types_embedded_EmbeddedDocument-parentArray">parentArray</a></li></ul></div><div class="file "><a href="#types-objectid-js" class="section">types/objectid.js</a><ul><li class=""><a href="#types_objectid_ObjectId">ObjectId</a></li><li class="private"><a href="#types_objectid_ObjectId-fromString">fromString</a></li><li class="private"><a href="#types_objectid_ObjectId-toString">toString</a></li></ul></div><div class="file "><a href="#utils-js" class="section">utils.js</a><ul><li class="private"><a href="#utils_exports-toCollectionName">toCollectionName</a></li><li class=""><a href="#utils_exports-pluralization">pluralization</a></li><li class=""><a href="#utils_exports-uncountables">uncountables</a></li><li class="private"><a href="#utils_exports-deepEqual">deepEqual</a></li><li class="private"><a href="#utils_exports-clone">clone</a></li><li class="private"><a href="#utils_exports-options">options</a></li><li class="private"><a href="#utils_exports-random">random</a></li><li class="private"><a href="#utils_exports-merge">merge</a></li><li class="private"><a href="#utils_exports-args">args</a></li><li class="private"><a href="#utils_exports-tick">tick</a></li><li class="private"><a href="#utils_exports-isMongooseObject">isMongooseObject</a></li><li class="private"><a href="#utils_exports-expires">expires</a></li></ul></div><div class="file "><a href="#virtualtype-js" class="section">virtualtype.js</a><ul><li class=""><a href="#virtualtype_VirtualType">VirtualType</a></li><li class=""><a href="#virtualtype_VirtualType-get">get</a></li><li class=""><a href="#virtualtype_VirtualType-set">set</a></li><li class=""><a href="#virtualtype_VirtualType-applyGetters">applyGetters</a></li><li class=""><a href="#virtualtype_VirtualType-applySetters">applySetters</a></li></ul></div></div></div><div id="content"><div class="controls"><label><input type="checkbox">private</label></div><ul><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/index.js" id="index-js">index.js</a><div class="item method public"><h3 id="index_Mongoose"><a href="#index_Mongoose">Mongoose()</a></h3><p>Mongoose constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">Mongoose</span> <span class="params">()</span> {</span>
<span class="keyword">this</span>.connections = [];
<span class="keyword">this</span>.plugins = [];
<span class="keyword">this</span>.models = {};
<span class="keyword">this</span>.modelSchemas = {};
<span class="keyword">this</span>.options = {};
<span class="keyword">this</span>.createConnection(); <span class="comment">// default connection</span>
};</code></pre></div><div class="description"><p>The exports object of the <code>mongoose</code> module is an instance of this class.<br />Most apps will only use this one instance.</p></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-set"><a href="#index_Mongoose-set">Mongoose#set(<code>key</code>, <code>value</code>)</a></h3><p>Sets mongoose options</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Mongoose.prototype.set = <span class="function"><span class="keyword">function</span> <span class="params">(key, value)</span> {</span>
<span class="keyword">if</span> (arguments.length == <span class="number">1</span>)
<span class="keyword">return</span> <span class="keyword">this</span>.options[key];
<span class="keyword">this</span>.options[key] = value;
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>key</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li><li><code>value</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="description"><h4>Example:</h4>
<pre><code>mongoose.set(<span class="string">'test'</span>, value) <span class="comment">// sets the 'test' option to `value`</span></code></pre></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-get"><a href="#index_Mongoose-get">Mongoose#get(<code>key</code>)</a></h3><p>Gets mongoose options</p><div class="params"><h4>Parameters:</h4><ul><li><code>key</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="description"><h4>Example:</h4>
<pre><code>mongoose.get(<span class="string">'test'</span>) <span class="comment">// returns the 'test' value</span></code></pre></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-createConnection"><a href="#index_Mongoose-createConnection">Mongoose#createConnection(<code>[uri]</code>)</a></h3><p>Creates a Connection instance.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Mongoose.prototype.createConnection = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> conn = <span class="keyword">new</span> Connection(<span class="keyword">this</span>);
<span class="keyword">this</span>.connections.push(conn);
<span class="keyword">if</span> (arguments.length) {
<span class="keyword">if</span> (rgxReplSet.test(arguments[<span class="number">0</span>])) {
conn.openSet.apply(conn, arguments);
} <span class="keyword">else</span> {
conn.open.apply(conn, arguments);
}
}
<span class="keyword">return</span> conn;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>[uri]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>a mongodb:// URI</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span>the created Connection object</span></li></ul></div><div class="description"><h4>Example:</h4>
<pre><code><span class="comment">// with mongodb:// URI</span>
db = mongoose.createConnection(<span class="string">'mongodb://localhost:port/database'</span>);
<span class="comment">// replica sets</span>
db = mongoose.createConnection(<span class="string">'mongodb://localhost:port/database,mongodb://anotherhost:port,mongodb://yetanother:port'</span>);
<span class="comment">// with [host, database_name[, port] signature</span>
db = mongoose.createConnection(<span class="string">'localhost'</span>, <span class="string">'database'</span>, port)
<span class="comment">// initialize now, connect later</span>
db = mongoose.createConnection();
db.open(<span class="string">'localhost'</span>, <span class="string">'database'</span>, port);</code></pre></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-connect"><a href="#index_Mongoose-connect">Mongoose#connect()</a></h3><p>Opens the default mongoose connection.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Mongoose.prototype.connect = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> conn = <span class="keyword">this</span>.connection;
<span class="keyword">if</span> (rgxReplSet.test(arguments[<span class="number">0</span>])) {
conn.openSet.apply(conn, arguments);
} <span class="keyword">else</span> {
conn.open.apply(conn, arguments);
}
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#index_Mongoose">Mongoose</a>&gt; </span><span>this</span></li></ul></div><div class="see"><h4>See:</h4><ul class="see"><li><a href="Mongoose#createConnection" title="Mongoose#createConnection">Mongoose#createConnection</a></li></ul></div><div class="description"><p>If arguments are passed, they are proxied to either <a href="#Connection-open">Connection#open</a> or <a href="#Connection-openSet">Connection#openSet</a> appropriately.</p></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-disconnect"><a href="#index_Mongoose-disconnect">Mongoose#disconnect(<code>[fn]</code>)</a></h3><p>Disconnects all connections.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Mongoose.prototype.disconnect = <span class="function"><span class="keyword">function</span> <span class="params">(fn)</span> {</span>
<span class="keyword">var</span> count = <span class="keyword">this</span>.connections.length
, error
<span class="keyword">this</span>.connections.forEach(<span class="keyword">function</span>(conn){
conn.close(<span class="keyword">function</span>(err){
<span class="keyword">if</span> (error) <span class="keyword">return</span>;
<span class="keyword">if</span> (err) {
error = err;
<span class="keyword">if</span> (fn) <span class="keyword">return</span> fn(err);
<span class="keyword">throw</span> err;
}
<span class="keyword">if</span> (fn)
--count || fn();
});
});
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>[fn]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>called after all connection close.</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#index_Mongoose">Mongoose</a>&gt; </span><span>this</span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-model"><a href="#index_Mongoose-model">Mongoose#model(<code>name</code>, <code>[schema]</code>, <code>[collection]</code>, <code>[skipInit]</code>)</a></h3><p>Defines a model or retrieves it.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Mongoose.prototype.model = function (name, schema, collection, skipInit) {
// normalize collection
if (!(schema instanceof Schema)) {
collection = schema;
schema = false;
}
if ('boolean' === typeof collection) {
skipInit = collection;
collection = null;
}
// look up models for the collection
if (!this.modelSchemas[name]) {
if (!schema &amp;&amp; name in SchemaDefaults) {
schema = SchemaDefaults[name];
}
if (schema) {
this.modelSchemas[name] = schema;
for (var i = 0, l = this.plugins.length; i &lt; l; i++) {
schema.plugin(this.plugins[i][0], this.plugins[i][1]);
}
} else {
throw new Error('Schema hasn\'t been registered for model "' + name + '".
'
+ 'Use mongoose.model(name, schema)');
}
}
if (!this.models[name]) {
schema || (schema = this.modelSchemas[name]);
collection || (collection = schema.set('collection') || format(name));
var model = Model.compile(name
, this.modelSchemas[name]
, collection
, this.connection
, this);
if (!skipInit) model.init();
this.models[name] = model;
}
return this.models[name];
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>name</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>model name</span></li><li><code>[schema]</code><span class="types"> &lt;<a href="#schema_Schema">Schema</a>&gt; </span><span></span></li><li><code>[collection]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>name (optional, induced from model name)</span></li><li><code>[skipInit]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span>whether to skip initialization (defaults to false)</span></li></ul></div><div class="description"><p>Models defined on the <code>mongoose</code> instance are available to all connection created by the same <code>mongoose</code> instance.</p>
<h4>Example:</h4>
<pre><code><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
<span class="comment">// define an Actor model with this mongoose instance</span>
mongoose.model(<span class="string">'Actor'</span>, <span class="keyword">new</span> Schema({ name: String }));
<span class="comment">// create a new connection</span>
<span class="keyword">var</span> conn = mongoose.createConnection(..);
<span class="comment">// retrieve the Actor model</span>
<span class="keyword">var</span> Actor = conn.model(<span class="string">'Actor'</span>);</code></pre>
<p>When no <code>collection</code> argument is passed, Mongoose produces a collection name by passing the model <code>name</code> to the <a href="#utils.toCollectionName">utils.toCollectionName</a> method. This method pluralizes the name. Collection names can also be declared through schema options.</p>
<h4>Example:</h4>
<pre><code><span class="keyword">var</span> schema = <span class="keyword">new</span> Schema({ name: String });
schema.set(<span class="string">'collection'</span>, <span class="string">'actor'</span>);</code></pre></div><hr class=""></div><div class="item method public"><h3 id="index_Mongoose-plugin"><a href="#index_Mongoose-plugin">Mongoose#plugin(<code>fn</code>, <code>[opts]</code>)</a></h3><p>Declares a global plugin executed on all Schemas.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Mongoose.prototype.plugin = <span class="function"><span class="keyword">function</span> <span class="params">(fn, opts)</span> {</span>
<span class="keyword">this</span>.plugins.push([fn, opts]);
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>fn</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>plugin callback</span></li><li><code>[opts]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>optional options</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#index_Mongoose">Mongoose</a>&gt; </span><span>this</span></li></ul></div><div class="description"><p>Equivalent to calling <code>.plugin(fn)</code> on each Schema you create.</p></div><hr class=""></div><div class="item static public"><h3 id="index_module-exports"><a href="#index_module-exports">module.exports</a></h3><p>The exports object is an instance of Mongoose.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">module.exports = exports = <span class="keyword">new</span> Mongoose;
<span class="keyword">var</span> mongoose = module.exports;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Collection"><a href="#index_mongoose-Collection">mongoose.Collection</a></h3><p>The Mongoose Collection constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Collection = Collection;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Connection"><a href="#index_mongoose-Connection">mongoose.Connection</a></h3><p>The Mongoose Connection constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Connection = Connection;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-version"><a href="#index_mongoose-version">mongoose.version</a></h3><p>Mongoose version</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.version = JSON.parse(
require(<span class="string">'fs'</span>).readFileSync(__dirname + <span class="string">'/../package.json'</span>, <span class="string">'utf8'</span>)
).version;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Mongoose"><a href="#index_mongoose-Mongoose">mongoose.Mongoose</a></h3><p>The Mongoose constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Mongoose = Mongoose;</code></pre></div><p>The exports of the mongoose module is an instance of this class.</p>
<h4>Example:</h4>
<pre><code><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
<span class="keyword">var</span> mongoose2 = <span class="keyword">new</span> mongoose.Mongoose();</code></pre><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Schema"><a href="#index_mongoose-Schema">mongoose.Schema</a></h3><p>The Mongoose Schema constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Schema = Schema;</code></pre></div><h4>Example:</h4>
<pre><code><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
<span class="keyword">var</span> Schema = mongoose.Schema;
<span class="keyword">var</span> CatSchema = <span class="keyword">new</span> Schema(..);</code></pre><hr class=""></div><div class="item static public"><h3 id="index_mongoose-SchemaType"><a href="#index_mongoose-SchemaType">mongoose.SchemaType</a></h3><p>The Mongoose SchemaType constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.SchemaType = SchemaType;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-SchemaTypes"><a href="#index_mongoose-SchemaTypes">mongoose.SchemaTypes</a></h3><p>The various Mongoose SchemaTypes.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.SchemaTypes = Schema.Types;</code></pre></div><div class="see"><h4>See:</h4><ul class="see"><li><a href="#schema_Schema-Types" title="Schema.SchemaTypes">Schema.SchemaTypes</a></li></ul></div><h4>Note:</h4>
<p><em>Alias of mongoose.Schema.Types for backwards compatibility.</em></p><hr class=""></div><div class="item static public"><h3 id="index_mongoose-VirtualType"><a href="#index_mongoose-VirtualType">mongoose.VirtualType</a></h3><p>The Mongoose VirtualType constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.VirtualType = VirtualType;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Types"><a href="#index_mongoose-Types">mongoose.Types</a></h3><p>The various Mongoose Types.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Types = Types;</code></pre></div><h4>Example:</h4>
<pre><code><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
<span class="keyword">var</span> array = mongoose.Types.Array;</code></pre>
<h4>Types:</h4>
<ul>
<li>Array</li>
<li>Buffer</li>
<li>Document</li>
<li>Embedded</li>
<li>DocumentArray</li>
<li>ObjectId</li>
</ul>
<p>Using this exposed access to the <code>ObjectId</code> type, we can construct ids on demand.</p>
<pre><code><span class="keyword">var</span> ObjectId = mongoose.Types.ObjectId;
<span class="keyword">var</span> id1 = <span class="keyword">new</span> ObjectId;</code></pre><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Query"><a href="#index_mongoose-Query">mongoose.Query</a></h3><p>The Mongoose Query constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Query = Query;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Promise"><a href="#index_mongoose-Promise">mongoose.Promise</a></h3><p>The Mongoose Promise constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Promise = Promise;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Model"><a href="#index_mongoose-Model">mongoose.Model</a></h3><p>The Mongoose Model constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Model = Model;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Document"><a href="#index_mongoose-Document">mongoose.Document</a></h3><p>The Mongoose Document constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Document = Document;</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-Error"><a href="#index_mongoose-Error">mongoose.Error</a></h3><p>The MongooseError constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.Error = require(<span class="string">'./error'</span>);</code></pre></div><hr class=""></div><div class="item static public"><h3 id="index_mongoose-mongo"><a href="#index_mongoose-mongo">mongoose.mongo</a></h3><p>The node-mongodb-native driver Mongoose uses.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">mongoose.mongo = require(<span class="string">'mongodb'</span>);</code></pre></div><hr class=""></div><div class="item property public"><h3 id="index_Mongoose-connection"><a href="#index_Mongoose-connection">Mongoose#<span>connection</span></a></h3><p>The default connection of the mongoose module.</p>
<h4>Example:</h4>
<pre><code><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
mongoose.connect(...);
mongoose.connection.on(<span class="string">'error'</span>, cb);</code></pre>
<p>This is the connection used by default for every model created using <a href="#index_Mongoose-model">mongoose.model</a>.</p><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span></span></li></ul></div><hr class=""></div></li><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/collection.js" id="collection-js">collection.js</a><div class="item method public"><h3 id="collection_Collection"><a href="#collection_Collection">Collection(<code>name</code>, <code>conn</code>, <code>opts</code>)</a></h3><p>Abstract Collection constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">Collection</span> <span class="params">(name, conn, opts)</span> {</span>
<span class="keyword">this</span>.name = name;
<span class="keyword">this</span>.conn = conn;
<span class="keyword">this</span>.buffer = <span class="literal">true</span>;
<span class="keyword">this</span>.queue = [];
<span class="keyword">if</span> (<span class="string">'number'</span> == <span class="keyword">typeof</span> opts) opts = { size: opts };
<span class="keyword">this</span>.opts = opts || {};
<span class="keyword">if</span> (STATES.connected == <span class="keyword">this</span>.conn.readyState) {
<span class="keyword">this</span>.onOpen();
}
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>name</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>name of the collection</span></li><li><code>conn</code><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span>A MongooseConnection instance</span></li><li><code>opts</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>optional collection options</span></li></ul></div><div class="description"><p>This is the base class that drivers inherit from and implement.</p></div><hr class=""></div><div class="item method private"><h3 id="collection_Collection-onOpen"><a href="#collection_Collection-onOpen">Collection#onOpen()</a></h3><p>Called when the database connects</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.onOpen = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="keyword">this</span>.buffer = <span class="literal">false</span>;
self.doQueue();
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="collection_Collection-onClose"><a href="#collection_Collection-onClose">Collection#onClose()</a></h3><p>Called when the database disconnects</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.onClose = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">this</span>.buffer = <span class="literal">true</span>;
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="collection_Collection-addQueue"><a href="#collection_Collection-addQueue">Collection#addQueue(<code>name</code>, <code>args</code>)</a></h3><p>Queues a method for later execution when its<br />database connection opens.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.addQueue = <span class="function"><span class="keyword">function</span> <span class="params">(name, args)</span> {</span>
<span class="keyword">this</span>.queue.push([name, args]);
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>name</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>name of the method to queue</span></li><li><code>args</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array">Array</a>&gt; </span><span>arguments to pass to the method when executed</span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="collection_Collection-doQueue"><a href="#collection_Collection-doQueue">Collection#doQueue()</a></h3><p>Executes all queued methods and clears the queue.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.doQueue = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, l = <span class="keyword">this</span>.queue.length; i &lt; l; i++){
<span class="keyword">this</span>[<span class="keyword">this</span>.queue[i][<span class="number">0</span>]].apply(<span class="keyword">this</span>, <span class="keyword">this</span>.queue[i][<span class="number">1</span>]);
}
<span class="keyword">this</span>.queue = [];
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="collection_Collection-ensureIndex"><a href="#collection_Collection-ensureIndex">Collection#ensureIndex()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.ensureIndex = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#ensureIndex unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-findAndModify"><a href="#collection_Collection-findAndModify">Collection#findAndModify()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.findAndModify = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#findAndModify unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-findOne"><a href="#collection_Collection-findOne">Collection#findOne()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.findOne = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#findOne unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-find"><a href="#collection_Collection-find">Collection#find()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.find = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#find unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-insert"><a href="#collection_Collection-insert">Collection#insert()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.insert = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#insert unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-save"><a href="#collection_Collection-save">Collection#save()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.save = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#save unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-update"><a href="#collection_Collection-update">Collection#update()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.update = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#update unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-getIndexes"><a href="#collection_Collection-getIndexes">Collection#getIndexes()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.getIndexes = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#getIndexes unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="collection_Collection-mapReduce"><a href="#collection_Collection-mapReduce">Collection#mapReduce()</a></h3><p>Abstract method that drivers must implement.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Collection.prototype.mapReduce = <span class="keyword">function</span>(){
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">'Collection#mapReduce unimplemented by driver'</span>);
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item property public"><h3 id="collection_Collection-conn"><a href="#collection_Collection-conn">Collection#<span>conn</span></a></h3><p>The Connection instance</p><hr class=""></div><div class="item property public"><h3 id="collection_Collection-name"><a href="#collection_Collection-name">Collection#<span>name</span></a></h3><p>The collection name</p><hr class=""></div></li><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/connection.js" id="connection-js">connection.js</a><div class="item method public"><h3 id="connection_Connection"><a href="#connection_Connection">Connection(<code>base</code>)</a></h3><p>Connection constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">Connection</span> <span class="params">(base)</span> {</span>
<span class="keyword">this</span>.base = base;
<span class="keyword">this</span>.collections = {};
<span class="keyword">this</span>.models = {};
<span class="keyword">this</span>.replica = <span class="literal">false</span>;
<span class="keyword">this</span>.host = <span class="literal">null</span>;
<span class="keyword">this</span>.port = <span class="literal">null</span>;
<span class="keyword">this</span>.user = <span class="literal">null</span>;
<span class="keyword">this</span>.pass = <span class="literal">null</span>;
<span class="keyword">this</span>.name = <span class="literal">null</span>;
<span class="keyword">this</span>.options = <span class="literal">null</span>;
<span class="keyword">this</span>._readyState = STATES.disconnected;
<span class="keyword">this</span>._closeCalled = <span class="literal">false</span>;
<span class="keyword">this</span>._hasOpened = <span class="literal">false</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>base</code><span class="types"> &lt;<a href="#index_Mongoose">Mongoose</a>&gt; </span><span>a mongoose instance</span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="http://nodejs.org/api/events.html#events_class_events_eventemitter" title="NodeJS EventEmitter">NodeJS EventEmitter</a></li></ul></div><div class="events"><h4>Events:</h4><ul><li><p><code>connecting</code>: Emitted when <code>connection.{open,openSet}()</code> is executed on this connection.</p></li><li><p><code>connected</code>: Emitted when this connection successfully connects to the db. May be emitted <em>multiple</em> times in <code>reconnected</code> scenarios.</p></li><li><p><code>open</code>: Emitted after we <code>connected</code> and <code>onOpen</code> is executed on all of this connections models.</p></li><li><p><code>disconnecting</code>: Emitted when <code>connection.close()</code> was executed.</p></li><li><p><code>disconnected</code>: Emitted after getting disconnected from the db.</p></li><li><p><code>close</code>: Emitted after we <code>disconnected</code> and <code>onClose</code> executed on all of this connections models.</p></li><li><p><code>reconnected</code>: Emitted after we <code>connected</code> and subsequently <code>disconnected</code>, followed by successfully another successfull connection.</p></li><li><p><code>error</code>: Emitted when an error occurs on this connection.</p></li><li><p><code>fullsetup</code>: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.</p></li></ul></div><div class="description"><p>For practical reasons, a Connection equals a Db.</p></div><hr class=""></div><div class="item method public"><h3 id="connection_Connection-open"><a href="#connection_Connection-open">Connection#open(<code>connection_string</code>, <code>[database]</code>, <code>[port]</code>, <code>[options]</code>, <code>[callback]</code>)</a></h3><p>Opens the connection to MongoDB.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.open = <span class="function"><span class="keyword">function</span> <span class="params">(host, database, port, options, callback)</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>
, uri;
<span class="keyword">if</span> (<span class="string">'string'</span> === <span class="keyword">typeof</span> database) {
<span class="keyword">switch</span> (arguments.length) {
<span class="keyword">case</span> <span class="number">2</span>:
port = <span class="number">27017</span>;
<span class="keyword">case</span> <span class="number">3</span>:
<span class="keyword">switch</span> (<span class="keyword">typeof</span> port) {
<span class="keyword">case</span> <span class="string">'function'</span>:
callback = port, port = <span class="number">27017</span>;
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">'object'</span>:
options = port, port = <span class="number">27017</span>;
<span class="keyword">break</span>;
}
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">4</span>:
<span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> options)
callback = options, options = {};
}
} <span class="keyword">else</span> {
<span class="keyword">switch</span> (<span class="keyword">typeof</span> database) {
<span class="keyword">case</span> <span class="string">'function'</span>:
callback = database, database = <span class="literal">undefined</span>;
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">'object'</span>:
options = database;
database = <span class="literal">undefined</span>;
callback = port;
<span class="keyword">break</span>;
}
<span class="keyword">if</span> (!rgxProtocol.test(host)) {
host = <span class="string">'mongodb://'</span> + host;
}
uri = url.parse(host);
host = uri.hostname;
port = uri.port || <span class="number">27017</span>;
database = uri.pathname &amp;&amp; uri.pathname.replace(<span class="regexp">/\//g</span>, <span class="string">''</span>);
}
<span class="keyword">this</span>.options = <span class="keyword">this</span>.defaultOptions(options);
<span class="comment">// make sure we can open</span>
<span class="keyword">if</span> (STATES.disconnected !== <span class="keyword">this</span>.readyState) {
<span class="keyword">var</span> err = <span class="keyword">new</span> Error(<span class="string">'Trying to open unclosed connection.'</span>);
err.state = <span class="keyword">this</span>.readyState;
<span class="keyword">this</span>.error(err, callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">if</span> (!host) {
<span class="keyword">this</span>.error(<span class="keyword">new</span> Error(<span class="string">'Missing connection hostname.'</span>), callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">if</span> (!database) {
<span class="keyword">this</span>.error(<span class="keyword">new</span> Error(<span class="string">'Missing connection database.'</span>), callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="comment">// handle authentication</span>
<span class="keyword">if</span> (uri &amp;&amp; uri.auth) {
<span class="keyword">var</span> auth = uri.auth.split(<span class="string">':'</span>);
<span class="keyword">this</span>.user = auth[<span class="number">0</span>];
<span class="keyword">this</span>.pass = auth[<span class="number">1</span>];
<span class="comment">// Check hostname for user/pass</span>
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="regexp">/@/</span>.test(host) &amp;&amp; <span class="regexp">/:/</span>.test(host.split(<span class="string">'@'</span>)[<span class="number">0</span>])) {
host = host.split(<span class="string">'@'</span>);
<span class="keyword">var</span> auth = host.shift().split(<span class="string">':'</span>);
host = host.pop();
<span class="keyword">this</span>.user = auth[<span class="number">0</span>];
<span class="keyword">this</span>.pass = auth[<span class="number">1</span>];
<span class="comment">// user/pass options</span>
} <span class="keyword">else</span> <span class="keyword">if</span> (options &amp;&amp; options.user &amp;&amp; options.pass) {
<span class="keyword">this</span>.user = options.user;
<span class="keyword">this</span>.pass = options.pass;
} <span class="keyword">else</span> {
<span class="keyword">this</span>.user = <span class="keyword">this</span>.pass = <span class="literal">undefined</span>;
}
<span class="keyword">this</span>.name = database;
<span class="keyword">this</span>.host = host;
<span class="keyword">this</span>.port = port;
<span class="keyword">this</span>._open(callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>connection_string</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>mongodb://uri or the host to which you are connecting</span></li><li><code>[database]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>database name</span></li><li><code>[port]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a>&gt; </span><span>database port</span></li><li><code>[options]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>options</span></li><li><code>[callback]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="see"><h4>See:</h4><ul class="see"><li><a href="https://github.com/mongodb/node-mongodb-native" title="node-mongodb-native">node-mongodb-native</a></li></ul></div><div class="description"><p><code>options</code> is a hash with the following possible properties:</p>
<pre><code>db - passed to the connection db instance
server - passed to the connection server instance(s)
replset - passed to the connection ReplSetServer instance
user - username <span class="keyword">for</span> authentication
pass - password <span class="keyword">for</span> authentication</code></pre>
<h4>Notes:</h4>
<p>Mongoose forces the db option <code>forceServerObjectId</code> false and cannot be overridden.<br />Mongoose defaults the server <code>auto_reconnect</code> options to true which can be overridden.<br />See the node-mongodb-native driver instance for options that it understands.</p></div><hr class=""></div><div class="item method public"><h3 id="connection_Connection-openSet"><a href="#connection_Connection-openSet">Connection#openSet(<code>uris</code>, <code>[database]</code>, <code>[options]</code>, <code>[callback]</code>)</a></h3><p>Connects to a replica set.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.openSet = <span class="function"><span class="keyword">function</span> <span class="params">(uris, database, options, callback)</span> {</span>
<span class="keyword">var</span> uris = uris.split(<span class="string">','</span>)
, self = <span class="keyword">this</span>;
<span class="keyword">switch</span> (arguments.length) {
<span class="keyword">case</span> <span class="number">3</span>:
<span class="keyword">this</span>.name = database;
<span class="keyword">if</span> (<span class="string">'function'</span> === <span class="keyword">typeof</span> options) callback = options, options = {};
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">2</span>:
<span class="keyword">switch</span> (<span class="keyword">typeof</span> database) {
<span class="keyword">case</span> <span class="string">'string'</span>:
<span class="keyword">this</span>.name = database;
<span class="keyword">case</span> <span class="string">'function'</span>:
callback = database, database = <span class="literal">null</span>;
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">'object'</span>:
options = database, database = <span class="literal">null</span>;
<span class="keyword">break</span>;
}
}
<span class="keyword">this</span>.options = options = <span class="keyword">this</span>.defaultOptions(options);
<span class="keyword">if</span> (uris.length &lt; <span class="number">2</span>) {
<span class="keyword">this</span>.error(<span class="keyword">new</span> Error(<span class="string">'Please provide comma-separated URIs'</span>), callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">this</span>.replica = <span class="literal">true</span>;
<span class="keyword">this</span>.host = [];
<span class="keyword">this</span>.port = [];
uris.forEach(<span class="function"><span class="keyword">function</span> <span class="params">(uri)</span> {</span>
<span class="comment">// handle missing protocols</span>
<span class="keyword">if</span> (!rgxProtocol.test(uri))
uri = <span class="string">'mongodb://'</span> + uri;
<span class="keyword">var</span> uri = url.parse(uri);
self.host.push(uri.hostname);
self.port.push(uri.port || <span class="number">27017</span>);
<span class="keyword">if</span> (!self.name &amp;&amp; uri.pathname &amp;&amp; uri.pathname.replace(<span class="regexp">/\//g</span>, <span class="string">''</span>))
self.name = uri.pathname.replace(<span class="regexp">/\//g</span>, <span class="string">''</span>);
<span class="keyword">if</span> (!self.user &amp;&amp; uri.auth) {
<span class="keyword">var</span> auth = uri.auth.split(<span class="string">':'</span>);
self.user = auth[<span class="number">0</span>];
self.pass = auth[<span class="number">1</span>];
}
});
<span class="keyword">if</span> (!<span class="keyword">this</span>.name) {
<span class="keyword">this</span>.error(<span class="keyword">new</span> Error(<span class="string">'No database name provided for replica set'</span>), callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">this</span>._open(callback);
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>uris</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>comma-separated mongodb:// `URI`s</span></li><li><code>[database]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>database name if not included in `uris`</span></li><li><code>[options]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>passed to the internal driver</span></li><li><code>[callback]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="see"><h4>See:</h4><ul class="see"><li><a href="https://github.com/mongodb/node-mongodb-native" title="node-mongodb-native">node-mongodb-native</a></li></ul></div><div class="description"><h4>Example:</h4>
<pre><code><span class="keyword">var</span> db = mongoose.createConnection();
db.openSet(<span class="string">"mongodb://user:pwd@localhost:27020/testing,mongodb://example.com:27020,mongodb://localhost:27019"</span>);</code></pre>
<p>The database name and/or auth need only be included in one URI.<br />The <code>options</code> are passed to the internal driver connection object.</p></div><hr class=""></div><div class="item method private"><h3 id="connection_Connection-error"><a href="#connection_Connection-error">Connection#error(<code>err</code>, <code>callback</code>)</a></h3><p>error</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.error = <span class="function"><span class="keyword">function</span> <span class="params">(err, callback)</span> {</span>
<span class="keyword">if</span> (callback) <span class="keyword">return</span> callback(err);
<span class="keyword">this</span>.emit(<span class="string">'error'</span>, err);
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>err</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error">Error</a>&gt; </span><span></span></li><li><code>callback</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>optional</span></li></ul></div><div class="description"><p>Graceful error handling, passes error to callback<br />if available, else emits error on the connection.</p></div><hr class="private"></div><div class="item method private"><h3 id="connection_Connection-_open"><a href="#connection_Connection-_open">Connection#_open(<code>callback</code>)</a></h3><p>Handles opening the connection with the appropriate method based on connection type.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype._open = <span class="function"><span class="keyword">function</span> <span class="params">(callback)</span> {</span>
<span class="keyword">this</span>.readyState = STATES.connecting;
<span class="keyword">this</span>._closeCalled = <span class="literal">false</span>;
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="keyword">var</span> method = <span class="keyword">this</span>.replica
? <span class="string">'doOpenSet'</span>
: <span class="string">'doOpen'</span>;
<span class="comment">// open connection</span>
<span class="keyword">this</span>[method](<span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
<span class="keyword">if</span> (err) {
self.readyState = STATES.disconnected;
<span class="keyword">if</span> (self._hasOpened) {
<span class="keyword">if</span> (callback) callback(err);
} <span class="keyword">else</span> {
self.error(err, callback);
}
<span class="keyword">return</span>;
}
self.onOpen();
callback &amp;&amp; callback();
});
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>callback</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="connection_Connection-onOpen"><a href="#connection_Connection-onOpen">Connection#onOpen()</a></h3><p>Called when the connection is opened</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.onOpen = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="function"><span class="keyword">function</span> <span class="title">open</span> <span class="params">()</span> {</span>
self.readyState = STATES.connected;
<span class="comment">// avoid having the collection subscribe to our event emitter</span>
<span class="comment">// to prevent 0.3 warning</span>
<span class="keyword">for</span> (<span class="keyword">var</span> i <span class="keyword">in</span> self.collections)
self.collections[i].onOpen();
self.emit(<span class="string">'open'</span>);
};
<span class="comment">// re-authenticate</span>
<span class="keyword">if</span> (self.user &amp;&amp; self.pass)
self.db.authenticate(self.user, self.pass, open);
<span class="keyword">else</span>
open();
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="connection_Connection-close"><a href="#connection_Connection-close">Connection#close(<code>[callback]</code>)</a></h3><p>Closes the connection</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.close = <span class="function"><span class="keyword">function</span> <span class="params">(callback)</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="keyword">this</span>._closeCalled = <span class="literal">true</span>;
<span class="keyword">switch</span> (<span class="keyword">this</span>.readyState){
<span class="keyword">case</span> <span class="number">0</span>: <span class="comment">// disconnected</span>
callback &amp;&amp; callback();
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">1</span>: <span class="comment">// connected</span>
<span class="keyword">this</span>.readyState = STATES.disconnecting;
<span class="keyword">this</span>.doClose(<span class="keyword">function</span>(err){
<span class="keyword">if</span> (err){
self.error(err, callback);
} <span class="keyword">else</span> {
self.onClose();
callback &amp;&amp; callback();
}
});
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">2</span>: <span class="comment">// connecting</span>
<span class="keyword">this</span>.once(<span class="string">'open'</span>, <span class="keyword">function</span>(){
self.close(callback);
});
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">3</span>: <span class="comment">// disconnecting</span>
<span class="keyword">if</span> (!callback) <span class="keyword">break</span>;
<span class="keyword">this</span>.once(<span class="string">'close'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
callback();
});
<span class="keyword">break</span>;
}
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>[callback]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>optional</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span>self</span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method private"><h3 id="connection_Connection-onClose"><a href="#connection_Connection-onClose">Connection#onClose()</a></h3><p>Called when the connection closes</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.onClose = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">this</span>.readyState = STATES.disconnected;
<span class="comment">// avoid having the collection subscribe to our event emitter</span>
<span class="comment">// to prevent 0.3 warning</span>
<span class="keyword">for</span> (<span class="keyword">var</span> i <span class="keyword">in</span> <span class="keyword">this</span>.collections)
<span class="keyword">this</span>.collections[i].onClose();
<span class="keyword">this</span>.emit(<span class="string">'close'</span>);
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="connection_Connection-collection"><a href="#connection_Connection-collection">Connection#collection(<code>name</code>, <code>[options]</code>)</a></h3><p>Retrieves a collection, creating it if not cached.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.collection = <span class="function"><span class="keyword">function</span> <span class="params">(name, options)</span> {</span>
<span class="keyword">if</span> (!(name <span class="keyword">in</span> <span class="keyword">this</span>.collections))
<span class="keyword">this</span>.collections[name] = <span class="keyword">new</span> Collection(name, <span class="keyword">this</span>, options);
<span class="keyword">return</span> <span class="keyword">this</span>.collections[name];
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>name</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>of the collection</span></li><li><code>[options]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>optional collection options</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#collection_Collection">Collection</a>&gt; </span><span>collection instance</span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="connection_Connection-model"><a href="#connection_Connection-model">Connection#model(<code>name</code>, <code>[schema]</code>, <code>[collection]</code>)</a></h3><p>Defines or retrieves a model.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.model = <span class="function"><span class="keyword">function</span> <span class="params">(name, schema, collection)</span> {</span>
<span class="keyword">if</span> (!<span class="keyword">this</span>.models[name]) {
<span class="keyword">var</span> model = <span class="keyword">this</span>.base.model(name, schema, collection, <span class="literal">true</span>)
, Model
<span class="keyword">if</span> (<span class="keyword">this</span> != model.prototype.db) {
<span class="comment">// subclass model using this connection and collection name</span>
Model = <span class="function"><span class="keyword">function</span> <span class="title">Model</span> <span class="params">(doc, fields, skipId)</span> {</span>
<span class="keyword">if</span> (!(<span class="keyword">this</span> <span class="keyword">instanceof</span> Model))
<span class="keyword">return</span> <span class="keyword">new</span> Model(doc, fields, skipId);
model.call(<span class="keyword">this</span>, doc, fields, skipId);
};
Model.__proto__ = model;
Model.prototype.__proto__ = model.prototype;
Model.db = Model.prototype.db = <span class="keyword">this</span>;
<span class="comment">// collection name discovery</span>
<span class="keyword">if</span> (<span class="string">'string'</span> === <span class="keyword">typeof</span> schema) {
collection = schema;
}
<span class="keyword">if</span> (!collection) {
collection = model.prototype.schema.set(<span class="string">'collection'</span>) || utils.toCollectionName(name);
}
<span class="keyword">var</span> s = <span class="string">'string'</span> != <span class="keyword">typeof</span> schema
? schema
: model.prototype.schema;
Model.prototype.collection = <span class="keyword">this</span>.collection(collection, s &amp;&amp; s.options.capped);
Model.collection = Model.prototype.collection;
Model.init();
}
<span class="keyword">this</span>.models[name] = Model || model;
}
<span class="keyword">return</span> <span class="keyword">this</span>.models[name];
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>name</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>the model name</span></li><li><code>[schema]</code><span class="types"> &lt;<a href="#schema_Schema">Schema</a>&gt; </span><span>a schema. necessary when defining a model</span></li><li><code>[collection]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>name of mongodb collection (optional) if not given it will be induced from model name</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#model_Model">Model</a>&gt; </span><span>The compiled model</span></li></ul></div><div class="see"><h4>See:</h4><ul class="see"><li><a href="#index_Mongoose-model" title="Mongoose#model">Mongoose#model</a></li></ul></div><div class="description"><pre><code><span class="keyword">var</span> mongoose = require(<span class="string">'mongoose'</span>);
<span class="keyword">var</span> db = mongoose.createConnection(..);
db.model(<span class="string">'Venue'</span>, <span class="keyword">new</span> Schema(..));
<span class="keyword">var</span> Ticket = db.model(<span class="string">'Ticket'</span>, <span class="keyword">new</span> Schema(..));
<span class="keyword">var</span> Venue = db.model(<span class="string">'Venue'</span>);</code></pre></div><hr class=""></div><div class="item method public"><h3 id="connection_Connection-setProfiling"><a href="#connection_Connection-setProfiling">Connection#setProfiling(<code>level</code>, <code>[ms]</code>, <code>callback</code>)</a></h3><p>Set profiling level.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.setProfiling = <span class="function"><span class="keyword">function</span> <span class="params">(level, ms, callback)</span> {</span>
<span class="keyword">if</span> (STATES.connected !== <span class="keyword">this</span>.readyState) {
<span class="keyword">return</span> <span class="keyword">this</span>.on(<span class="string">'open'</span>, <span class="keyword">this</span>.setProfiling.bind(<span class="keyword">this</span>, level, ms, callback));
}
<span class="keyword">if</span> (!callback) callback = ms, ms = <span class="number">100</span>;
<span class="keyword">var</span> cmd = {};
<span class="keyword">switch</span> (level) {
<span class="keyword">case</span> <span class="number">0</span>:
<span class="keyword">case</span> <span class="string">'off'</span>:
cmd.profile = <span class="number">0</span>;
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">1</span>:
<span class="keyword">case</span> <span class="string">'slow'</span>:
cmd.profile = <span class="number">1</span>;
<span class="keyword">if</span> (<span class="string">'number'</span> !== <span class="keyword">typeof</span> ms) {
ms = parseInt(ms, <span class="number">10</span>);
<span class="keyword">if</span> (isNaN(ms)) ms = <span class="number">100</span>;
}
cmd.slowms = ms;
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="number">2</span>:
<span class="keyword">case</span> <span class="string">'all'</span>:
cmd.profile = <span class="number">2</span>;
<span class="keyword">break</span>;
<span class="keyword">default</span>:
<span class="keyword">return</span> callback(<span class="keyword">new</span> Error(<span class="string">'Invalid profiling level: '</span>+ level));
}
<span class="keyword">this</span>.db.executeDbCommand(cmd, <span class="function"><span class="keyword">function</span> <span class="params">(err, resp)</span> {</span>
<span class="keyword">if</span> (err) <span class="keyword">return</span> callback(err);
<span class="keyword">var</span> doc = resp.documents[<span class="number">0</span>];
err = <span class="number">1</span> === doc.ok
? <span class="literal">null</span>
: <span class="keyword">new</span> Error(<span class="string">'Could not set profiling level to: '</span>+ level)
callback(err, doc);
});
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>level</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>either off (0), slow (1), or all (2)</span></li><li><code>[ms]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a>&gt; </span><span>the threshold in milliseconds above which queries will be logged when in `slow` mode. defaults to 100.</span></li><li><code>callback</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method private"><h3 id="connection_Connection-defaultOptions"><a href="#connection_Connection-defaultOptions">Connection#defaultOptions(<code>options</code>)</a></h3><p>Prepares default connection options.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Connection.prototype.defaultOptions = <span class="function"><span class="keyword">function</span> <span class="params">(options)</span> {</span>
<span class="keyword">var</span> o = options || {};
o.server = o.server || {};
<span class="keyword">if</span> (!(<span class="string">'auto_reconnect'</span> <span class="keyword">in</span> o.server)) {
o.server.auto_reconnect = <span class="literal">true</span>;
}
o.db = o.db || {};
o.db.forceServerObjectId = <span class="literal">false</span>;
<span class="keyword">return</span> o;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>options</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item property public"><h3 id="connection_Connection-db"><a href="#connection_Connection-db">Connection#<span>db</span></a></h3><p>The mongodb.Db instance, set when the connection is opened</p><hr class=""></div><div class="item property public"><h3 id="connection_Connection-collections"><a href="#connection_Connection-collections">Connection#<span>collections</span></a></h3><p>A hash of the collections associated with this connection</p><hr class=""></div><div class="item property public"><h3 id="connection_Connection-readyState"><a href="#connection_Connection-readyState">Connection#<span>readyState</span></a></h3><p>Connection ready state</p>
<ul>
<li>0 = disconnected</li>
<li>1 = connected</li>
<li>2 = connecting</li>
<li>3 = disconnecting</li>
</ul>
<p>Each state change emits its associated event name.</p>
<h4>Example</h4>
<pre><code>conn.on(<span class="string">'connected'</span>, callback);
conn.on(<span class="string">'disconnected'</span>, callback);</code></pre><hr class=""></div></li><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/document.js" id="document-js">document.js</a><div class="item method private"><h3 id="document_Document"><a href="#document_Document">Document(<code>obj</code>, <code>[fields]</code>, <code>[skipId]</code>)</a></h3><p>Document constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">Document</span> <span class="params">(obj, fields, skipId)</span> {</span>
<span class="comment">// node &lt;0.4.3 bug</span>
<span class="keyword">if</span> (!<span class="keyword">this</span>._events) <span class="keyword">this</span>._events = {};
<span class="keyword">this</span>.setMaxListeners(<span class="number">0</span>);
<span class="keyword">if</span> (<span class="string">'boolean'</span> === <span class="keyword">typeof</span> fields) {
<span class="keyword">this</span>._strictMode = fields;
<span class="keyword">this</span>._selected = fields = <span class="literal">undefined</span>;
} <span class="keyword">else</span> {
<span class="keyword">this</span>._strictMode = <span class="keyword">this</span>.schema.options &amp;&amp; <span class="keyword">this</span>.schema.options.strict;
<span class="keyword">this</span>._selected = fields;
}
<span class="keyword">this</span>.isNew = <span class="literal">true</span>;
<span class="keyword">this</span>.errors = <span class="literal">undefined</span>;
<span class="keyword">this</span>._shardval = <span class="literal">undefined</span>;
<span class="keyword">this</span>._saveError = <span class="literal">undefined</span>;
<span class="keyword">this</span>._validationError = <span class="literal">undefined</span>;
<span class="keyword">this</span>._adhocPaths = <span class="literal">undefined</span>;
<span class="keyword">this</span>._removing = <span class="literal">undefined</span>;
<span class="keyword">this</span>._inserting = <span class="literal">undefined</span>;
<span class="keyword">this</span>.__version = <span class="literal">undefined</span>;
<span class="keyword">this</span>.__getters = {};
<span class="keyword">this</span>.__id = <span class="literal">undefined</span>;
<span class="keyword">this</span>._activePaths = <span class="keyword">new</span> ActiveRoster;
<span class="keyword">var</span> required = <span class="keyword">this</span>.schema.requiredPaths();
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; required.length; ++i) {
<span class="keyword">this</span>._activePaths.require(required[i]);
}
<span class="keyword">this</span>._doc = <span class="keyword">this</span>._buildDoc(obj, fields, skipId);
<span class="keyword">if</span> (obj) <span class="keyword">this</span>.set(obj, <span class="literal">undefined</span>, <span class="literal">true</span>);
<span class="keyword">this</span>._registerHooks();
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>obj</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>the values to set</span></li><li><code>[fields]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>the fields which were selected in the query returning this document</span></li><li><code>[skipId]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span>bool, should we auto create an ObjectId _id</span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="http://nodejs.org/api/events.html#events_class_events_eventemitter" title="NodeJS EventEmitter">NodeJS EventEmitter</a></li></ul></div><div class="events"><h4>Events:</h4><ul><li><p><code>init</code>: Emitted on a document after it has was retreived from the db and fully hydrated by Mongoose.</p></li><li><p><code>save</code>: Emitted when the document is successfully saved</p></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_buildDoc"><a href="#document_Document-_buildDoc">Document#_buildDoc(<code>obj</code>, <code>[fields]</code>, <code>[skipId]</code>)</a></h3><p>Builds the default doc structure</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._buildDoc = <span class="function"><span class="keyword">function</span> <span class="params">(obj, fields, skipId)</span> {</span>
<span class="keyword">var</span> doc = {}
, self = <span class="keyword">this</span>
, exclude
, keys
, key
, ki
<span class="comment">// determine if this doc is a result of a query with</span>
<span class="comment">// excluded fields</span>
<span class="keyword">if</span> (fields &amp;&amp; <span class="string">'Object'</span> === fields.constructor.name) {
keys = Object.keys(fields);
ki = keys.length;
<span class="keyword">while</span> (ki--) {
<span class="keyword">if</span> (<span class="string">'_id'</span> !== keys[ki]) {
exclude = <span class="number">0</span> === fields[keys[ki]];
<span class="keyword">break</span>;
}
}
}
<span class="keyword">var</span> paths = Object.keys(<span class="keyword">this</span>.schema.paths)
, plen = paths.length
, ii = <span class="number">0</span>
<span class="keyword">for</span> (; ii &lt; plen; ++ii) {
<span class="keyword">var</span> p = paths[ii];
<span class="keyword">if</span> (<span class="string">'_id'</span> == p) {
<span class="keyword">if</span> (skipId) <span class="keyword">continue</span>;
<span class="keyword">if</span> (obj &amp;&amp; <span class="string">'_id'</span> <span class="keyword">in</span> obj) <span class="keyword">continue</span>;
}
<span class="keyword">var</span> type = <span class="keyword">this</span>.schema.paths[p]
, path = p.split(<span class="string">'.'</span>)
, len = path.length
, last = len-<span class="number">1</span>
, doc_ = doc
, i = <span class="number">0</span>
<span class="keyword">for</span> (; i &lt; len; ++i) {
<span class="keyword">var</span> piece = path[i]
, def
<span class="keyword">if</span> (i === last) {
<span class="keyword">if</span> (fields) {
<span class="keyword">if</span> (exclude) {
<span class="comment">// apply defaults to all non-excluded fields</span>
<span class="keyword">if</span> (p <span class="keyword">in</span> fields) <span class="keyword">continue</span>;
def = type.getDefault(self, <span class="literal">true</span>);
<span class="keyword">if</span> (<span class="string">'undefined'</span> !== <span class="keyword">typeof</span> def) {
doc_[piece] = def;
self._activePaths.<span class="keyword">default</span>(p);
}
} <span class="keyword">else</span> <span class="keyword">if</span> (p <span class="keyword">in</span> fields) {
<span class="comment">// selected field</span>
def = type.getDefault(self, <span class="literal">true</span>);
<span class="keyword">if</span> (<span class="string">'undefined'</span> !== <span class="keyword">typeof</span> def) {
doc_[piece] = def;
self._activePaths.<span class="keyword">default</span>(p);
}
}
} <span class="keyword">else</span> {
def = type.getDefault(self, <span class="literal">true</span>);
<span class="keyword">if</span> (<span class="string">'undefined'</span> !== <span class="keyword">typeof</span> def) {
doc_[piece] = def;
self._activePaths.<span class="keyword">default</span>(p);
}
}
} <span class="keyword">else</span> {
doc_ = doc_[piece] || (doc_[piece] = {});
}
}
};
<span class="keyword">return</span> doc;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>obj</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li><li><code>[fields]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li><li><code>[skipId]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-init"><a href="#document_Document-init">Document#init(<code>doc</code>, <code>fn</code>)</a></h3><p>Initializes the document without setters or marking anything modified.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.init = <span class="function"><span class="keyword">function</span> <span class="params">(doc, fn)</span> {</span>
<span class="keyword">this</span>.isNew = <span class="literal">false</span>;
init(<span class="keyword">this</span>, doc, <span class="keyword">this</span>._doc);
<span class="keyword">this</span>._storeShard();
<span class="keyword">this</span>.emit(<span class="string">'init'</span>);
<span class="keyword">if</span> (fn) fn(<span class="literal">null</span>);
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>doc</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>document returned by mongo</span></li><li><code>fn</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>callback</span></li></ul></div><div class="description"><p>Called internally after a document is returned from mongodb.</p></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_storeShard"><a href="#document_Document-_storeShard">Document#_storeShard()</a></h3><p>Stores the current values of the shard keys.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._storeShard = <span class="function"><span class="keyword">function</span> <span class="title">_storeShard</span> <span class="params">()</span> {</span>
<span class="comment">// backwards compat</span>
<span class="keyword">var</span> key = <span class="keyword">this</span>.schema.options.shardKey || <span class="keyword">this</span>.schema.options.shardkey;
<span class="keyword">if</span> (!(key &amp;&amp; <span class="string">'Object'</span> == key.constructor.name)) <span class="keyword">return</span>;
<span class="keyword">var</span> orig = <span class="keyword">this</span>._shardval = {}
, paths = Object.keys(key)
, len = paths.length
, val
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; ++i) {
val = <span class="keyword">this</span>.getValue(paths[i]);
<span class="keyword">if</span> (isMongooseObject(val)) {
orig[paths[i]] = val.toObject({ depopulate: <span class="literal">true</span> })
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="literal">null</span> != val &amp;&amp; val.valueOf) {
orig[paths[i]] = val.valueOf();
} <span class="keyword">else</span> {
orig[paths[i]] = val;
}
}
}</code></pre></div><div class="description"><h4>Note:</h4>
<p><em>Shard key values do not / are not allowed to change.</em></p></div><hr class="private"></div><div class="item method public"><h3 id="document_Document-update"><a href="#document_Document-update">Document#update(<code>doc</code>, <code>options</code>, <code>callback</code>)</a></h3><p>Sends an update command with this document <code>_id</code> as the query selector.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.update = <span class="function"><span class="keyword">function</span> <span class="title">update</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> args = utils.args(arguments);
args.unshift({_id: <span class="keyword">this</span>._id});
<span class="keyword">this</span>.constructor.update.apply(<span class="keyword">this</span>.constructor, args);
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>doc</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li><li><code>options</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li><li><code>callback</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#query-js">Query</a>&gt; </span><span></span></li></ul></div><div class="description"><h4>Example:</h4>
<pre><code>weirdCar.update({$inc: {wheels:<span class="number">1</span>}}, { safe: <span class="literal">true</span> }, callback);</code></pre>
<h4>Valid options:</h4>
<ul>
<li><code>safe</code> safe mode (defaults to value set in schema (true))</li>
<li><code>upsert</code> (boolean) whether to create the doc if it doesn't match (false)</li>
</ul></div><hr class=""></div><div class="item method public"><h3 id="document_Document-set"><a href="#document_Document-set">Document#set(<code>path</code>, <code>val</code>, <code>[type]</code>)</a></h3><p>Sets the value of a path, or many paths.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.set = <span class="function"><span class="keyword">function</span> <span class="params">(path, val, type)</span> {</span>
<span class="keyword">var</span> constructing = <span class="literal">true</span> === type
, adhoc = type &amp;&amp; <span class="literal">true</span> !== type
, adhocs
<span class="keyword">if</span> (adhoc) {
adhocs = <span class="keyword">this</span>._adhocPaths || (<span class="keyword">this</span>._adhocPaths = {});
adhocs[path] = Schema.interpretAsType(path, type);
}
<span class="keyword">if</span> (<span class="string">'string'</span> !== <span class="keyword">typeof</span> path) {
<span class="comment">// new Document({ key: val })</span>
<span class="keyword">if</span> (<span class="literal">null</span> === path || <span class="literal">undefined</span> === path) {
<span class="keyword">var</span> _ = path;
path = val;
val = _;
} <span class="keyword">else</span> {
<span class="keyword">var</span> prefix = val
? val + <span class="string">'.'</span>
: <span class="string">''</span>;
<span class="keyword">if</span> (path <span class="keyword">instanceof</span> Document) path = path._doc;
<span class="keyword">var</span> keys = Object.keys(path)
, i = keys.length
, pathtype
, key
<span class="keyword">while</span> (i--) {
key = keys[i];
<span class="keyword">if</span> (<span class="literal">null</span> != path[key] &amp;&amp; <span class="string">'Object'</span> === path[key].constructor.name
&amp;&amp; !(<span class="keyword">this</span>._path(prefix + key) <span class="keyword">instanceof</span> MixedSchema)) {
<span class="keyword">this</span>.set(path[key], prefix + key, constructing);
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>._strictMode) {
pathtype = <span class="keyword">this</span>.schema.pathType(prefix + key);
<span class="keyword">if</span> (<span class="string">'real'</span> === pathtype || <span class="string">'virtual'</span> === pathtype) {
<span class="keyword">this</span>.set(prefix + key, path[key], constructing);
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'throw'</span> == <span class="keyword">this</span>._strictMode) {
<span class="keyword">throw</span> <span class="keyword">new</span> Error(<span class="string">"Field `"</span> + key + <span class="string">"` is not in schema."</span>);
}
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="literal">undefined</span> !== path[key]) {
<span class="keyword">this</span>.set(prefix + key, path[key], constructing);
}
}
<span class="keyword">return</span> <span class="keyword">this</span>;
}
}
<span class="comment">// ensure _strict is honored for obj props</span>
<span class="comment">// docschema = new Schema({ path: { nest: 'string' }})</span>
<span class="comment">// doc.set('path', obj);</span>
<span class="keyword">var</span> pathType = <span class="keyword">this</span>.schema.pathType(path);
<span class="keyword">if</span> (<span class="string">'nested'</span> == pathType &amp;&amp; val &amp;&amp; <span class="string">'Object'</span> == val.constructor.name) {
<span class="keyword">this</span>.set(val, path, constructing);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">var</span> schema;
<span class="keyword">if</span> (<span class="string">'adhocOrUndefined'</span> == pathType &amp;&amp; <span class="keyword">this</span>._strictMode) {
<span class="keyword">return</span> <span class="keyword">this</span>;
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="string">'virtual'</span> == pathType) {
schema = <span class="keyword">this</span>.schema.virtualpath(path);
schema.applySetters(val, <span class="keyword">this</span>);
<span class="keyword">return</span> <span class="keyword">this</span>;
} <span class="keyword">else</span> {
schema = <span class="keyword">this</span>._path(path);
}
<span class="keyword">var</span> parts = path.split(<span class="string">'.'</span>)
, pathToMark
<span class="comment">// When using the $set operator the path to the field must already exist.</span>
<span class="comment">// Else mongodb throws: "LEFT_SUBFIELD only supports Object"</span>
<span class="keyword">if</span> (parts.length &lt;= <span class="number">1</span>) {
pathToMark = path;
} <span class="keyword">else</span> {
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; parts.length; ++i) {
<span class="keyword">var</span> part = parts[i];
<span class="keyword">var</span> subpath = parts.slice(<span class="number">0</span>, i).concat(part).join(<span class="string">'.'</span>);
<span class="keyword">if</span> (<span class="keyword">this</span>.isDirectModified(subpath) <span class="comment">// earlier prefixes that are already</span>
<span class="comment">// marked as dirty have precedence</span>
|| <span class="keyword">this</span>.get(subpath) === <span class="literal">null</span>) {
pathToMark = subpath;
<span class="keyword">break</span>;
}
}
<span class="keyword">if</span> (!pathToMark) pathToMark = path;
}
<span class="keyword">if</span> (!schema || <span class="literal">null</span> === val || <span class="literal">undefined</span> === val) {
<span class="keyword">this</span>._set(pathToMark, path, constructing, parts, schema, val);
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="comment">// if this doc is being constructed we should not</span>
<span class="comment">// trigger getters.</span>
<span class="keyword">var</span> priorVal = constructing
? <span class="literal">undefined</span>
: <span class="keyword">this</span>.get(path);
<span class="keyword">var</span> shouldSet = <span class="keyword">this</span>.<span class="keyword">try</span>(<span class="keyword">function</span>(){
val = schema.applySetters(val, self, <span class="literal">false</span>, priorVal);
});
<span class="keyword">if</span> (shouldSet) {
<span class="keyword">this</span>._set(pathToMark, path, constructing, parts, schema, val, priorVal);
}
<span class="keyword">return</span> <span class="keyword">this</span>;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>path or object of key/vals to set</span></li><li><code>val</code><span class="types"> &lt;<a href="#Any">Any</a>&gt; </span><span>the value to set</span></li><li><code>[type]</code><span class="types"> &lt;<a href="#schema_Schema">Schema</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a>, <a href="http://nodejs.org/api/buffer.html">Buffer</a>, <a href="#etc..">etc..</a>&gt; </span><span>optionally specify a type for &quot;on-the-fly&quot; attributes</span></li></ul></div><div class="description"><h4>Example:</h4>
<pre><code><span class="comment">// path, value</span>
doc.set(path, value)
<span class="comment">// object</span>
doc.set({
path : value
, path2 : {
path : value
}
})
<span class="comment">// only-the-fly cast to number</span>
doc.set(path, value, Number)
<span class="comment">// only-the-fly cast to string</span>
doc.set(path, value, String)</code></pre></div><hr class=""></div><div class="item method private"><h3 id="document_Document-_set"><a href="#document_Document-_set">Document#_set()</a></h3><p>Handles the actual setting of the value and marking the path modified if appropriate.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._set = <span class="function"><span class="keyword">function</span> <span class="params">(pathToMark, path, constructing, parts, schema, val, priorVal)</span> {</span>
<span class="keyword">if</span> (<span class="keyword">this</span>.isNew) {
<span class="keyword">this</span>.markModified(pathToMark);
} <span class="keyword">else</span> {
priorVal || (priorVal = <span class="keyword">this</span>.get(path));
<span class="keyword">if</span> (!<span class="keyword">this</span>.isDirectModified(pathToMark)) {
<span class="keyword">if</span> (<span class="literal">undefined</span> === val &amp;&amp; !<span class="keyword">this</span>.isSelected(path)) {
<span class="comment">// special case:</span>
<span class="comment">// when a path is not selected in a query its initial</span>
<span class="comment">// value will be undefined.</span>
<span class="keyword">this</span>.markModified(pathToMark, priorVal);
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="literal">undefined</span> === val &amp;&amp; path <span class="keyword">in</span> <span class="keyword">this</span>._activePaths.states.<span class="keyword">default</span>) {
<span class="comment">// do nothing</span>
<span class="comment">// unsetting the default value which was never saved</span>
} <span class="keyword">else</span> <span class="keyword">if</span> (!deepEqual(val, priorVal)) {
<span class="keyword">this</span>.markModified(pathToMark, priorVal);
} <span class="keyword">else</span> <span class="keyword">if</span> (!constructing &amp;&amp;
<span class="literal">null</span> != val &amp;&amp;
path <span class="keyword">in</span> <span class="keyword">this</span>._activePaths.states.<span class="keyword">default</span> &amp;&amp;
deepEqual(val, schema.getDefault(<span class="keyword">this</span>, constructing))) {
<span class="comment">// special case:</span>
<span class="comment">// a path with a default was $unset on the server</span>
<span class="comment">// and the user is setting it to the same value again</span>
<span class="keyword">this</span>.markModified(pathToMark, priorVal);
}
}
}
<span class="keyword">var</span> obj = <span class="keyword">this</span>._doc
, i = <span class="number">0</span>
, l = parts.length
<span class="keyword">for</span> (; i &lt; l; i++) {
<span class="keyword">var</span> next = i + <span class="number">1</span>
, last = next === l;
<span class="keyword">if</span> (last) {
obj[parts[i]] = val;
} <span class="keyword">else</span> {
<span class="keyword">if</span> (obj[parts[i]] &amp;&amp; <span class="string">'Object'</span> === obj[parts[i]].constructor.name) {
obj = obj[parts[i]];
} <span class="keyword">else</span> <span class="keyword">if</span> (obj[parts[i]] &amp;&amp; Array.isArray(obj[parts[i]])) {
obj = obj[parts[i]];
} <span class="keyword">else</span> {
obj = obj[parts[i]] = {};
}
}
}
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-getValue"><a href="#document_Document-getValue">Document#getValue(<code>path</code>)</a></h3><p>Gets a raw value from a path (no getters)</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.getValue = <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">var</span> parts = path.split(<span class="string">'.'</span>)
, obj = <span class="keyword">this</span>._doc
, part;
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, l = parts.length; i &lt; l; i++) {
part = parts[i];
obj = obj.getValue
? obj.getValue(part) <span class="comment">// If we have an embedded array document member</span>
: obj[part];
<span class="keyword">if</span> (!obj) <span class="keyword">return</span> obj;
}
<span class="keyword">return</span> obj;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-setValue"><a href="#document_Document-setValue">Document#setValue(<code>path</code>, <code>value</code>)</a></h3><p>Sets a raw value for a path (no casting, setters, transformations)</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.setValue = <span class="function"><span class="keyword">function</span> <span class="params">(path, val)</span> {</span>
<span class="keyword">var</span> parts = path.split(<span class="string">'.'</span>)
, obj = <span class="keyword">this</span>._doc;
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = parts.length-<span class="number">1</span>; i &lt; len; i++) {
obj = obj[parts[i]];
}
obj[parts[len]] = val;
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li><li><code>value</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="document_Document-get"><a href="#document_Document-get">Document#get(<code>path</code>, <code>[type]</code>)</a></h3><p>Returns the value of a path.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.get = <span class="function"><span class="keyword">function</span> <span class="params">(path, type)</span> {</span>
<span class="keyword">var</span> adhocs;
<span class="keyword">if</span> (type) {
adhocs = <span class="keyword">this</span>._adhocPaths || (<span class="keyword">this</span>._adhocPaths = {});
adhocs[path] = Schema.interpretAsType(path, type);
}
<span class="keyword">var</span> schema = <span class="keyword">this</span>._path(path) || <span class="keyword">this</span>.schema.virtualpath(path)
, pieces = path.split(<span class="string">'.'</span>)
, obj = <span class="keyword">this</span>._doc;
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, l = pieces.length; i &lt; l; i++) {
obj = <span class="literal">null</span> == obj ? <span class="literal">null</span> : obj[pieces[i]];
}
<span class="keyword">if</span> (schema) {
obj = schema.applyGetters(obj, <span class="keyword">this</span>);
}
<span class="keyword">return</span> obj;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li><li><code>[type]</code><span class="types"> &lt;<a href="#schema_Schema">Schema</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a>, <a href="http://nodejs.org/api/buffer.html">Buffer</a>, <a href="#etc..">etc..</a>&gt; </span><span>optionally specify a type for on-the-fly attributes</span></li></ul></div><div class="description"><h4>Example</h4>
<pre><code><span class="comment">// path</span>
doc.get(<span class="string">'age'</span>) <span class="comment">// 47</span>
<span class="comment">// dynamic casting to a string</span>
doc.get(<span class="string">'age'</span>, String) <span class="comment">// "47"</span></code></pre></div><hr class=""></div><div class="item method private"><h3 id="document_Document-_path"><a href="#document_Document-_path">Document#_path(<code>path</code>)</a></h3><p>Returns the schematype for the given <code>path</code>.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._path = <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">var</span> adhocs = <span class="keyword">this</span>._adhocPaths
, adhocType = adhocs &amp;&amp; adhocs[path];
<span class="keyword">if</span> (adhocType) {
<span class="keyword">return</span> adhocType;
} <span class="keyword">else</span> {
<span class="keyword">return</span> <span class="keyword">this</span>.schema.path(path);
}
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="document_Document-markModified"><a href="#document_Document-markModified">Document#markModified(<code>path</code>)</a></h3><p>Marks the path as having pending changes to write to the db.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.markModified = <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">this</span>._activePaths.modify(path);
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>the path to mark modified</span></li></ul></div><div class="description"><p><em>Very helpful when using <a href="./schematypes.html#mixed">Mixed</a> types.</em></p>
<h4>Example:</h4>
<pre><code>doc.mixed.type = <span class="string">'changed'</span>;
doc.markModified(<span class="string">'mixed.type'</span>);
doc.save() <span class="comment">// changes to mixed.type are now persisted</span></code></pre></div><hr class=""></div><div class="item method private"><h3 id="document_Document-try"><a href="#document_Document-try">Document#try(<code>fn</code>, <code>scope</code>)</a></h3><p>Catches errors that occur during execution of <code>fn</code> and stores them to later be passed when <code>save()</code> is executed.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.<span class="keyword">try</span> = <span class="function"><span class="keyword">function</span> <span class="params">(fn, scope)</span> {</span>
<span class="keyword">var</span> res;
<span class="keyword">try</span> {
fn.call(scope);
res = <span class="literal">true</span>;
} <span class="keyword">catch</span> (e) {
<span class="keyword">this</span>._error(e);
res = <span class="literal">false</span>;
}
<span class="keyword">return</span> res;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>fn</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>function to execute</span></li><li><code>scope</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>the scope with which to call fn</span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="document_Document-modifiedPaths"><a href="#document_Document-modifiedPaths">Document#modifiedPaths()</a></h3><p>Returns the list of paths that have been modified.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.modifiedPaths = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> directModifiedPaths = Object.keys(<span class="keyword">this</span>._activePaths.states.modify);
<span class="keyword">return</span> directModifiedPaths.reduce(<span class="function"><span class="keyword">function</span> <span class="params">(list, path)</span> {</span>
<span class="keyword">var</span> parts = path.split(<span class="string">'.'</span>);
<span class="keyword">return</span> list.concat(parts.reduce(<span class="function"><span class="keyword">function</span> <span class="params">(chains, part, i)</span> {</span>
<span class="keyword">return</span> chains.concat(parts.slice(<span class="number">0</span>, i).concat(part).join(<span class="string">'.'</span>));
}, []));
}, []);
};</code></pre></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array">Array</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="document_Document-isModified"><a href="#document_Document-isModified">Document#isModified(<code>[path]</code>)</a></h3><p>Returns true if this document was modified, else false.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.isModified = <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">return</span> path
? !!~<span class="keyword">this</span>.modifiedPaths().indexOf(path)
: <span class="keyword">this</span>._activePaths.some(<span class="string">'modify'</span>);
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>[path]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>optional</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span></span></li></ul></div><div class="description"><p>If <code>path</code> is given, checks if a path or any full path containing <code>path</code> as part of its path chain has been modified.</p>
<h4>Example</h4>
<pre><code>doc.set(<span class="string">'documents.0.title'</span>, <span class="string">'changed'</span>);
doc.isModified() <span class="comment">// true</span>
doc.isModified(<span class="string">'documents'</span>) <span class="comment">// true</span>
doc.isModified(<span class="string">'documents.0.title'</span>) <span class="comment">// true</span>
doc.isDirectModified(<span class="string">'documents'</span>) <span class="comment">// false</span></code></pre></div><hr class=""></div><div class="item method public"><h3 id="document_Document-isDirectModified"><a href="#document_Document-isDirectModified">Document#isDirectModified(<code>path</code>)</a></h3><p>Returns true if <code>path</code> was directly set and modified, else false.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.isDirectModified = <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">return</span> (path <span class="keyword">in</span> <span class="keyword">this</span>._activePaths.states.modify);
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span></span></li></ul></div><div class="description"><h4>Example</h4>
<pre><code>doc.set(<span class="string">'documents.0.title'</span>, <span class="string">'changed'</span>);
doc.isDirectModified(<span class="string">'documents.0.title'</span>) <span class="comment">// true</span>
doc.isDirectModified(<span class="string">'documents'</span>) <span class="comment">// false</span></code></pre></div><hr class=""></div><div class="item method public"><h3 id="document_Document-isInit"><a href="#document_Document-isInit">Document#isInit(<code>path</code>)</a></h3><p>Checks if <code>path</code> was initialized.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.isInit = <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">return</span> (path <span class="keyword">in</span> <span class="keyword">this</span>._activePaths.states.init);
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="document_Document-isSelected"><a href="#document_Document-isSelected">Document#isSelected(<code>path</code>)</a></h3><p>Checks if <code>path</code> was selected in the source query which initialized this document.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.isSelected = <span class="function"><span class="keyword">function</span> <span class="title">isSelected</span> <span class="params">(path)</span> {</span>
<span class="keyword">if</span> (<span class="keyword">this</span>._selected) {
<span class="keyword">if</span> (<span class="string">'_id'</span> === path) {
<span class="keyword">return</span> <span class="number">0</span> !== <span class="keyword">this</span>._selected._id;
}
<span class="keyword">var</span> paths = Object.keys(<span class="keyword">this</span>._selected)
, i = paths.length
, inclusive = <span class="literal">false</span>
, cur
<span class="keyword">if</span> (<span class="number">1</span> === i &amp;&amp; <span class="string">'_id'</span> === paths[<span class="number">0</span>]) {
<span class="comment">// only _id was selected.</span>
<span class="keyword">return</span> <span class="number">0</span> === <span class="keyword">this</span>._selected._id;
}
<span class="keyword">while</span> (i--) {
cur = paths[i];
<span class="keyword">if</span> (<span class="string">'_id'</span> == cur) <span class="keyword">continue</span>;
inclusive = !! <span class="keyword">this</span>._selected[cur];
<span class="keyword">break</span>;
}
<span class="keyword">if</span> (path <span class="keyword">in</span> <span class="keyword">this</span>._selected) {
<span class="keyword">return</span> inclusive;
}
i = paths.length;
<span class="keyword">var</span> pathDot = path + <span class="string">'.'</span>;
<span class="keyword">while</span> (i--) {
cur = paths[i];
<span class="keyword">if</span> (<span class="string">'_id'</span> == cur) <span class="keyword">continue</span>;
<span class="keyword">if</span> (<span class="number">0</span> === cur.indexOf(pathDot)) {
<span class="keyword">return</span> inclusive;
}
<span class="keyword">if</span> (<span class="number">0</span> === pathDot.indexOf(cur)) {
<span class="keyword">return</span> inclusive;
}
}
<span class="keyword">return</span> ! inclusive;
}
<span class="keyword">return</span> <span class="literal">true</span>;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span></span></li></ul></div><div class="description"><h4>Example</h4>
<pre><code>Thing.findOne().select(<span class="string">'name'</span>).exec(<span class="function"><span class="keyword">function</span> <span class="params">(err, doc)</span> {</span>
doc.isSelected(<span class="string">'name'</span>) <span class="comment">// true</span>
doc.isSelected(<span class="string">'age'</span>) <span class="comment">// false</span>
})</code></pre></div><hr class=""></div><div class="item method public"><h3 id="document_Document-validate"><a href="#document_Document-validate">Document#validate(<code>cb</code>)</a></h3><p>Executes registered validation rules for this document.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.validate = <span class="function"><span class="keyword">function</span> <span class="params">(cb)</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>
<span class="comment">// only validate required fields when necessary</span>
<span class="keyword">var</span> paths = Object.keys(<span class="keyword">this</span>._activePaths.states.require).filter(<span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">if</span> (!self.isSelected(path) &amp;&amp; !self.isModified(path)) <span class="keyword">return</span> <span class="literal">false</span>;
<span class="keyword">return</span> <span class="literal">true</span>;
});
paths = paths.concat(Object.keys(<span class="keyword">this</span>._activePaths.states.init));
paths = paths.concat(Object.keys(<span class="keyword">this</span>._activePaths.states.modify));
<span class="keyword">if</span> (<span class="number">0</span> === paths.length) {
complete();
<span class="keyword">return</span> <span class="keyword">this</span>;
}
<span class="keyword">var</span> validating = {}
, total = <span class="number">0</span>;
paths.forEach(validatePath);
<span class="keyword">return</span> <span class="keyword">this</span>;
<span class="function"><span class="keyword">function</span> <span class="title">validatePath</span> <span class="params">(path)</span> {</span>
<span class="keyword">if</span> (validating[path]) <span class="keyword">return</span>;
validating[path] = <span class="literal">true</span>;
total++;
process.nextTick(<span class="keyword">function</span>(){
<span class="keyword">var</span> p = self.schema.path(path);
<span class="keyword">if</span> (!p) <span class="keyword">return</span> --total || complete();
p.doValidate(self.getValue(path), <span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
<span class="keyword">if</span> (err) self.invalidate(path, err, <span class="literal">true</span>);
--total || complete();
}, self);
});
}
<span class="function"><span class="keyword">function</span> <span class="title">complete</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> err = self._validationError;
self._validationError = <span class="literal">undefined</span>;
cb(err);
}
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>cb</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span>called after validation completes, passing an error if one occurred</span></li></ul></div><div class="description"><h4>Note:</h4>
<p>This method is called <code>pre</code> save and if a validation rule is violated, <a href="#model_Model-save">save</a> is aborted and the error is returned to your <code>callback</code>.</p>
<h4>Example:</h4>
<pre><code>doc.validate(<span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
<span class="keyword">if</span> (err) handleError(err);
<span class="keyword">else</span> <span class="comment">// validation passed</span>
});</code></pre></div><hr class=""></div><div class="item method public"><h3 id="document_Document-invalidate"><a href="#document_Document-invalidate">Document#invalidate(<code>path</code>, <code>err</code>)</a></h3><p>Marks a path as invalid, causing validation to fail.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.invalidate = <span class="function"><span class="keyword">function</span> <span class="params">(path, err)</span> {</span>
<span class="keyword">if</span> (!<span class="keyword">this</span>._validationError) {
<span class="keyword">this</span>._validationError = <span class="keyword">new</span> ValidationError(<span class="keyword">this</span>);
}
<span class="keyword">if</span> (!err || <span class="string">'string'</span> === <span class="keyword">typeof</span> err) {
err = <span class="keyword">new</span> ValidatorError(path, err);
}
<span class="keyword">this</span>._validationError.errors[path] = err;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span>the field to invalidate</span></li><li><code>err</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error">Error</a>&gt; </span><span>the error which states the reason `path` was invalid</span></li></ul></div><div class="description"></div><hr class=""></div><div class="item method private"><h3 id="document_Document-_reset"><a href="#document_Document-_reset">Document#_reset()</a></h3><p>Resets the internal modified state of this document.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._reset = <span class="function"><span class="keyword">function</span> <span class="title">reset</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>;
DocumentArray || (DocumentArray = require(<span class="string">'./types/documentarray'</span>));
<span class="keyword">this</span>._activePaths
.map(<span class="string">'init'</span>, <span class="string">'modify'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(i)</span> {</span>
<span class="keyword">return</span> self.getValue(i);
})
.filter(<span class="function"><span class="keyword">function</span> <span class="params">(val)</span> {</span>
<span class="keyword">return</span> (val &amp;&amp; val <span class="keyword">instanceof</span> DocumentArray &amp;&amp; val.length);
})
.forEach(<span class="function"><span class="keyword">function</span> <span class="params">(array)</span> {</span>
array.forEach(<span class="function"><span class="keyword">function</span> <span class="params">(doc)</span> {</span>
doc._reset();
});
});
<span class="comment">// clear atomics</span>
<span class="keyword">this</span>._dirty().forEach(<span class="function"><span class="keyword">function</span> <span class="params">(dirt)</span> {</span>
<span class="keyword">var</span> type = dirt.value;
<span class="keyword">if</span> (type &amp;&amp; type._atomics) {
type._atomics = {};
}
});
<span class="comment">// Clear 'modify'('dirty') cache</span>
<span class="keyword">this</span>._activePaths.clear(<span class="string">'modify'</span>);
<span class="keyword">this</span>._validationError = <span class="literal">undefined</span>;
<span class="keyword">this</span>.errors = <span class="literal">undefined</span>;
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="keyword">this</span>.schema.requiredPaths().forEach(<span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
self._activePaths.require(path);
});
<span class="keyword">return</span> <span class="keyword">this</span>;
}</code></pre></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#document_Document">Document</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_dirty"><a href="#document_Document-_dirty">Document#_dirty()</a></h3><p>Returns this documents dirty paths / vals.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._dirty = <span class="function"><span class="keyword">function</span> <span class="title">_dirty</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> self = <span class="keyword">this</span>;
<span class="keyword">var</span> all = <span class="keyword">this</span>._activePaths.map(<span class="string">'modify'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(path)</span> {</span>
<span class="keyword">return</span> { path: path
, value: self.getValue(path)
, schema: self._path(path) };
});
<span class="comment">// Sort dirty paths in a flat hierarchy.</span>
all.sort(<span class="function"><span class="keyword">function</span> <span class="params">(a, b)</span> {</span>
<span class="keyword">return</span> (a.path &lt; b.path ? -<span class="number">1</span> : (a.path > b.path ? <span class="number">1</span> : <span class="number">0</span>));
});
<span class="comment">// Ignore "foo.a" if "foo" is dirty already.</span>
<span class="keyword">var</span> minimal = []
, lastPath
, top;
all.forEach(<span class="function"><span class="keyword">function</span> <span class="params">(item, i)</span> {</span>
<span class="keyword">if</span> (item.path.indexOf(lastPath) !== <span class="number">0</span>) {
lastPath = item.path + <span class="string">'.'</span>;
minimal.push(item);
top = item;
} <span class="keyword">else</span> {
<span class="keyword">if</span> (!(item.value &amp;&amp; top.value)) <span class="keyword">return</span>;
<span class="comment">// special case for top level MongooseArrays</span>
<span class="keyword">if</span> (top.value._atomics &amp;&amp; top.value.hasAtomics()) {
<span class="comment">// and the item is not a MongooseArray</span>
<span class="keyword">if</span> (!(item.value._atomics &amp;&amp; item.value.hasAtomics())) {
<span class="comment">// theres a sub path of top being explicitly set.</span>
<span class="comment">// the only way to honor all of their modifications</span>
<span class="comment">// is through a $set of entire array.</span>
<span class="comment">// change top to a $set op</span>
top.value._atomics = {};
top.value._atomics.$set = top.value;
}
}
}
});
top = lastPath = <span class="literal">null</span>;
<span class="keyword">return</span> minimal;
}</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_setSchema"><a href="#document_Document-_setSchema">Document#_setSchema(<code>schema</code>)</a></h3><p>Assigns/compiles <code>schema</code> into this documents prototype.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._setSchema = <span class="function"><span class="keyword">function</span> <span class="params">(schema)</span> {</span>
compile(schema.tree, <span class="keyword">this</span>);
<span class="keyword">this</span>.schema = schema;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>schema</code><span class="types"> &lt;<a href="#schema_Schema">Schema</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_registerHooks"><a href="#document_Document-_registerHooks">Document#_registerHooks()</a></h3><p>Register default hooks</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._registerHooks = function _registerHooks () {
if (!this.save) return;
DocumentArray || (DocumentArray = require('./types/documentarray'));
this.pre('save', function (next) {
// we keep the error semaphore to make sure we don't
// call `save` unnecessarily (we only need 1 error)
var subdocs = 0
, error = false
, self = this;
// check for DocumentArrays
var arrays = this._activePaths
.map('init', 'modify', function (i) {
return self.getValue(i);
})
.filter(function (val) {
return (val &amp;&amp; val instanceof DocumentArray &amp;&amp; val.length);
});
if (!arrays.length)
return next();
arrays.forEach(function (array) {
subdocs += array.length;
array.forEach(function (value) {
if (error) return;
value.save(function (err) {
if (error) return;
if (err) {
error = true;
self._validationError = undefined;
return next(err);
}
--subdocs || next();
});
});
});
}, function (err) {
// emit on the Model if listening
if (this.constructor.listeners('error').length) {
this.constructor.emit('error', err);
} else {
// emit on the connection
if (!this.db.listeners('error').length) {
err.stack = 'No listeners detected, throwing. '
+ 'Consider adding an error listener to your connection.
'
+ err.stack
}
this.db.emit('error', err);
}
}).pre('save', function checkForExistingErrors (next) {
// if any doc.set() calls failed
if (this._saveError) {
next(this._saveError);
this._saveError = null;
} else {
next();
}
}).pre('save', function validation (next) {
return this.validate(next);
});
// add user defined queues
this._doQueue();
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_error"><a href="#document_Document-_error">Document#_error(<code>err</code>)</a></h3><p>Registers an error</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._error = <span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
<span class="keyword">this</span>._saveError = err;
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>err</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error">Error</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="document_Document-_doQueue"><a href="#document_Document-_doQueue">Document#_doQueue()</a></h3><p>Executes methods queued from the Schema definition</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype._doQueue = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">var</span> q = <span class="keyword">this</span>.schema &amp;&amp; <span class="keyword">this</span>.schema.callQueue;
<span class="keyword">if</span> (q) {
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, l = q.length; i &lt; l; i++) {
<span class="keyword">this</span>[q[i][<span class="number">0</span>]].apply(<span class="keyword">this</span>, q[i][<span class="number">1</span>]);
}
}
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="document_Document-toObject"><a href="#document_Document-toObject">Document#toObject(<code>[options]</code>)</a></h3><p>Converts this document into a plain javascript object</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.toObject = <span class="function"><span class="keyword">function</span> <span class="params">(options)</span> {</span>
<span class="comment">// When internally saving this document we always pass options,</span>
<span class="comment">// bypassing the custom schema options.</span>
<span class="keyword">if</span> (!(options &amp;&amp; <span class="string">'Object'</span> == options.constructor.name)) {
options = <span class="keyword">this</span>.schema.options.toObject
? clone(<span class="keyword">this</span>.schema.options.toObject)
: {};
}
;(<span class="string">'minimize'</span> <span class="keyword">in</span> options) || (options.minimize = <span class="keyword">this</span>.schema.options.minimize);
<span class="keyword">var</span> ret = clone(<span class="keyword">this</span>._doc, options);
<span class="keyword">if</span> (options.virtuals || options.getters &amp;&amp; <span class="literal">false</span> !== options.virtuals) {
applyGetters(<span class="keyword">this</span>, ret, <span class="string">'virtuals'</span>, options);
}
<span class="keyword">if</span> (options.getters) {
applyGetters(<span class="keyword">this</span>, ret, <span class="string">'paths'</span>, options);
}
<span class="keyword">return</span> ret;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>[options]</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>js object</span></li></ul></div><div class="description"><p>Available options</p>
<ul>
<li><code>getters</code> apply all getters (path and virtual getters)</li>
<li><code>virtuals</code> apply virtual getters (can override <code>getters</code> option)</li>
<li><code>minimize</code> remove empty objects (defaults to true)</li>
</ul>
<p>Example of only applying path getters</p>
<pre><code>doc.toObject({ getters: <span class="literal">true</span>, virtuals: <span class="literal">false</span> })</code></pre>
<p>Example of only applying virtual getters</p>
<pre><code>doc.toObject({ virtuals: <span class="literal">true</span> })</code></pre>
<p>Example of applying both path and virtual getters</p>
<pre><code>doc.toObject({ getters: <span class="literal">true</span> })</code></pre></div><hr class=""></div><div class="item method public"><h3 id="document_Document-toJSON"><a href="#document_Document-toJSON">Document#toJSON(<code>options</code>)</a></h3><p>The return value of this method is used in calls to JSON.stringify(doc).</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.toJSON = <span class="function"><span class="keyword">function</span> <span class="params">(options)</span> {</span>
<span class="comment">// check for object type since an array of documents</span>
<span class="comment">// being stringified passes array indexes instead</span>
<span class="comment">// of options objects. JSON.stringify([doc, doc])</span>
<span class="keyword">if</span> (!(options &amp;&amp; <span class="string">'Object'</span> == options.constructor.name)) {
options = <span class="keyword">this</span>.schema.options.toJSON
? clone(<span class="keyword">this</span>.schema.options.toJSON)
: {};
}
options.json = <span class="literal">true</span>;
<span class="keyword">return</span> <span class="keyword">this</span>.toObject(options);
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>options</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>same options as Document#toObject</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span></span></li></ul></div><div class="see"><h4>See:</h4><ul class="see"><li><a href="Document#toObject" title="Document#toObject">Document#toObject</a></li></ul></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="document_Document-inspect"><a href="#document_Document-inspect">Document#inspect()</a></h3><p>Helper for console.log</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.inspect = <span class="function"><span class="keyword">function</span> <span class="params">(options)</span> {</span>
<span class="keyword">var</span> opts = options &amp;&amp; <span class="string">'Object'</span> == options.constructor.name
? options
: <span class="literal">undefined</span>
<span class="keyword">return</span> inspect(<span class="keyword">this</span>.toObject(opts));
};</code></pre></div><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="document_Document-toString"><a href="#document_Document-toString">Document#toString()</a></h3><p>Helper for console.log</p><div class="description"></div><hr class=""></div><div class="item method public"><h3 id="document_Document-equals"><a href="#document_Document-equals">Document#equals(<code>doc</code>)</a></h3><p>Returns true if the Document stores the same data as doc.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Document.prototype.equals = <span class="function"><span class="keyword">function</span> <span class="params">(doc)</span> {</span>
<span class="keyword">var</span> tid = <span class="keyword">this</span>.get(<span class="string">'_id'</span>);
<span class="keyword">var</span> docid = doc.get(<span class="string">'_id'</span>);
<span class="keyword">return</span> tid.equals
? tid.equals(docid)
: tid === docid;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>doc</code><span class="types"> &lt;<a href="#document_Document">Document</a>&gt; </span><span>a document to compare</span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>&gt; </span><span></span></li></ul></div><div class="description"><p>Documents are considered equal when they have matching <code>_id</code>s.</p></div><hr class=""></div><div class="item property public"><h3 id="document_Document-errors"><a href="#document_Document-errors">Document#<span>errors</span></a></h3><p>Hash containing current validation errors.</p><hr class=""></div><div class="item property public"><h3 id="document_Document-isNew"><a href="#document_Document-isNew">Document#<span>isNew</span></a></h3><p>Boolean flag specifying if the document is new.</p><hr class=""></div><div class="item property public"><h3 id="document_Document-schema"><a href="#document_Document-schema">Document#<span>schema</span></a></h3><p>The documents schema.</p><hr class=""></div></li><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/drivers/node-mongodb-native/collection.js" id="drivers-node-mongodb-native-collection-js">drivers/node-mongodb-native/collection.js</a><div class="item method private"><h3 id="drivers_node-mongodb-native_collection_NativeCollection"><a href="#drivers_node-mongodb-native_collection_NativeCollection">NativeCollection()</a></h3><p>A <a href="https://github.com/mongodb/node-mongodb-native">node-mongodb-native</a> collection implementation.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">NativeCollection</span> <span class="params">()</span> {</span>
<span class="keyword">this</span>.collection = <span class="literal">null</span>;
MongooseCollection.apply(<span class="keyword">this</span>, arguments);
}</code></pre></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#collection_Collection">Collection</a></li></ul></div><div class="description"><p>All methods methods from the <a href="https://github.com/mongodb/node-mongodb-native">node-mongodb-native</a> driver are copied and wrapped in queue management.</p></div><hr class="private"></div><div class="item method private"><h3 id="drivers_node-mongodb-native_collection_NativeCollection-onOpen"><a href="#drivers_node-mongodb-native_collection_NativeCollection-onOpen">NativeCollection#onOpen()</a></h3><p>Called when the connection opens.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">NativeCollection.prototype.onOpen = function () {
var self = this;
if (this.collection) {
return MongooseCollection.prototype.onOpen.call(self);
}
if (!self.opts.size) {
// non-capped
return self.conn.db.collection(self.name, callback);
}
// capped
return self.conn.db.collection(self.name, function (err, c) {
if (err) return callback(err);
// discover if this collection exists and if it is capped
c.options(function (err, exists) {
if (err) return callback(err);
if (exists) {
if (exists.capped) {
callback(null, c);
} else {
var msg = 'A non-capped collection exists with this name.
'
+ ' To use this collection as a capped collection, please '
+ 'first convert it.
'
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped'
err = new Error(msg);
callback(err);
}
} else {
// create
var opts = utils.clone(self.opts);
opts.capped = true;
self.conn.db.createCollection(self.name, opts, callback);
}
});
});
function callback (err, collection) {
if (err) {
// likely a strict mode error
self.conn.emit('error', err);
} else {
self.collection = collection;
MongooseCollection.prototype.onOpen.call(self);
}
};
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="drivers_node-mongodb-native_collection_NativeCollection-onClose"><a href="#drivers_node-mongodb-native_collection_NativeCollection-onClose">NativeCollection#onClose()</a></h3><p>Called when the connection closes</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">NativeCollection.prototype.onClose = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
MongooseCollection.prototype.onClose.call(<span class="keyword">this</span>);
};</code></pre></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="drivers_node-mongodb-native_collection_NativeCollection-getIndexes"><a href="#drivers_node-mongodb-native_collection_NativeCollection-getIndexes">NativeCollection#getIndexes(<code>callback</code>)</a></h3><p>Retreives information about this collections indexes.</p><div class="params"><h4>Parameters:</h4><ul><li><code>callback</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="description"></div><hr class=""></div></li><li class="module private"><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/drivers/node-mongodb-native/connection.js" id="drivers-node-mongodb-native-connection-js">drivers/node-mongodb-native/connection.js</a><div class="item method private"><h3 id="drivers_node-mongodb-native_connection_NativeConnection"><a href="#drivers_node-mongodb-native_connection_NativeConnection">NativeConnection()</a></h3><p>A <a href="https://github.com/mongodb/node-mongodb-native">node-mongodb-native</a> connection implementation.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">NativeConnection</span><span class="params">()</span> {</span>
MongooseConnection.apply(<span class="keyword">this</span>, arguments);
};</code></pre></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#connection_Connection">Connection</a></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="drivers_node-mongodb-native_connection_NativeConnection-doOpen"><a href="#drivers_node-mongodb-native_connection_NativeConnection-doOpen">NativeConnection#doOpen(<code>fn</code>)</a></h3><p>Opens the connection to MongoDB.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">NativeConnection.prototype.doOpen = <span class="function"><span class="keyword">function</span> <span class="params">(fn)</span> {</span>
<span class="keyword">var</span> server
, self = <span class="keyword">this</span>;
<span class="keyword">if</span> (!<span class="keyword">this</span>.db) {
server = <span class="keyword">new</span> mongo.Server(<span class="keyword">this</span>.host, Number(<span class="keyword">this</span>.port), <span class="keyword">this</span>.options.server);
<span class="keyword">this</span>.db = <span class="keyword">new</span> mongo.Db(<span class="keyword">this</span>.name, server, <span class="keyword">this</span>.options.db);
}
<span class="keyword">this</span>.db.open(<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> fn(err);
fn();
listen(self);
});
<span class="keyword">return</span> <span class="keyword">this</span>;
};
<span class="function"><span class="keyword">function</span> <span class="title">listen</span> <span class="params">(conn)</span> {</span>
<span class="keyword">if</span> (conn._listening) <span class="keyword">return</span>;
conn._listening = <span class="literal">true</span>;
conn.db.on(<span class="string">'close'</span>, <span class="keyword">function</span>(){
<span class="keyword">if</span> (conn._closeCalled) <span class="keyword">return</span>;
<span class="comment">// the driver never emits an `open` event. auto_reconnect still</span>
<span class="comment">// emits a `close` event but since we never get another</span>
<span class="comment">// `open` we can't emit close</span>
<span class="keyword">if</span> (conn.db.serverConfig.autoReconnect) {
conn.readyState = STATES.disconnected;
conn.emit(<span class="string">'close'</span>);
<span class="keyword">return</span>;
}
conn.onClose();
});
conn.db.on(<span class="string">'error'</span>, <span class="keyword">function</span>(err){
conn.emit(<span class="string">'error'</span>, err);
});
conn.db.on(<span class="string">'timeout'</span>, <span class="keyword">function</span>(err){
<span class="keyword">var</span> error = <span class="keyword">new</span> Error(err &amp;&amp; err.err || <span class="string">'connection timeout'</span>);
conn.emit(<span class="string">'error'</span>, error);
});
conn.db.on(<span class="string">'open'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(err, db)</span> {</span>
<span class="keyword">if</span> (STATES.disconnected === conn.readyState &amp;&amp; db &amp;&amp; db.databaseName) {
conn.readyState = STATES.connected;
conn.emit(<span class="string">'reconnected'</span>)
}
})
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>fn</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span>this</span></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method private"><h3 id="drivers_node-mongodb-native_connection_NativeConnection-doOpenSet"><a href="#drivers_node-mongodb-native_connection_NativeConnection-doOpenSet">NativeConnection#doOpenSet(<code>fn</code>)</a></h3><p>Opens a connection to a MongoDB ReplicaSet.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">NativeConnection.prototype.doOpenSet = <span class="function"><span class="keyword">function</span> <span class="params">(fn)</span> {</span>
<span class="keyword">if</span> (!<span class="keyword">this</span>.db) {
<span class="keyword">var</span> servers = []
, ports = <span class="keyword">this</span>.port
, self = <span class="keyword">this</span>
<span class="keyword">this</span>.host.forEach(<span class="function"><span class="keyword">function</span> <span class="params">(host, i)</span> {</span>
servers.push(<span class="keyword">new</span> mongo.Server(host, Number(ports[i]), self.options.server));
});
<span class="keyword">var</span> server = <span class="keyword">new</span> ReplSetServers(servers, <span class="keyword">this</span>.options.replset);
<span class="keyword">this</span>.db = <span class="keyword">new</span> mongo.Db(<span class="keyword">this</span>.name, server, <span class="keyword">this</span>.options.db);
<span class="keyword">this</span>.db.on(<span class="string">'fullsetup'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
self.emit(<span class="string">'fullsetup'</span>)
});
}
<span class="keyword">this</span>.db.open(<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> fn(err);
fn();
listen(self);
});
<span class="keyword">return</span> <span class="keyword">this</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>fn</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span>this</span></li></ul></div><div class="description"><p>See description of <a href="#NativeConnection-doOpen">doOpen</a> for server options. In this case <code>options.replset</code> is also passed to ReplSetServers.</p></div><hr class="private"></div><div class="item method private"><h3 id="drivers_node-mongodb-native_connection_NativeConnection-doClose"><a href="#drivers_node-mongodb-native_connection_NativeConnection-doClose">NativeConnection#doClose(<code>fn</code>)</a></h3><p>Closes the connection</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">NativeConnection.prototype.doClose = <span class="function"><span class="keyword">function</span> <span class="params">(fn)</span> {</span>
<span class="keyword">this</span>.db.close();
<span class="keyword">if</span> (fn) fn();
<span class="keyword">return</span> <span class="keyword">this</span>;
}</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>fn</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&gt; </span><span></span></li></ul></div><div class="returns"><h4>Returns:</h4><ul><li><span class="types"> &lt;<a href="#connection_Connection">Connection</a>&gt; </span><span>this</span></li></ul></div><div class="description"></div><hr class="private"></div></li><li class="module private"><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/error.js" id="error-js">error.js</a><div class="item method private"><h3 id="error_MongooseError"><a href="#error_MongooseError">MongooseError()</a></h3><p>Mongoose error</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">MongooseError</span> <span class="params">(msg)</span> {</span>
Error.call(<span class="keyword">this</span>);
Error.captureStackTrace(<span class="keyword">this</span>, arguments.callee);
<span class="keyword">this</span>.message = msg;
<span class="keyword">this</span>.name = <span class="string">'MongooseError'</span>;
};</code></pre></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error" title="Error">Error</a></li></ul></div><div class="description"></div><hr class="private"></div></li><li class="module private"><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/errors/cast.js" id="errors-cast-js">errors/cast.js</a><div class="item method private"><h3 id="errors_cast_CastError"><a href="#errors_cast_CastError">CastError(<code>type</code>, <code>value</code>)</a></h3><p>Casting Error constructor.</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">CastError</span> <span class="params">(type, value)</span> {</span>
MongooseError.call(<span class="keyword">this</span>, <span class="string">'Cast to '</span> + type + <span class="string">' failed for value "'</span> + value + <span class="string">'"'</span>);
Error.captureStackTrace(<span class="keyword">this</span>, arguments.callee);
<span class="keyword">this</span>.name = <span class="string">'CastError'</span>;
<span class="keyword">this</span>.type = type;
<span class="keyword">this</span>.value = value;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>type</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li><li><code>value</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#error_MongooseError">MongooseError</a></li></ul></div><div class="description"></div><hr class="private"></div></li><li class="module private"><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/errors/document.js" id="errors-document-js">errors/document.js</a><div class="item method private"><h3 id="errors_document_DocumentError"><a href="#errors_document_DocumentError">DocumentError(<code>msg</code>)</a></h3><p>Document Error</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">DocumentError</span> <span class="params">(msg)</span> {</span>
MongooseError.call(<span class="keyword">this</span>, msg);
Error.captureStackTrace(<span class="keyword">this</span>, arguments.callee);
<span class="keyword">this</span>.name = <span class="string">'DocumentError'</span>;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>msg</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#error_MongooseError">MongooseError</a></li></ul></div><div class="description"></div><hr class="private"></div></li><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/errors/validation.js" id="errors-validation-js">errors/validation.js</a><div class="item method private"><h3 id="errors_validation_ValidationError"><a href="#errors_validation_ValidationError">ValidationError(<code>instance</code>)</a></h3><p>Document Validation Error</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">ValidationError</span> <span class="params">(instance)</span> {</span>
MongooseError.call(<span class="keyword">this</span>, <span class="string">"Validation failed"</span>);
Error.captureStackTrace(<span class="keyword">this</span>, arguments.callee);
<span class="keyword">this</span>.name = <span class="string">'ValidationError'</span>;
<span class="keyword">this</span>.errors = instance.errors = {};
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>instance</code><span class="types"> &lt;<a href="#document_Document">Document</a>&gt; </span><span></span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#error_MongooseError">MongooseError</a></li></ul></div><div class="description"></div><hr class="private"></div><div class="item method public"><h3 id="errors_validation_ValidationError-toString"><a href="#errors_validation_ValidationError-toString">ValidationError#toString()</a></h3><p>Console.log helper</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">ValidationError.prototype.toString = <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span>
<span class="keyword">return</span> <span class="keyword">this</span>.name + <span class="string">': '</span> + Object.keys(<span class="keyword">this</span>.errors).map(<span class="function"><span class="keyword">function</span> <span class="params">(key)</span> {</span>
<span class="keyword">return</span> String(<span class="keyword">this</span>.errors[key]);
}, <span class="keyword">this</span>).join(<span class="string">', '</span>);
};</code></pre></div><div class="description"></div><hr class=""></div></li><li class="module private"><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/errors/validator.js" id="errors-validator-js">errors/validator.js</a><div class="item method private"><h3 id="errors_validator_ValidatorError"><a href="#errors_validator_ValidatorError">ValidatorError(<code>path</code>, <code>msg</code>)</a></h3><p>Schema validator error</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">ValidatorError</span> <span class="params">(path, type)</span> {</span>
<span class="keyword">var</span> msg = type
? <span class="string">'"'</span> + type + <span class="string">'" '</span>
: <span class="string">''</span>;
MongooseError.call(<span class="keyword">this</span>, <span class="string">'Validator '</span> + msg + <span class="string">'failed for path '</span> + path);
Error.captureStackTrace(<span class="keyword">this</span>, arguments.callee);
<span class="keyword">this</span>.name = <span class="string">'ValidatorError'</span>;
<span class="keyword">this</span>.path = path;
<span class="keyword">this</span>.type = type;
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>path</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li><li><code>msg</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a>&gt; </span><span></span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#error_MongooseError">MongooseError</a></li></ul></div><div class="description"></div><hr class="private"></div></li><li class="module "><a href="https://github.com/LearnBoost/mongoose/tree/3.0.2/model.js" id="model-js">model.js</a><div class="item method public"><h3 id="model_Model"><a href="#model_Model">Model(<code>doc</code>)</a></h3><p>Model constructor</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">Model</span> <span class="params">(doc, fields, skipId)</span> {</span>
Document.call(<span class="keyword">this</span>, doc, fields, skipId);
};</code></pre></div><div class="params"><h4>Parameters:</h4><ul><li><code>doc</code><span class="types"> &lt;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">Object</a>&gt; </span><span>values to with which to create the document</span></li></ul></div><div class="inherits"><h4>Inherits:</h4><ul><li><a href="#document_Document">Document</a></li></ul></div><div class="events"><h4>Events:</h4><ul><li><p><code>error</code>: If listening to this Model event, it is emitted when a document was saved without passing a callback and an <code>error</code> occurred. If not listening, the event bubbles to the connection used to create this Model.</p></li><li><p><code>index</code>: Emitted after <code>Model#ensureIndexes</code> completes. If an error occurred it is passed with the event.</p></li></ul></div><div class="description"></div><hr class=""></div><div class="item method private"><h3 id="model_Model-_getPopulationKeys"><a href="#model_Model-_getPopulationKeys">Model#_getPopulationKeys(<code>query</code>)</a></h3><p>Returns what paths can be populated</p><span class="showcode">show code</span><div class="sourcecode"><pre><code class="javascript">Model.prototype._getPopulationKeys = <span class="function"><span class="keyword">function</span> <span class="title">getPopulationKeys</span> <span class="params">(query)</span> {</span>
<span class="keyword">if</span> (!(query &amp;&amp; query.options.populate)) <span class="keyword">return</span>;
<span class="keyword">var</span> names = Object.keys(query.options.populate)
, n = names.length
, name
, paths = {}
, ha