mongoose 6.2.7 → 6.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +35 -26
- package/CHANGELOG.md +27 -0
- package/dist/browser.umd.js +71 -71
- package/lib/aggregate.js +36 -36
- package/lib/browser.js +4 -4
- package/lib/browserDocument.js +1 -0
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -3
- package/lib/cursor/QueryCursor.js +3 -3
- package/lib/document.js +75 -47
- package/lib/error/index.js +2 -2
- package/lib/helpers/document/handleSpreadDoc.js +19 -1
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/query/castFilterPath.js +0 -1
- package/lib/index.js +24 -26
- package/lib/model.js +141 -136
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +9 -2
- package/lib/options/SchemaDocumentArrayOptions.js +3 -3
- package/lib/options/SchemaMapOptions.js +2 -2
- package/lib/options/SchemaNumberOptions.js +3 -3
- package/lib/options/SchemaObjectIdOptions.js +2 -2
- package/lib/options/SchemaStringOptions.js +1 -1
- package/lib/options/SchemaSubdocumentOptions.js +2 -2
- package/lib/options/SchemaTypeOptions.js +3 -3
- package/lib/query.js +253 -225
- package/lib/schema/SubdocumentPath.js +6 -3
- package/lib/schema/array.js +3 -2
- package/lib/schema/boolean.js +4 -4
- package/lib/schema/buffer.js +3 -3
- package/lib/schema/date.js +7 -7
- package/lib/schema/decimal128.js +2 -2
- package/lib/schema/documentarray.js +17 -10
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +6 -6
- package/lib/schema/objectid.js +4 -4
- package/lib/schema/string.js +14 -14
- package/lib/schema.js +28 -28
- package/lib/schematype.js +78 -68
- package/lib/types/ArraySubdocument.js +1 -1
- package/lib/types/DocumentArray/methods/index.js +2 -2
- package/lib/types/array/index.js +1 -1
- package/lib/types/array/methods/index.js +14 -12
- package/lib/types/buffer.js +1 -1
- package/lib/types/decimal128.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +2 -2
- package/lib/virtualtype.js +4 -3
- package/package.json +18 -17
- package/tools/repl.js +8 -8
- package/tools/sharded.js +3 -3
- package/types/connection.d.ts +116 -116
- package/types/document.d.ts +3 -0
- package/types/error.d.ts +2 -2
- package/types/index.d.ts +75 -67
- package/types/pipelinestage.d.ts +194 -194
- package/types/schemaoptions.d.ts +2 -2
package/lib/schema.js
CHANGED
|
@@ -38,7 +38,7 @@ let id = 0;
|
|
|
38
38
|
/**
|
|
39
39
|
* Schema constructor.
|
|
40
40
|
*
|
|
41
|
-
* ####Example:
|
|
41
|
+
* #### Example:
|
|
42
42
|
*
|
|
43
43
|
* const child = new Schema({ name: String });
|
|
44
44
|
* const schema = new Schema({ name: String, age: Number, children: [child] });
|
|
@@ -47,7 +47,7 @@ let id = 0;
|
|
|
47
47
|
* // setting schema options
|
|
48
48
|
* new Schema({ name: String }, { _id: false, autoIndex: false })
|
|
49
49
|
*
|
|
50
|
-
* ####Options:
|
|
50
|
+
* #### Options:
|
|
51
51
|
*
|
|
52
52
|
* - [autoIndex](/docs/guide.html#autoIndex): bool - defaults to null (which means use the connection's autoIndex option)
|
|
53
53
|
* - [autoCreate](/docs/guide.html#autoCreate): bool - defaults to null (which means use the connection's autoCreate option)
|
|
@@ -77,10 +77,10 @@ let id = 0;
|
|
|
77
77
|
* - [timestamps](/docs/guide.html#timestamps): object or boolean - defaults to `false`. If true, Mongoose adds `createdAt` and `updatedAt` properties to your schema and manages those properties for you.
|
|
78
78
|
* - [pluginTags](/docs/guide.html#pluginTags): array of strings - defaults to `undefined`. If set and plugin called with `tags` option, will only apply that plugin to schemas with a matching tag.
|
|
79
79
|
*
|
|
80
|
-
* ####Options for Nested Schemas:
|
|
80
|
+
* #### Options for Nested Schemas:
|
|
81
81
|
* - `excludeIndexes`: bool - defaults to `false`. If `true`, skip building indexes on this schema's paths.
|
|
82
82
|
*
|
|
83
|
-
* ####Note:
|
|
83
|
+
* #### Note:
|
|
84
84
|
*
|
|
85
85
|
* _When nesting schemas, (`children` in the example above), always declare the child schema first before passing it into its parent._
|
|
86
86
|
*
|
|
@@ -231,7 +231,7 @@ Object.defineProperty(Schema.prototype, 'childSchemas', {
|
|
|
231
231
|
* This property is typically only useful for plugin authors and advanced users.
|
|
232
232
|
* You do not need to interact with this property at all to use mongoose.
|
|
233
233
|
*
|
|
234
|
-
* ####Example:
|
|
234
|
+
* #### Example:
|
|
235
235
|
* const schema = new Schema({});
|
|
236
236
|
* schema.virtual('answer').get(() => 42);
|
|
237
237
|
*
|
|
@@ -253,7 +253,7 @@ Object.defineProperty(Schema.prototype, 'virtuals', {
|
|
|
253
253
|
/**
|
|
254
254
|
* The original object passed to the schema constructor
|
|
255
255
|
*
|
|
256
|
-
* ####Example:
|
|
256
|
+
* #### Example:
|
|
257
257
|
*
|
|
258
258
|
* const schema = new Schema({ a: String }).add({ b: String });
|
|
259
259
|
* schema.obj; // { a: String }
|
|
@@ -270,7 +270,7 @@ Schema.prototype.obj;
|
|
|
270
270
|
* The paths defined on this schema. The keys are the top-level paths
|
|
271
271
|
* in this schema, and the values are instances of the SchemaType class.
|
|
272
272
|
*
|
|
273
|
-
* ####Example:
|
|
273
|
+
* #### Example:
|
|
274
274
|
* const schema = new Schema({ name: String }, { _id: false });
|
|
275
275
|
* schema.paths; // { name: SchemaString { ... } }
|
|
276
276
|
*
|
|
@@ -288,7 +288,7 @@ Schema.prototype.paths;
|
|
|
288
288
|
/**
|
|
289
289
|
* Schema as a tree
|
|
290
290
|
*
|
|
291
|
-
* ####Example:
|
|
291
|
+
* #### Example:
|
|
292
292
|
* {
|
|
293
293
|
* '_id' : ObjectId
|
|
294
294
|
* , 'nested' : {
|
|
@@ -307,7 +307,7 @@ Schema.prototype.tree;
|
|
|
307
307
|
/**
|
|
308
308
|
* Returns a deep copy of the schema
|
|
309
309
|
*
|
|
310
|
-
* ####Example:
|
|
310
|
+
* #### Example:
|
|
311
311
|
*
|
|
312
312
|
* const schema = new Schema({ name: String });
|
|
313
313
|
* const clone = schema.clone();
|
|
@@ -381,7 +381,7 @@ Schema.prototype._clone = function _clone(Constructor) {
|
|
|
381
381
|
*
|
|
382
382
|
* This method is analagous to [Lodash's `pick()` function](https://lodash.com/docs/4.17.15#pick) for Mongoose schemas.
|
|
383
383
|
*
|
|
384
|
-
* ####Example:
|
|
384
|
+
* #### Example:
|
|
385
385
|
*
|
|
386
386
|
* const schema = Schema({ name: String, age: Number });
|
|
387
387
|
* // Creates a new schema with the same `name` path as `schema`,
|
|
@@ -472,7 +472,7 @@ Schema.prototype.defaultOptions = function(options) {
|
|
|
472
472
|
/**
|
|
473
473
|
* Adds key path / schema type pairs to this schema.
|
|
474
474
|
*
|
|
475
|
-
* ####Example:
|
|
475
|
+
* #### Example:
|
|
476
476
|
*
|
|
477
477
|
* const ToySchema = new Schema();
|
|
478
478
|
* ToySchema.add({ name: 'string', color: 'string', price: 'number' });
|
|
@@ -646,7 +646,7 @@ reserved.collection = 1;
|
|
|
646
646
|
* Sets a path (if arity 2)
|
|
647
647
|
* Gets a path (if arity 1)
|
|
648
648
|
*
|
|
649
|
-
* ####Example
|
|
649
|
+
* #### Example
|
|
650
650
|
*
|
|
651
651
|
* schema.path('name') // returns a SchemaType
|
|
652
652
|
* schema.path('name', Number) // changes the schemaType of `name` to Number
|
|
@@ -1133,7 +1133,7 @@ function createMapNestedSchemaType(schema, schemaType, path, obj, options) {
|
|
|
1133
1133
|
*
|
|
1134
1134
|
* The callback is passed the pathname and the schemaType instance.
|
|
1135
1135
|
*
|
|
1136
|
-
* ####Example:
|
|
1136
|
+
* #### Example:
|
|
1137
1137
|
*
|
|
1138
1138
|
* const userSchema = new Schema({ name: String, registeredAt: Date });
|
|
1139
1139
|
* userSchema.eachPath((pathname, schematype) => {
|
|
@@ -1162,7 +1162,7 @@ Schema.prototype.eachPath = function(fn) {
|
|
|
1162
1162
|
/**
|
|
1163
1163
|
* Returns an Array of path strings that are required by this schema.
|
|
1164
1164
|
*
|
|
1165
|
-
* ####Example:
|
|
1165
|
+
* #### Example:
|
|
1166
1166
|
* const s = new Schema({
|
|
1167
1167
|
* name: { type: String, required: true },
|
|
1168
1168
|
* age: { type: String, required: true },
|
|
@@ -1214,7 +1214,7 @@ Schema.prototype.indexedPaths = function indexedPaths() {
|
|
|
1214
1214
|
*
|
|
1215
1215
|
* Given a path, returns whether it is a real, virtual, nested, or ad-hoc/undefined path.
|
|
1216
1216
|
*
|
|
1217
|
-
* ####Example:
|
|
1217
|
+
* #### Example:
|
|
1218
1218
|
* const s = new Schema({ name: String, nested: { foo: String } });
|
|
1219
1219
|
* s.virtual('foo').get(() => 42);
|
|
1220
1220
|
* s.pathType('name'); // "real"
|
|
@@ -1371,7 +1371,7 @@ function getPositionalPath(self, path) {
|
|
|
1371
1371
|
/**
|
|
1372
1372
|
* Adds a method call to the queue.
|
|
1373
1373
|
*
|
|
1374
|
-
* ####Example:
|
|
1374
|
+
* #### Example:
|
|
1375
1375
|
*
|
|
1376
1376
|
* schema.methods.print = function() { console.log(this); };
|
|
1377
1377
|
* schema.queue('print', []); // Print the doc every one is instantiated
|
|
@@ -1392,7 +1392,7 @@ Schema.prototype.queue = function(name, args) {
|
|
|
1392
1392
|
/**
|
|
1393
1393
|
* Defines a pre hook for the model.
|
|
1394
1394
|
*
|
|
1395
|
-
* ####Example
|
|
1395
|
+
* #### Example
|
|
1396
1396
|
*
|
|
1397
1397
|
* const toySchema = new Schema({ name: String, created: Date });
|
|
1398
1398
|
*
|
|
@@ -1514,7 +1514,7 @@ Schema.prototype.post = function(name) {
|
|
|
1514
1514
|
/**
|
|
1515
1515
|
* Registers a plugin for this schema.
|
|
1516
1516
|
*
|
|
1517
|
-
* ####Example:
|
|
1517
|
+
* #### Example:
|
|
1518
1518
|
*
|
|
1519
1519
|
* const s = new Schema({ name: String });
|
|
1520
1520
|
* s.plugin(schema => console.log(schema.path('name').path));
|
|
@@ -1548,7 +1548,7 @@ Schema.prototype.plugin = function(fn, opts) {
|
|
|
1548
1548
|
/**
|
|
1549
1549
|
* Adds an instance method to documents constructed from Models compiled from this schema.
|
|
1550
1550
|
*
|
|
1551
|
-
* ####Example
|
|
1551
|
+
* #### Example
|
|
1552
1552
|
*
|
|
1553
1553
|
* const schema = kittySchema = new Schema(..);
|
|
1554
1554
|
*
|
|
@@ -1595,7 +1595,7 @@ Schema.prototype.method = function(name, fn, options) {
|
|
|
1595
1595
|
/**
|
|
1596
1596
|
* Adds static "class" methods to Models compiled from this schema.
|
|
1597
1597
|
*
|
|
1598
|
-
* ####Example
|
|
1598
|
+
* #### Example
|
|
1599
1599
|
*
|
|
1600
1600
|
* const schema = new Schema(..);
|
|
1601
1601
|
* // Equivalent to `schema.statics.findByName = function(name) {}`;
|
|
@@ -1628,7 +1628,7 @@ Schema.prototype.static = function(name, fn) {
|
|
|
1628
1628
|
/**
|
|
1629
1629
|
* Defines an index (most likely compound) for this schema.
|
|
1630
1630
|
*
|
|
1631
|
-
* ####Example
|
|
1631
|
+
* #### Example
|
|
1632
1632
|
*
|
|
1633
1633
|
* schema.index({ first: 1, last: -1 })
|
|
1634
1634
|
*
|
|
@@ -1653,7 +1653,7 @@ Schema.prototype.index = function(fields, options) {
|
|
|
1653
1653
|
/**
|
|
1654
1654
|
* Sets a schema option.
|
|
1655
1655
|
*
|
|
1656
|
-
* ####Example
|
|
1656
|
+
* #### Example
|
|
1657
1657
|
*
|
|
1658
1658
|
* schema.set('strict'); // 'true' by default
|
|
1659
1659
|
* schema.set('strict', false); // Sets 'strict' to false
|
|
@@ -1702,7 +1702,7 @@ Schema.prototype.set = function(key, value, _tags) {
|
|
|
1702
1702
|
/**
|
|
1703
1703
|
* Gets a schema option.
|
|
1704
1704
|
*
|
|
1705
|
-
* ####Example:
|
|
1705
|
+
* #### Example:
|
|
1706
1706
|
*
|
|
1707
1707
|
* schema.get('strict'); // true
|
|
1708
1708
|
* schema.set('strict', false);
|
|
@@ -1740,7 +1740,7 @@ Object.defineProperty(Schema, 'indexTypes', {
|
|
|
1740
1740
|
* Returns a list of indexes that this schema declares, via `schema.index()` or by `index: true` in a path's options.
|
|
1741
1741
|
* Indexes are expressed as an array `[spec, options]`.
|
|
1742
1742
|
*
|
|
1743
|
-
* ####Example:
|
|
1743
|
+
* #### Example:
|
|
1744
1744
|
*
|
|
1745
1745
|
* const userSchema = new Schema({
|
|
1746
1746
|
* email: { type: String, required: true, unique: true },
|
|
@@ -1902,7 +1902,7 @@ Schema.prototype.virtualpath = function(name) {
|
|
|
1902
1902
|
/**
|
|
1903
1903
|
* Removes the given `path` (or [`paths`]).
|
|
1904
1904
|
*
|
|
1905
|
-
* ####Example:
|
|
1905
|
+
* #### Example:
|
|
1906
1906
|
*
|
|
1907
1907
|
* const schema = new Schema({ name: String, age: Number });
|
|
1908
1908
|
* schema.remove('name');
|
|
@@ -1969,7 +1969,7 @@ function _deletePath(schema, name) {
|
|
|
1969
1969
|
* [statics](/docs/guide.html#statics), and
|
|
1970
1970
|
* [methods](/docs/guide.html#methods).
|
|
1971
1971
|
*
|
|
1972
|
-
* ####Example:
|
|
1972
|
+
* #### Example:
|
|
1973
1973
|
*
|
|
1974
1974
|
* ```javascript
|
|
1975
1975
|
* const md5 = require('md5');
|
|
@@ -2232,12 +2232,12 @@ module.exports = exports = Schema;
|
|
|
2232
2232
|
/**
|
|
2233
2233
|
* The various built-in Mongoose Schema Types.
|
|
2234
2234
|
*
|
|
2235
|
-
* ####Example:
|
|
2235
|
+
* #### Example:
|
|
2236
2236
|
*
|
|
2237
2237
|
* const mongoose = require('mongoose');
|
|
2238
2238
|
* const ObjectId = mongoose.Schema.Types.ObjectId;
|
|
2239
2239
|
*
|
|
2240
|
-
* ####Types:
|
|
2240
|
+
* #### Types:
|
|
2241
2241
|
*
|
|
2242
2242
|
* - [String](/docs/schematypes.html#strings)
|
|
2243
2243
|
* - [Number](/docs/schematypes.html#numbers)
|
package/lib/schematype.js
CHANGED
|
@@ -22,11 +22,13 @@ const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
|
|
|
22
22
|
const CastError = MongooseError.CastError;
|
|
23
23
|
const ValidatorError = MongooseError.ValidatorError;
|
|
24
24
|
|
|
25
|
+
const setOptionsForDefaults = { _skipMarkModified: true };
|
|
26
|
+
|
|
25
27
|
/**
|
|
26
28
|
* SchemaType constructor. Do **not** instantiate `SchemaType` directly.
|
|
27
29
|
* Mongoose converts your schema paths into SchemaTypes automatically.
|
|
28
30
|
*
|
|
29
|
-
* ####Example:
|
|
31
|
+
* #### Example:
|
|
30
32
|
*
|
|
31
33
|
* const schema = new Schema({ name: String });
|
|
32
34
|
* schema.path('name') instanceof SchemaType; // true
|
|
@@ -131,7 +133,7 @@ SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;
|
|
|
131
133
|
/**
|
|
132
134
|
* The path to this SchemaType in a Schema.
|
|
133
135
|
*
|
|
134
|
-
* ####Example:
|
|
136
|
+
* #### Example:
|
|
135
137
|
* const schema = new Schema({ name: String });
|
|
136
138
|
* schema.path('name').path; // 'name'
|
|
137
139
|
*
|
|
@@ -145,7 +147,7 @@ SchemaType.prototype.path;
|
|
|
145
147
|
/**
|
|
146
148
|
* The validators that Mongoose should run to validate properties at this SchemaType's path.
|
|
147
149
|
*
|
|
148
|
-
* ####Example:
|
|
150
|
+
* #### Example:
|
|
149
151
|
* const schema = new Schema({ name: { type: String, required: true } });
|
|
150
152
|
* schema.path('name').validators.length; // 1, the `required` validator
|
|
151
153
|
*
|
|
@@ -159,7 +161,7 @@ SchemaType.prototype.validators;
|
|
|
159
161
|
/**
|
|
160
162
|
* True if this SchemaType has a required validator. False otherwise.
|
|
161
163
|
*
|
|
162
|
-
* ####Example:
|
|
164
|
+
* #### Example:
|
|
163
165
|
* const schema = new Schema({ name: { type: String, required: true } });
|
|
164
166
|
* schema.path('name').isRequired; // true
|
|
165
167
|
*
|
|
@@ -192,7 +194,7 @@ SchemaType.prototype.splitPath = function() {
|
|
|
192
194
|
/**
|
|
193
195
|
* Get/set the function used to cast arbitrary values to this type.
|
|
194
196
|
*
|
|
195
|
-
* ####Example:
|
|
197
|
+
* #### Example:
|
|
196
198
|
*
|
|
197
199
|
* // Disallow `null` for numbers, and don't try to cast any values to
|
|
198
200
|
* // numbers, so even strings like '123' will cause a CastError.
|
|
@@ -225,7 +227,7 @@ SchemaType.cast = function cast(caster) {
|
|
|
225
227
|
* Get/set the function used to cast arbitrary values to this particular schematype instance.
|
|
226
228
|
* Overrides `SchemaType.cast()`.
|
|
227
229
|
*
|
|
228
|
-
* ####Example:
|
|
230
|
+
* #### Example:
|
|
229
231
|
*
|
|
230
232
|
* // Disallow `null` for numbers, and don't try to cast any values to
|
|
231
233
|
* // numbers, so even strings like '123' will cause a CastError.
|
|
@@ -271,7 +273,7 @@ SchemaType.prototype.cast = function cast() {
|
|
|
271
273
|
/**
|
|
272
274
|
* Sets a default option for this schema type.
|
|
273
275
|
*
|
|
274
|
-
* ####Example:
|
|
276
|
+
* #### Example:
|
|
275
277
|
*
|
|
276
278
|
* // Make all strings be trimmed by default
|
|
277
279
|
* mongoose.SchemaTypes.String.set('trim', true);
|
|
@@ -295,7 +297,7 @@ SchemaType.set = function set(option, value) {
|
|
|
295
297
|
/**
|
|
296
298
|
* Attaches a getter for all instances of this schema type.
|
|
297
299
|
*
|
|
298
|
-
* ####Example:
|
|
300
|
+
* #### Example:
|
|
299
301
|
*
|
|
300
302
|
* // Make all numbers round down
|
|
301
303
|
* mongoose.Number.get(function(v) { return Math.floor(v); });
|
|
@@ -316,7 +318,7 @@ SchemaType.get = function(getter) {
|
|
|
316
318
|
/**
|
|
317
319
|
* Sets a default value for this SchemaType.
|
|
318
320
|
*
|
|
319
|
-
* ####Example:
|
|
321
|
+
* #### Example:
|
|
320
322
|
*
|
|
321
323
|
* const schema = new Schema({ n: { type: Number, default: 10 })
|
|
322
324
|
* const M = db.model('M', schema)
|
|
@@ -325,7 +327,7 @@ SchemaType.get = function(getter) {
|
|
|
325
327
|
*
|
|
326
328
|
* Defaults can be either `functions` which return the value to use as the default or the literal value itself. Either way, the value will be cast based on its schema type before being set during document creation.
|
|
327
329
|
*
|
|
328
|
-
* ####Example:
|
|
330
|
+
* #### Example:
|
|
329
331
|
*
|
|
330
332
|
* // values are cast:
|
|
331
333
|
* const schema = new Schema({ aNumber: { type: Number, default: 4.815162342 }})
|
|
@@ -379,7 +381,7 @@ SchemaType.prototype.default = function(val) {
|
|
|
379
381
|
/**
|
|
380
382
|
* Declares the index options for this schematype.
|
|
381
383
|
*
|
|
382
|
-
* ####Example:
|
|
384
|
+
* #### Example:
|
|
383
385
|
*
|
|
384
386
|
* const s = new Schema({ name: { type: String, index: true })
|
|
385
387
|
* const s = new Schema({ loc: { type: [Number], index: 'hashed' })
|
|
@@ -390,7 +392,7 @@ SchemaType.prototype.default = function(val) {
|
|
|
390
392
|
* s.path('my.date').index({ expires: 60 });
|
|
391
393
|
* s.path('my.path').index({ unique: true, sparse: true });
|
|
392
394
|
*
|
|
393
|
-
* ####
|
|
395
|
+
* #### Note:
|
|
394
396
|
*
|
|
395
397
|
* _Indexes are created [in the background](https://docs.mongodb.com/manual/core/index-creation/#index-creation-background)
|
|
396
398
|
* by default. If `background` is set to `false`, MongoDB will not execute any
|
|
@@ -411,7 +413,7 @@ SchemaType.prototype.index = function(options) {
|
|
|
411
413
|
/**
|
|
412
414
|
* Declares an unique index.
|
|
413
415
|
*
|
|
414
|
-
* ####Example:
|
|
416
|
+
* #### Example:
|
|
415
417
|
*
|
|
416
418
|
* const s = new Schema({ name: { type: String, unique: true }});
|
|
417
419
|
* s.path('name').index({ unique: true });
|
|
@@ -449,7 +451,7 @@ SchemaType.prototype.unique = function(bool) {
|
|
|
449
451
|
/**
|
|
450
452
|
* Declares a full text index.
|
|
451
453
|
*
|
|
452
|
-
* ###Example:
|
|
454
|
+
* ### Example:
|
|
453
455
|
*
|
|
454
456
|
* const s = new Schema({name : {type: String, text : true })
|
|
455
457
|
* s.path('name').index({text : true});
|
|
@@ -485,7 +487,7 @@ SchemaType.prototype.text = function(bool) {
|
|
|
485
487
|
/**
|
|
486
488
|
* Declares a sparse index.
|
|
487
489
|
*
|
|
488
|
-
* ####Example:
|
|
490
|
+
* #### Example:
|
|
489
491
|
*
|
|
490
492
|
* const s = new Schema({ name: { type: String, sparse: true } });
|
|
491
493
|
* s.path('name').index({ sparse: true });
|
|
@@ -522,7 +524,7 @@ SchemaType.prototype.sparse = function(bool) {
|
|
|
522
524
|
* Defines this path as immutable. Mongoose prevents you from changing
|
|
523
525
|
* immutable paths unless the parent document has [`isNew: true`](/docs/api.html#document_Document-isNew).
|
|
524
526
|
*
|
|
525
|
-
* ####Example:
|
|
527
|
+
* #### Example:
|
|
526
528
|
*
|
|
527
529
|
* const schema = new Schema({
|
|
528
530
|
* name: { type: String, immutable: true },
|
|
@@ -540,7 +542,7 @@ SchemaType.prototype.sparse = function(bool) {
|
|
|
540
542
|
* Mongoose also prevents changing immutable properties using `updateOne()`
|
|
541
543
|
* and `updateMany()` based on [strict mode](/docs/guide.html#strict).
|
|
542
544
|
*
|
|
543
|
-
* ####Example:
|
|
545
|
+
* #### Example:
|
|
544
546
|
*
|
|
545
547
|
* // Mongoose will strip out the `name` update, because `name` is immutable
|
|
546
548
|
* Model.updateOne({}, { $set: { name: 'test2' }, $inc: { age: 1 } });
|
|
@@ -574,7 +576,7 @@ SchemaType.prototype.immutable = function(bool) {
|
|
|
574
576
|
* Mongoose calls this function with one parameter: the current `value` of the path. Mongoose
|
|
575
577
|
* then uses the return value in the JSON output.
|
|
576
578
|
*
|
|
577
|
-
* ####Example:
|
|
579
|
+
* #### Example:
|
|
578
580
|
*
|
|
579
581
|
* const schema = new Schema({
|
|
580
582
|
* date: { type: Date, transform: v => v.getFullYear() }
|
|
@@ -603,19 +605,21 @@ SchemaType.prototype.transform = function(fn) {
|
|
|
603
605
|
/**
|
|
604
606
|
* Adds a setter to this schematype.
|
|
605
607
|
*
|
|
606
|
-
* ####Example:
|
|
608
|
+
* #### Example:
|
|
607
609
|
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
610
|
+
* ```javascript
|
|
611
|
+
* function capitalize (val) {
|
|
612
|
+
* if (typeof val !== 'string') val = '';
|
|
613
|
+
* return val.charAt(0).toUpperCase() + val.substring(1);
|
|
614
|
+
* }
|
|
612
615
|
*
|
|
613
|
-
*
|
|
614
|
-
*
|
|
616
|
+
* // defining within the schema
|
|
617
|
+
* const s = new Schema({ name: { type: String, set: capitalize }});
|
|
615
618
|
*
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
+
* // or with the SchemaType
|
|
620
|
+
* const s = new Schema({ name: String })
|
|
621
|
+
* s.path('name').set(capitalize);
|
|
622
|
+
* ```
|
|
619
623
|
*
|
|
620
624
|
* Setters allow you to transform the data before it gets to the raw mongodb
|
|
621
625
|
* document or query.
|
|
@@ -627,52 +631,58 @@ SchemaType.prototype.transform = function(fn) {
|
|
|
627
631
|
*
|
|
628
632
|
* You can set up email lower case normalization easily via a Mongoose setter.
|
|
629
633
|
*
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
634
|
+
* ```javascript
|
|
635
|
+
* function toLower(v) {
|
|
636
|
+
* return v.toLowerCase();
|
|
637
|
+
* }
|
|
633
638
|
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
639
|
+
* const UserSchema = new Schema({
|
|
640
|
+
* email: { type: String, set: toLower }
|
|
641
|
+
* });
|
|
637
642
|
*
|
|
638
|
-
*
|
|
643
|
+
* const User = db.model('User', UserSchema);
|
|
639
644
|
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
645
|
+
* const user = new User({email: 'AVENUE@Q.COM'});
|
|
646
|
+
* console.log(user.email); // 'avenue@q.com'
|
|
642
647
|
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
+
* // or
|
|
649
|
+
* const user = new User();
|
|
650
|
+
* user.email = 'Avenue@Q.com';
|
|
651
|
+
* console.log(user.email); // 'avenue@q.com'
|
|
652
|
+
* User.updateOne({ _id: _id }, { $set: { email: 'AVENUE@Q.COM' } }); // update to 'avenue@q.com'
|
|
653
|
+
* ```
|
|
648
654
|
*
|
|
649
655
|
* As you can see above, setters allow you to transform the data before it
|
|
650
656
|
* stored in MongoDB, or before executing a query.
|
|
651
657
|
*
|
|
652
658
|
* _NOTE: we could have also just used the built-in `lowercase: true` SchemaType option instead of defining our own function._
|
|
653
659
|
*
|
|
654
|
-
*
|
|
660
|
+
* ```javascript
|
|
661
|
+
* new Schema({ email: { type: String, lowercase: true }})
|
|
662
|
+
* ```
|
|
655
663
|
*
|
|
656
664
|
* Setters are also passed a second argument, the schematype on which the setter was defined. This allows for tailored behavior based on options passed in the schema.
|
|
657
665
|
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
666
|
+
* ```javascript
|
|
667
|
+
* function inspector (val, priorValue, schematype) {
|
|
668
|
+
* if (schematype.options.required) {
|
|
669
|
+
* return schematype.path + ' is required';
|
|
670
|
+
* } else {
|
|
671
|
+
* return val;
|
|
672
|
+
* }
|
|
673
|
+
* }
|
|
665
674
|
*
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
675
|
+
* const VirusSchema = new Schema({
|
|
676
|
+
* name: { type: String, required: true, set: inspector },
|
|
677
|
+
* taxonomy: { type: String, set: inspector }
|
|
678
|
+
* })
|
|
670
679
|
*
|
|
671
|
-
*
|
|
672
|
-
*
|
|
680
|
+
* const Virus = db.model('Virus', VirusSchema);
|
|
681
|
+
* const v = new Virus({ name: 'Parvoviridae', taxonomy: 'Parvovirinae' });
|
|
673
682
|
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
683
|
+
* console.log(v.name); // name is required
|
|
684
|
+
* console.log(v.taxonomy); // Parvovirinae
|
|
685
|
+
* ```
|
|
676
686
|
*
|
|
677
687
|
* You can also use setters to modify other properties on the document. If
|
|
678
688
|
* you're setting a property `name` on a document, the setter will run with
|
|
@@ -708,7 +718,7 @@ SchemaType.prototype.set = function(fn) {
|
|
|
708
718
|
/**
|
|
709
719
|
* Adds a getter to this schematype.
|
|
710
720
|
*
|
|
711
|
-
* ####Example:
|
|
721
|
+
* #### Example:
|
|
712
722
|
*
|
|
713
723
|
* function dob (val) {
|
|
714
724
|
* if (!val) return val;
|
|
@@ -784,7 +794,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
784
794
|
*
|
|
785
795
|
* The error message argument is optional. If not passed, the [default generic error message template](#error_messages_MongooseError-messages) will be used.
|
|
786
796
|
*
|
|
787
|
-
* ####Examples:
|
|
797
|
+
* #### Examples:
|
|
788
798
|
*
|
|
789
799
|
* // make sure every value is equal to "something"
|
|
790
800
|
* function validator (val) {
|
|
@@ -810,7 +820,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
810
820
|
* const schema = new Schema({ name: 'string' });
|
|
811
821
|
* schema.path('name').validate(validator, 'validation of `{PATH}` failed with value `{VALUE}`');
|
|
812
822
|
*
|
|
813
|
-
* ####Error message templates:
|
|
823
|
+
* #### Error message templates:
|
|
814
824
|
*
|
|
815
825
|
* From the examples above, you may have noticed that error messages support
|
|
816
826
|
* basic templating. There are a few other template keywords besides `{PATH}`
|
|
@@ -837,7 +847,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
837
847
|
* message: function(props) { return props.reason.message; }
|
|
838
848
|
* });
|
|
839
849
|
*
|
|
840
|
-
* ####Asynchronous validation:
|
|
850
|
+
* #### Asynchronous validation:
|
|
841
851
|
*
|
|
842
852
|
* Mongoose supports validators that return a promise. A validator that returns
|
|
843
853
|
* a promise is called an _async validator_. Async validators run in
|
|
@@ -930,7 +940,7 @@ SchemaType.prototype.validate = function(obj, message, type) {
|
|
|
930
940
|
* Adds a required validator to this SchemaType. The validator gets added
|
|
931
941
|
* to the front of this SchemaType's validators array using `unshift()`.
|
|
932
942
|
*
|
|
933
|
-
* ####Example:
|
|
943
|
+
* #### Example:
|
|
934
944
|
*
|
|
935
945
|
* const s = new Schema({ born: { type: Date, required: true })
|
|
936
946
|
*
|
|
@@ -1069,7 +1079,7 @@ SchemaType.prototype.required = function(required, message) {
|
|
|
1069
1079
|
* Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
|
|
1070
1080
|
* looks at to determine the foreign collection it should query.
|
|
1071
1081
|
*
|
|
1072
|
-
* ####Example:
|
|
1082
|
+
* #### Example:
|
|
1073
1083
|
* const userSchema = new Schema({ name: String });
|
|
1074
1084
|
* const User = mongoose.model('User', userSchema);
|
|
1075
1085
|
*
|
|
@@ -1124,7 +1134,7 @@ SchemaType.prototype.getDefault = function(scope, init) {
|
|
|
1124
1134
|
ret = utils.clone(ret);
|
|
1125
1135
|
}
|
|
1126
1136
|
|
|
1127
|
-
const casted = this.applySetters(ret, scope, init);
|
|
1137
|
+
const casted = this.applySetters(ret, scope, init, undefined, setOptionsForDefaults);
|
|
1128
1138
|
if (casted && !Array.isArray(casted) && casted.$isSingleNested) {
|
|
1129
1139
|
casted.$__parent = scope;
|
|
1130
1140
|
}
|
|
@@ -1211,7 +1221,7 @@ SchemaType.prototype.applyGetters = function(value, scope) {
|
|
|
1211
1221
|
*
|
|
1212
1222
|
* Set to `true` if this path should always be included in the results, `false` if it should be excluded by default. This setting can be overridden at the query level.
|
|
1213
1223
|
*
|
|
1214
|
-
* ####Example:
|
|
1224
|
+
* #### Example:
|
|
1215
1225
|
*
|
|
1216
1226
|
* T = db.model('T', new Schema({ x: { type: String, select: true }}));
|
|
1217
1227
|
* T.find(..); // field x will always be selected ..
|
|
@@ -1340,7 +1350,7 @@ function _validate(ok, validatorProperties) {
|
|
|
1340
1350
|
/**
|
|
1341
1351
|
* Performs a validation of `value` using the validators declared for this SchemaType.
|
|
1342
1352
|
*
|
|
1343
|
-
* ####Note:
|
|
1353
|
+
* #### Note:
|
|
1344
1354
|
*
|
|
1345
1355
|
* This method ignores the asynchronous validators.
|
|
1346
1356
|
*
|
|
@@ -1618,7 +1628,7 @@ SchemaType.prototype._castForQuery = function(val) {
|
|
|
1618
1628
|
* Override the function the required validator uses to check whether a value
|
|
1619
1629
|
* passes the `required` check. Override this on the individual SchemaType.
|
|
1620
1630
|
*
|
|
1621
|
-
* ####Example:
|
|
1631
|
+
* #### Example:
|
|
1622
1632
|
*
|
|
1623
1633
|
* // Use this to allow empty strings to pass the `required` validator
|
|
1624
1634
|
* mongoose.Schema.Types.String.checkRequired(v => typeof v === 'string');
|
|
@@ -89,7 +89,7 @@ const methods = {
|
|
|
89
89
|
/**
|
|
90
90
|
* Searches array items for the first document with a matching _id.
|
|
91
91
|
*
|
|
92
|
-
* ####Example:
|
|
92
|
+
* #### Example:
|
|
93
93
|
*
|
|
94
94
|
* const embeddedDoc = m.array.id(some_id);
|
|
95
95
|
*
|
|
@@ -141,7 +141,7 @@ const methods = {
|
|
|
141
141
|
/**
|
|
142
142
|
* Returns a native js Array of plain js objects
|
|
143
143
|
*
|
|
144
|
-
* ####
|
|
144
|
+
* #### Note:
|
|
145
145
|
*
|
|
146
146
|
* _Each sub-document is converted to a plain object by calling its `#toObject` method._
|
|
147
147
|
*
|
package/lib/types/array/index.js
CHANGED
|
@@ -16,7 +16,7 @@ const arraySchemaSymbol = require('../../helpers/symbols').arraySchemaSymbol;
|
|
|
16
16
|
/**
|
|
17
17
|
* Mongoose Array constructor.
|
|
18
18
|
*
|
|
19
|
-
* ####
|
|
19
|
+
* #### Note:
|
|
20
20
|
*
|
|
21
21
|
* _Values always have to be passed to the constructor to initialize, otherwise `MongooseArray#push` will mark the array as modified._
|
|
22
22
|
*
|