forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguide.jade
More file actions
472 lines (407 loc) · 20.2 KB
/
Copy pathguide.jade
File metadata and controls
472 lines (407 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
extends layout
block content
h2 Schemas
.important
:markdown
If you haven't yet done so, please take a minute to read the [quickstart](./index.html) to get an idea of how Mongoose works.
If you are migrating from 2.x to 3.x please take a moment to read the [migration guide](./migration.html).
:markdown
This page covers `Schema` [definition](#definition), [plugins](#plugins), instance [methods](#methods), [statics](#statics), [indexes](#indexes), [virtuals](#virtuals) and [options](#options). Let's start with `Schema` definition.
h3#definition Defining your schema
p
| Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
:js
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
p
em
| If you want to add additional keys later, use the
a(href="./api.html#schema_Schema-add") Schema#add
| method.
p
| Each key in our
code blogSchema
| defines a property in our documents which will be cast to its associated
a(href="./api.html#schematype_SchemaType") SchemaType
|. For example, we've defined a
code title
| which will be cast to the
a(href="./api.html#schema-string-js") String
| SchemaType and
code date
| which will be cast to a
code Date
| SchemaType.
| Keys may also be assigned nested objects containing further key/type definitions
em (e.g. the `meta` property above).
p
| The permitted SchemaTypes are
ul
li String
li Number
li Date
li Buffer
li Boolean
li Mixed
li ObjectId
li Array
| Read more about them
a(href="./schematypes.html") here
| .
p
| Schemas not only define the structure of your document and casting of properties, they also define document
a(href="#methods") instance methods
|, static
a(href="#statics") Model methods
|,
a(href="#indexes") compound indexes
| and document lifecycle hooks called
a(href="./middleware.html") middleware
|.
h3#plugins Pluggable
p
| Schemas are
a(href="./plugins.html") pluggable
| which allows us to package up reusable features into
a(href="http://plugins.mongoosejs.com") plugins
| that can be shared with the community or just between your projects.
h3#methods Instance methods
p
a(href="./models.html") Models
| are just fancy
code constructor
| functions. As such they can have prototype methods inherited by their instances. In the case of Mongoose, instances are
a(href="./documents.html") documents
|.
p
| Defining an instance method is easy.
:js
var animalSchema = new Schema({ name: String, type: String });
animalSchema.methods.findSimilarTypes = function (cb) {
return this.model('Animal').find({ type: this.type }, cb);
}
p
| Now all of our
code animal
| instances have a
code findSimilarTypes
| method available to it.
:js
var Animal = mongoose.model('Animal', animalSchema);
var dog = new Animal({ type: 'dog' });
dog.findSimilarTypes(function (err, dogs) {
console.log(dogs); // woof
});
h3#statics Statics
p
| Adding static constructor methods to Models is simple as well. Continuing with our
code animalSchema
|:
:js
animalSchema.statics.findByName = function (name, cb) {
this.find({ name: new RegExp(name, 'i') }, cb);
}
var Animal = mongoose.model('Animal', animalSchema);
Animal.findByName('fido', function (err, animals) {
console.log(animals);
});
h3#indexes Indexes
p
a(href="http://www.mongodb.org/display/DOCS/Indexes") Indexes
| can be defined
a(href="./api.html#schematype_SchemaType-index") at
|
a(href="./api.html#schematype_SchemaType-unique") the
|
a(href="./api.html#schematype_SchemaType-sparse") path
|
a(href="./api.html#schematype_SchemaType-expires") level
| or the
code schema
| level. Defining indexes at the schema level is necessary when defining
a(href="http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeys", target="_blank") compound indexes
|.
:js
animalSchema.index({ name: 1, type: -1 });
.important
:markdown
When your application starts up, Mongoose automatically calls `ensureIndex` for each defined index in your schema.
While nice for development, it is recommended this behavior be disabled in production since index creation can cause a [significant performance impact](http://docs.mongodb.org/manual/core/indexes/#index-creation-operations). Disable the behavior by setting the `autoIndex` option of your schema to `false`.
:js
animalSchema.set('autoIndex', false);
// or
new Schema({..}, { autoIndex: false });
:markdown
See also the [Model#ensureIndexes](./api.html#model_Model.ensureIndexes) method.
h3#virtuals Virtuals
:markdown
[Virtual](./api.html#schema_Schema-virtual) attributes are attributes that are convenient to have around but that do not get persisted to MongoDB.
:js
var personSchema = new Schema({
name: {
first: String,
last: String
}
});
var Person = mongoose.model('Person', personSchema);
var bad = new Person({
name: { first: 'Walter', last: 'White' }
});
:markdown
Suppose we want to log the full name of `bad`. We could do this manually like so:
:js
console.log(bad.name.first + ' ' + bad.name.last); // Walter White
:markdown
Or we could add a `virtual` attribute [getter](./api.html#virtualtype_VirtualType-get) to our `personSchema` so we don't need to write out this string concatenation mess each time:
:js
personSchema.virtual('name.full').get(function () {
return this.name.first + ' ' + this.name.last;
});
:markdown
Now, when we access our virtual full name property, our getter function will be invoked and the value returned:
:js
console.log('%s is insane', bad.name.full); // Walter White is insane
:markdown
It would also be nice to be able to set `this.name.first` and `this.name.last` by setting `this.name.full`. For example, if we wanted to change `bad`'s `name.first` and `name.last` to 'Breaking' and 'Bad' respectively, it'd be nice to just:
:js
bad.name.full = 'Breaking Bad';
:markdown
Mongoose let's you do this as well through its virtual attribute [setters](./api.html#virtualtype_VirtualType-set):
:js
personSchema.virtual('name.full').set(function (name) {
var split = name.split(' ');
this.name.first = split[0];
this.name.last = split[1];
});
...
mad.name.full = 'Breaking Bad';
console.log(mad.name.first); // Breaking
:markdown
If you need attributes that you can get and set but that are not themselves persisted to MongoDB, virtual attributes is the Mongoose feature for you.
h3#options Options
:markdown
`Schema`s have a few configurable options which can be passed to the constructor or `set` directly:
:js
new Schema({..}, options);
// or
var schema = new Schema({..});
schema.set(option, value);
:markdown
Valid options:
- [autoIndex](#autoIndex)
- [capped](#capped)
- [collection](#collection)
- [id](#id)
- [_id](#_id)
- [read](#read)
- [safe](#safe)
- [shardKey](#shardKey)
- [strict](#strict)
- [toJSON](#toJSON)
- [toObject](#toObject)
- [versionKey](#versionKey)
h4#autoIndex option: autoIndex
:markdown
At application startup, Mongoose sends an `ensureIndex` command for each index declared in your `Schema`. As of Mongoose v3, indexes are created in the `background` by default. If you wish to disable the auto-creation feature and manually handle when indexes are created, set your `Schema`s `autoIndex` option to `false` and use the [ensureIndexes](./api.html#model_Model.ensureIndexes) method on your model.
:js
var schema = new Schema({..}, { autoIndex: false });
var Clock = mongoose.model('Clock', schema);
Clock.ensureIndexes(callback);
h4#capped option: capped
:markdown
Mongoose supports MongoDBs [capped](http://www.mongodb.org/display/DOCS/Capped+Collections) collections. To specify the underlying MongoDB collection be `capped`, set the `capped` option to the maximum size of the collection in [bytes](http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-size.).
:js
new Schema({..}, { capped: 1024 });
:markdown
The `capped` option may also be set to an object if you want to pass additional options like [max](http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-max) or [autoIndexId](http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-autoIndexId). In this case you must explicitly pass the `size` option which is required.
:js
new Schema({..}, { capped: { size: 1024, max: 1000, autoIndexId: true } });
h4#collection option: collection
:markdown
Mongoose by default produces a collection name by passing the model name to the [utils.toCollectionName](./api.html#utils_exports.toCollectionName) method. This method pluralizes the name. Set this option if you need a different name for your collection.
:js
var dataSchema = new Schema({..}, { collection: 'data' });
h4#id option: id
:markdown
Mongoose assigns each of your schemas an `id` virtual getter by default which returns the documents `_id` field cast to a string, or in the case of ObjectIds, its hexString. If you don't want an `id` getter added to your schema, you may disable it passing this option at schema construction time.
:js
// default behavior
var schema = new Schema({ name: String });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p.id); // '50341373e894ad16347efe01'
// disabled id
var schema = new Schema({ name: String }, { id: false });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p.id); // undefined
h4#_id option: _id
:markdown
Mongoose assigns each of your schemas an `_id` field by default if one is not passed into the [Schema](/docs/api.html#schema-js) constructor. The type assiged is an [ObjectId](/docs/api.html#schema_Schema.Types) to coincide with MongoDBs default behavior. If you don't want an `_id` added to your schema at all, you may disable it using this option.
Pass this option during schema construction to prevent documents from getting an `_id` auto created by Mongoose (parent documents will still have an `_id` created by MongoDB when inserted).
:js
// default behavior
var schema = new Schema({ name: String });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p); // { _id: '50341373e894ad16347efe01', name: 'mongodb.org' }
// disabled _id
var schema = new Schema({ name: String }, { _id: false });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p); // { name: 'mongodb.org' }
// MongoDB will create the _id when inserted
p.save(function (err) {
if (err) return handleError(err);
Page.findById(p, function (err, doc) {
if (err) return handleError(err);
console.log(doc); // { name: 'mongodb.org', _id: '50341373e894ad16347efe12' }
})
})
h4#read option: read
:markdown
Allows setting [query#read](/docs/api.html#query_Query-read) options at the schema level, providing us a way to apply default [ReadPreferences](http://docs.mongodb.org/manual/applications/replication/#replica-set-read-preference) to all queries derived from a model.
:js
var schema = new Schema({..}, { read: 'primary' }); // also aliased as 'p'
var schema = new Schema({..}, { read: 'primaryPreferred' }); // aliased as 'pp'
var schema = new Schema({..}, { read: 'secondary' }); // aliased as 's'
var schema = new Schema({..}, { read: 'secondaryPreferred' }); // aliased as 'sp'
var schema = new Schema({..}, { read: 'nearest' }); // aliased as 'n'
:markdown
The alias of each pref is also permitted so instead of having to type out 'secondaryPreferred' and getting the spelling wrong, we can simply pass 'sp'.
The read option also allows us to specify _tag sets_. These tell the [driver](https://github.com/mongodb/node-mongodb-native/) from which members of the replica-set it should attempt to read. Read more about tag sets [here](http://docs.mongodb.org/manual/applications/replication/#tag-sets) and [here](http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences).
_NOTE: if you specify the read pref 'nearest', you must also pass the [strategy](http://mongodb.github.com/node-mongodb-native/api-generated/replset.html?highlight=strategy) option when connecting or your reads will not behave predictably:_
:js
// pings the replset members periodically to track network latency
// now `nearest` works as intended
var options = { replset: { strategy: 'ping' }};
mongoose.connect(uri, options);
var schema = new Schema({..}, { read: ['n', { disk: 'ssd' }] });
mongoose.model('JellyBean', schema);
h4#safe option: safe
:markdown
This option is passed to MongoDB with all operations and let's us specify if errors should be returned to our callbacks as well as tune write behavior.
:js
var safe = true;
new Schema({ .. }, { safe: safe });
:markdown
By default this is set to `true` for all schemas which guarentees that any occurring error gets passed back to our callback.
By setting `safe` to something else like `{ j: 1, w: 2, wtimeout: 10000 }` we can guarantee the write was committed to the MongoDB journal (j: 1), at least 2 replicas (w: 2), and that the write will timeout if it takes longer than 10 seconds (wtimeout: 10000). Errors will still be passed to our callback.
**NOTE: this setting overrides any setting specified by passing db options while [creating a connection](/docs/api.html#index_Mongoose-createConnection).
There are other write concerns like `{ w: "majority" }` too. See the MongoDB [docs](http://www.mongodb.org/display/DOCS/getLastError+Command) for more details.
:js
var safe = { w: "majority", wtimeout: 10000 };
new Schema({ .. }, { safe: safe });
h4#shardKey option: shardKey
:markdown
The `shardKey` option is used when we have a [sharded MongoDB architecture](http://www.mongodb.org/display/DOCS/Sharding+Introduction). Each sharded collection is given a shard key which must be present in all insert/update operations. We just need to set this schema option to the same shard key and we’ll be all set.
:js
new Schema({ .. }, { shardkey: { tag: 1, name: 1 }})
:markdown
_Note that Mongoose does not send the `shardcollection` command for you. You must configure your shards yourself._
h4#strict option: strict
:markdown
The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not get saved to the db.
:js
var thingSchema = new Schema({..})
var Thing = mongoose.model('Thing', schemaSchema);
var thing = new Thing({ iAmNotInTheSchema: true });
thing.save(); // iAmNotInTheSchema is not saved to the db
// set to false..
var thingSchema = new Schema({..}, { strict: false });
var thing = new Thing({ iAmNotInTheSchema: true });
thing.save(); // iAmNotInTheSchema is now saved to the db!!
:markdown
This also affects the use of `doc.set()` to set a property value.
:js
var thingSchema = new Schema({..})
var Thing = mongoose.model('Thing', schemaSchema);
var thing = new Thing;
thing.set('iAmNotInTheSchema', true);
thing.save(); // iAmNotInTheSchema is not saved to the db
:markdown
This value can be overridden at the model instance level by passing a second boolean argument:
:js
var Thing = mongoose.model('Thing');
var thing = new Thing(doc, true); // enables strict mode
var thing = new Thing(doc, false); // disables strict mode
:markdown
The `strict` option may also be set to `"throw"` which will cause errors to be produced instead of dropping the bad data.
:markdown
_NOTE: do not set to false unless you have good reason._
_NOTE: in mongoose v2 the default was false._
_NOTE: Any key/val set on the instance that does not exist in your schema is always ignored, regardless of schema option._
:js
var thingSchema = new Schema({..})
var Thing = mongoose.model('Thing', schemaSchema);
var thing = new Thing;
thing.iAmNotInTheSchema = true;
thing.save(); // iAmNotInTheSchema is never saved to the db
h4#toJSON option: toJSON
:markdown
Exactly the same as the [toObject](#toObject) option but only applies when the documents `toJSON` method is called.
:js
var schema = new Schema({ name: String });
schema.path('name').get(function (v) {
return v + ' is my name';
});
schema.set('toJSON', { getters: true, virtuals: false });
var M = mongoose.model('Person', schema);
var m = new M({ name: 'Max Headroom' });
console.log(m.toObject()); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom' }
console.log(m.toJSON()); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom is my name' }
// since we know toJSON is called whenever a js object is stringified:
console.log(JSON.stringify(m)); // { "_id": "504e0cd7dd992d9be2f20b6f", "name": "Max Headroom is my name" }
:markdown
To see all available `toJSON/toObject` options, read [this](/docs/api.html#document_Document-toObject).
h4#toObject option: toObject
:markdown
Documents have a [toObject](/docs/api.html#document_Document-toObject) method which converts the mongoose document into a plain javascript object. This method accepts a few options. Instead of applying these options on a per-document basis we may declare the options here and have it applied to all of this schemas documents by default.
To have all virtuals show up in your `console.log` output, set the `toObject` option to `{ getters: true }`:
:js
var schema = new Schema({ name: String });
schema.path('name').get(function (v) {
return v + ' is my name';
});
schema.set('toObject', { getters: true });
var M = mongoose.model('Person', schema);
var m = new M({ name: 'Max Headroom' });
console.log(m); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom is my name' }
:markdown
To see all available `toObject` options, read [this](/docs/api.html#document_Document-toObject).
h4#versionKey option: versionKey
:markdown
The `versionKey` is a property set on each document when first created by Mongoose. This keys value contains the internal [revision](http://aaronheckmann.posterous.com/mongoose-v3-part-1-versioning) of the document. The name of this document property is configurable. The default is `__v`. If this conflicts with your application you can configure as such:
:js
var schema = new Schema({ name: 'string' });
var Thing = mongoose.model('Thing', schema);
var thing = new Thing({ name: 'mongoose v3' });
thing.save(); // { __v: 0, name: 'mongoose v3' }
// customized versionKey
new Schema({..}, { versionKey: '_somethingElse' })
var Thing = mongoose.model('Thing', schema);
var thing = new Thing({ name: 'mongoose v3' });
thing.save(); // { _somethingElse: 0, name: 'mongoose v3' }
:markdown
Document versioning can also be disabled by setting the `versionKey` to false. _DO NOT disable versioning unless you [know what you are doing](http://aaronheckmann.posterous.com/mongoose-v3-part-1-versioning)._
:js
new Schema({..}, { versionKey: false });
var Thing = mongoose.model('Thing', schema);
var thing = new Thing({ name: 'no versioning please' });
thing.save(); // { name: 'no versioning please' }
h3#next Next Up
:markdown
Now that we've covered `Schemas`, let's take a look at [SchemaTypes](/docs/schematypes.html).
script.
document.body.className = 'load';
include includes/googleanalytics