mongoose 6.2.8 → 6.2.11
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 +28 -0
- package/dist/browser.umd.js +38 -27
- package/lib/aggregate.js +57 -66
- package/lib/browser.js +4 -4
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -2
- package/lib/cursor/QueryCursor.js +3 -3
- package/lib/document.js +65 -49
- package/lib/error/index.js +2 -2
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/query/castUpdate.js +3 -1
- package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
- package/lib/helpers/update/applyTimestampsToUpdate.js +0 -1
- package/lib/index.js +24 -26
- package/lib/model.js +144 -146
- 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 +291 -248
- package/lib/schema/SubdocumentPath.js +4 -3
- package/lib/schema/array.js +2 -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 +3 -3
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +6 -6
- package/lib/schema/objectid.js +4 -7
- package/lib/schema/string.js +38 -16
- package/lib/schema.js +30 -29
- package/lib/schematype.js +78 -69
- 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 +13 -13
- 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 +31 -2
- package/lib/virtualtype.js +3 -3
- package/package.json +18 -16
- package/tools/repl.js +8 -8
- package/tools/sharded.js +3 -3
- package/types/aggregate.d.ts +223 -0
- package/types/connection.d.ts +116 -116
- package/types/error.d.ts +2 -2
- package/types/index.d.ts +76 -213
- 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
|
|
@@ -1010,6 +1010,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
|
|
|
1010
1010
|
if (options.hasOwnProperty('strict')) {
|
|
1011
1011
|
childSchemaOptions.strict = options.strict;
|
|
1012
1012
|
}
|
|
1013
|
+
|
|
1013
1014
|
if (this._userProvidedOptions.hasOwnProperty('_id')) {
|
|
1014
1015
|
childSchemaOptions._id = this._userProvidedOptions._id;
|
|
1015
1016
|
} else if (Schema.Types.DocumentArray.defaultOptions._id != null) {
|
|
@@ -1133,7 +1134,7 @@ function createMapNestedSchemaType(schema, schemaType, path, obj, options) {
|
|
|
1133
1134
|
*
|
|
1134
1135
|
* The callback is passed the pathname and the schemaType instance.
|
|
1135
1136
|
*
|
|
1136
|
-
* ####Example:
|
|
1137
|
+
* #### Example:
|
|
1137
1138
|
*
|
|
1138
1139
|
* const userSchema = new Schema({ name: String, registeredAt: Date });
|
|
1139
1140
|
* userSchema.eachPath((pathname, schematype) => {
|
|
@@ -1162,7 +1163,7 @@ Schema.prototype.eachPath = function(fn) {
|
|
|
1162
1163
|
/**
|
|
1163
1164
|
* Returns an Array of path strings that are required by this schema.
|
|
1164
1165
|
*
|
|
1165
|
-
* ####Example:
|
|
1166
|
+
* #### Example:
|
|
1166
1167
|
* const s = new Schema({
|
|
1167
1168
|
* name: { type: String, required: true },
|
|
1168
1169
|
* age: { type: String, required: true },
|
|
@@ -1214,7 +1215,7 @@ Schema.prototype.indexedPaths = function indexedPaths() {
|
|
|
1214
1215
|
*
|
|
1215
1216
|
* Given a path, returns whether it is a real, virtual, nested, or ad-hoc/undefined path.
|
|
1216
1217
|
*
|
|
1217
|
-
* ####Example:
|
|
1218
|
+
* #### Example:
|
|
1218
1219
|
* const s = new Schema({ name: String, nested: { foo: String } });
|
|
1219
1220
|
* s.virtual('foo').get(() => 42);
|
|
1220
1221
|
* s.pathType('name'); // "real"
|
|
@@ -1371,7 +1372,7 @@ function getPositionalPath(self, path) {
|
|
|
1371
1372
|
/**
|
|
1372
1373
|
* Adds a method call to the queue.
|
|
1373
1374
|
*
|
|
1374
|
-
* ####Example:
|
|
1375
|
+
* #### Example:
|
|
1375
1376
|
*
|
|
1376
1377
|
* schema.methods.print = function() { console.log(this); };
|
|
1377
1378
|
* schema.queue('print', []); // Print the doc every one is instantiated
|
|
@@ -1392,7 +1393,7 @@ Schema.prototype.queue = function(name, args) {
|
|
|
1392
1393
|
/**
|
|
1393
1394
|
* Defines a pre hook for the model.
|
|
1394
1395
|
*
|
|
1395
|
-
* ####Example
|
|
1396
|
+
* #### Example
|
|
1396
1397
|
*
|
|
1397
1398
|
* const toySchema = new Schema({ name: String, created: Date });
|
|
1398
1399
|
*
|
|
@@ -1466,7 +1467,7 @@ Schema.prototype.pre = function(name) {
|
|
|
1466
1467
|
* });
|
|
1467
1468
|
*
|
|
1468
1469
|
* schema.post(/Many$/, function(res) {
|
|
1469
|
-
* console.log('this fired after you ran `updateMany()` or `deleteMany()`);
|
|
1470
|
+
* console.log('this fired after you ran `updateMany()` or `deleteMany()`');
|
|
1470
1471
|
* });
|
|
1471
1472
|
*
|
|
1472
1473
|
* const Model = mongoose.model('Model', schema);
|
|
@@ -1514,7 +1515,7 @@ Schema.prototype.post = function(name) {
|
|
|
1514
1515
|
/**
|
|
1515
1516
|
* Registers a plugin for this schema.
|
|
1516
1517
|
*
|
|
1517
|
-
* ####Example:
|
|
1518
|
+
* #### Example:
|
|
1518
1519
|
*
|
|
1519
1520
|
* const s = new Schema({ name: String });
|
|
1520
1521
|
* s.plugin(schema => console.log(schema.path('name').path));
|
|
@@ -1548,7 +1549,7 @@ Schema.prototype.plugin = function(fn, opts) {
|
|
|
1548
1549
|
/**
|
|
1549
1550
|
* Adds an instance method to documents constructed from Models compiled from this schema.
|
|
1550
1551
|
*
|
|
1551
|
-
* ####Example
|
|
1552
|
+
* #### Example
|
|
1552
1553
|
*
|
|
1553
1554
|
* const schema = kittySchema = new Schema(..);
|
|
1554
1555
|
*
|
|
@@ -1595,7 +1596,7 @@ Schema.prototype.method = function(name, fn, options) {
|
|
|
1595
1596
|
/**
|
|
1596
1597
|
* Adds static "class" methods to Models compiled from this schema.
|
|
1597
1598
|
*
|
|
1598
|
-
* ####Example
|
|
1599
|
+
* #### Example
|
|
1599
1600
|
*
|
|
1600
1601
|
* const schema = new Schema(..);
|
|
1601
1602
|
* // Equivalent to `schema.statics.findByName = function(name) {}`;
|
|
@@ -1628,7 +1629,7 @@ Schema.prototype.static = function(name, fn) {
|
|
|
1628
1629
|
/**
|
|
1629
1630
|
* Defines an index (most likely compound) for this schema.
|
|
1630
1631
|
*
|
|
1631
|
-
* ####Example
|
|
1632
|
+
* #### Example
|
|
1632
1633
|
*
|
|
1633
1634
|
* schema.index({ first: 1, last: -1 })
|
|
1634
1635
|
*
|
|
@@ -1653,7 +1654,7 @@ Schema.prototype.index = function(fields, options) {
|
|
|
1653
1654
|
/**
|
|
1654
1655
|
* Sets a schema option.
|
|
1655
1656
|
*
|
|
1656
|
-
* ####Example
|
|
1657
|
+
* #### Example
|
|
1657
1658
|
*
|
|
1658
1659
|
* schema.set('strict'); // 'true' by default
|
|
1659
1660
|
* schema.set('strict', false); // Sets 'strict' to false
|
|
@@ -1702,7 +1703,7 @@ Schema.prototype.set = function(key, value, _tags) {
|
|
|
1702
1703
|
/**
|
|
1703
1704
|
* Gets a schema option.
|
|
1704
1705
|
*
|
|
1705
|
-
* ####Example:
|
|
1706
|
+
* #### Example:
|
|
1706
1707
|
*
|
|
1707
1708
|
* schema.get('strict'); // true
|
|
1708
1709
|
* schema.set('strict', false);
|
|
@@ -1740,7 +1741,7 @@ Object.defineProperty(Schema, 'indexTypes', {
|
|
|
1740
1741
|
* Returns a list of indexes that this schema declares, via `schema.index()` or by `index: true` in a path's options.
|
|
1741
1742
|
* Indexes are expressed as an array `[spec, options]`.
|
|
1742
1743
|
*
|
|
1743
|
-
* ####Example:
|
|
1744
|
+
* #### Example:
|
|
1744
1745
|
*
|
|
1745
1746
|
* const userSchema = new Schema({
|
|
1746
1747
|
* email: { type: String, required: true, unique: true },
|
|
@@ -1902,7 +1903,7 @@ Schema.prototype.virtualpath = function(name) {
|
|
|
1902
1903
|
/**
|
|
1903
1904
|
* Removes the given `path` (or [`paths`]).
|
|
1904
1905
|
*
|
|
1905
|
-
* ####Example:
|
|
1906
|
+
* #### Example:
|
|
1906
1907
|
*
|
|
1907
1908
|
* const schema = new Schema({ name: String, age: Number });
|
|
1908
1909
|
* schema.remove('name');
|
|
@@ -1969,7 +1970,7 @@ function _deletePath(schema, name) {
|
|
|
1969
1970
|
* [statics](/docs/guide.html#statics), and
|
|
1970
1971
|
* [methods](/docs/guide.html#methods).
|
|
1971
1972
|
*
|
|
1972
|
-
* ####Example:
|
|
1973
|
+
* #### Example:
|
|
1973
1974
|
*
|
|
1974
1975
|
* ```javascript
|
|
1975
1976
|
* const md5 = require('md5');
|
|
@@ -2232,12 +2233,12 @@ module.exports = exports = Schema;
|
|
|
2232
2233
|
/**
|
|
2233
2234
|
* The various built-in Mongoose Schema Types.
|
|
2234
2235
|
*
|
|
2235
|
-
* ####Example:
|
|
2236
|
+
* #### Example:
|
|
2236
2237
|
*
|
|
2237
2238
|
* const mongoose = require('mongoose');
|
|
2238
2239
|
* const ObjectId = mongoose.Schema.Types.ObjectId;
|
|
2239
2240
|
*
|
|
2240
|
-
* ####Types:
|
|
2241
|
+
* #### Types:
|
|
2241
2242
|
*
|
|
2242
2243
|
* - [String](/docs/schematypes.html#strings)
|
|
2243
2244
|
* - [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
|
*
|
|
@@ -1118,13 +1128,12 @@ SchemaType.prototype.getDefault = function(scope, init) {
|
|
|
1118
1128
|
ret = this.defaultValue;
|
|
1119
1129
|
}
|
|
1120
1130
|
|
|
1121
|
-
|
|
1122
1131
|
if (ret !== null && ret !== undefined) {
|
|
1123
1132
|
if (typeof ret === 'object' && (!this.options || !this.options.shared)) {
|
|
1124
1133
|
ret = utils.clone(ret);
|
|
1125
1134
|
}
|
|
1126
1135
|
|
|
1127
|
-
const casted = this.applySetters(ret, scope, init);
|
|
1136
|
+
const casted = this.applySetters(ret, scope, init, undefined, setOptionsForDefaults);
|
|
1128
1137
|
if (casted && !Array.isArray(casted) && casted.$isSingleNested) {
|
|
1129
1138
|
casted.$__parent = scope;
|
|
1130
1139
|
}
|
|
@@ -1211,7 +1220,7 @@ SchemaType.prototype.applyGetters = function(value, scope) {
|
|
|
1211
1220
|
*
|
|
1212
1221
|
* 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
1222
|
*
|
|
1214
|
-
* ####Example:
|
|
1223
|
+
* #### Example:
|
|
1215
1224
|
*
|
|
1216
1225
|
* T = db.model('T', new Schema({ x: { type: String, select: true }}));
|
|
1217
1226
|
* T.find(..); // field x will always be selected ..
|
|
@@ -1340,7 +1349,7 @@ function _validate(ok, validatorProperties) {
|
|
|
1340
1349
|
/**
|
|
1341
1350
|
* Performs a validation of `value` using the validators declared for this SchemaType.
|
|
1342
1351
|
*
|
|
1343
|
-
* ####Note:
|
|
1352
|
+
* #### Note:
|
|
1344
1353
|
*
|
|
1345
1354
|
* This method ignores the asynchronous validators.
|
|
1346
1355
|
*
|
|
@@ -1618,7 +1627,7 @@ SchemaType.prototype._castForQuery = function(val) {
|
|
|
1618
1627
|
* Override the function the required validator uses to check whether a value
|
|
1619
1628
|
* passes the `required` check. Override this on the individual SchemaType.
|
|
1620
1629
|
*
|
|
1621
|
-
* ####Example:
|
|
1630
|
+
* #### Example:
|
|
1622
1631
|
*
|
|
1623
1632
|
* // Use this to allow empty strings to pass the `required` validator
|
|
1624
1633
|
* 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
|
*
|