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
|
Schema Types
============
`SchemaType`s take care of validation, casting, defaults, and other general
options in our models. We can specify our types one of two ways:
// directly without options
var Person = new Schema({
title : String
});
// or with options
var Person = new Schema({
title : { type: String, lowercase: true }
});
In the example above we specified the `lowercase` option for strings which
will lowercase the string whenever it is set. Options are functions that are
called on each SchemaType. Each `SchemaType` has its own set of custom options.
## Available Schema Types
### String
- `lowercase`: {Boolean}
Creates a setter which calls `.toLowerCase()` on the value
- `uppercase`: {Boolean}
Creates a setter which calls `.toUpperCase()` on the value
- `trim`: {Boolean}
Creates a setter which calls `.trim()` on the value
- `match`: {RegExp}
Creates a RegExp based [validator](/docs/validation.html). The value being set is `.test()`ed
against the RegExp. If it does not pass, validation will fail.
- `enum`: {Array}
Creates an enum validator. If the value being set is not in this
array, validation will fail.
### Number
- `min`: {Number}
Creates a validator which checks that the value being set is not less
than the value specified.
- `max`: {Number}
Creates a validator which checks that the value being set is not greater
than the value specified.
### Date
- no custom options
### Boolean
- no custom options
### Buffer (v2.x only)
- no custom options
### ObjectId
To specify a type of `ObjectId`, use `Schema.ObjectId` in your declaration.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Car = new Schema({ driver: Schema.ObjectId })
- no custom options
### Mixed
An "anything goes" `SchemaType`, its flexibility comes at a trade-off of it being
harder to maintain. `Mixed` is available either through `Schema.Types.Mixed` or
by passing an empty object literal. The following are equivalent:
var Any = new Schema({ any: {} });
var Any = new Schema({ any: Schema.Types.Mixed });
Since it is a schema-less type, you can change the value to anything else
you like, but Mongoose loses the ability to auto detect/save those changes.
To "tell" Mongoose that the value of a `Mixed` type has changed, call
the `.markModified(path)` method of the document passing the path to
the `Mixed` type you just changed.
person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved
- no custom options
### Array
Creates an array of `SchemaTypes` or [Embedded Documents](/docs/embedded-documents.html).
var ToySchema = new Schema({ name: String });
var ToyBox = new Schema({
toys: [ToySchema]
, buffers: [Buffer]
, string: [String]
, numbers: [Number]
... etc
});
Note: specifying an empty array is equivalent to `[Mixed]`. The following all
create arrays of `Mixed`:
var Empty1 = new Schema({ any: [] });
var Empty2 = new Schema({ any: Array });
var Empty3 = new Schema({ any: [Schema.Types.Mixed] });
var Empty4 = new Schema({ any: [{}] });
- no custom options
## Additional options
Besides the options listed above, all SchemaTypes share the following additional
options.
- `default`: {Function|value} - Determines the default value for the path. All values are casted. If using a function, the value it returns will be casted as the default value.
- `required`: {Boolean} - If true, creates a validation rule requiring this path be set before saving occurs.
- `get`: {Function} - Adds a getter for this path. See the [getters / setters](/docs/getters-setters.html) docs for more detail.
- `set`: {Function} - Adds a setter for this path. See the [getters / setters](/docs/getters-setters.html) docs for more detail.
- `index`: {Boolean|Object} - Tells Mongoose to ensure an index is created for this path. An object can be passed as well.
var Person = new Schema({ name: String, index: true })
var Person = new Schema({ name: String, index: { unique: true }})
Note: indexes cannot be created for `Buffer` `SchemaTypes`. <br>
Note: if the index already exists on the db, it will _not_ be replaced.
- `unique`: {Boolean} - Tells Mongoose to ensure a unique index is created for this path. The following are equivalent:
var Person = new Schema({ name: String, unique: true })
var Person = new Schema({ name: String, index: { unique: true }})
Note: indexes cannot be created for `Buffer` `SchemaTypes`. <br>
Note: if the index already exists on the db, it will _not_ be replaced.
- `sparse`: {Boolean} - Tells Mongoose to ensure a sparse index is created for this path. The following are equivalent:
var Person = new Schema({ name: String, sparse: true })
var Person = new Schema({ name: String, index: { sparse: true }})
Note: indexes cannot be created for `Buffer` `SchemaTypes`. <br>
Note: if the index already exists on the db, it will _not_ be replaced.
- `validate`: {Function|RegExp|Array} - Creates a [validator](/docs/validation.html) for this path.
// passing a function
function hasNumber (v) {
return v.length && /\d/.test(v);
}
var Person = new Schema({ street: String, validate: hasNumber });
// passing a RegExp
var Person = new Schema({ street: String, validate: /\d/ });
// passing an array
var Person = new Schema({ street: String, validate: [hasNumber, 'street number required'] });
// or
var Person = new Schema({ street: String, validate: [/\d/, 'street number required'] });
For more detail about validation including async validation, see the [validation](/docs/validation.html) page.
## Alternate options definition
Instead of defining options when instanciating your `Schema` we can also
access keys through the `path` function and add options there:
Person.path('age').max(400);
Person.path('meta.birth').set(function (v) {
// this is a setter
});
Person.path('title').validate(function (v) {
return v.length > 50;
});
|