mongoose 6.2.9 → 6.3.0
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/CHANGELOG.md +28 -0
- package/dist/browser.umd.js +2 -1693
- package/lib/aggregate.js +59 -67
- package/lib/browser.js +4 -4
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -2
- package/lib/cursor/ChangeStream.js +42 -2
- package/lib/cursor/QueryCursor.js +5 -3
- package/lib/document.js +39 -46
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +2 -2
- package/lib/helpers/cursor/eachAsync.js +44 -12
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/model/discriminator.js +1 -3
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/query/applyGlobalOption.js +29 -0
- 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 +33 -26
- package/lib/model.js +88 -90
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +2 -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 +273 -249
- 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 +144 -30
- package/lib/schematype.js +75 -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 +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/validoptions.js +1 -0
- package/lib/virtualtype.js +3 -3
- package/package.json +19 -13
- package/tools/repl.js +2 -1
- package/types/aggregate.d.ts +223 -0
- package/types/cursor.d.ts +10 -4
- package/types/index.d.ts +194 -209
- package/types/mongooseoptions.d.ts +10 -4
- package/lib/helpers/query/applyGlobalMaxTimeMS.js +0 -15
package/lib/schematype.js
CHANGED
|
@@ -28,7 +28,7 @@ const setOptionsForDefaults = { _skipMarkModified: true };
|
|
|
28
28
|
* SchemaType constructor. Do **not** instantiate `SchemaType` directly.
|
|
29
29
|
* Mongoose converts your schema paths into SchemaTypes automatically.
|
|
30
30
|
*
|
|
31
|
-
* ####Example:
|
|
31
|
+
* #### Example:
|
|
32
32
|
*
|
|
33
33
|
* const schema = new Schema({ name: String });
|
|
34
34
|
* schema.path('name') instanceof SchemaType; // true
|
|
@@ -133,7 +133,7 @@ SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;
|
|
|
133
133
|
/**
|
|
134
134
|
* The path to this SchemaType in a Schema.
|
|
135
135
|
*
|
|
136
|
-
* ####Example:
|
|
136
|
+
* #### Example:
|
|
137
137
|
* const schema = new Schema({ name: String });
|
|
138
138
|
* schema.path('name').path; // 'name'
|
|
139
139
|
*
|
|
@@ -147,7 +147,7 @@ SchemaType.prototype.path;
|
|
|
147
147
|
/**
|
|
148
148
|
* The validators that Mongoose should run to validate properties at this SchemaType's path.
|
|
149
149
|
*
|
|
150
|
-
* ####Example:
|
|
150
|
+
* #### Example:
|
|
151
151
|
* const schema = new Schema({ name: { type: String, required: true } });
|
|
152
152
|
* schema.path('name').validators.length; // 1, the `required` validator
|
|
153
153
|
*
|
|
@@ -161,7 +161,7 @@ SchemaType.prototype.validators;
|
|
|
161
161
|
/**
|
|
162
162
|
* True if this SchemaType has a required validator. False otherwise.
|
|
163
163
|
*
|
|
164
|
-
* ####Example:
|
|
164
|
+
* #### Example:
|
|
165
165
|
* const schema = new Schema({ name: { type: String, required: true } });
|
|
166
166
|
* schema.path('name').isRequired; // true
|
|
167
167
|
*
|
|
@@ -194,7 +194,7 @@ SchemaType.prototype.splitPath = function() {
|
|
|
194
194
|
/**
|
|
195
195
|
* Get/set the function used to cast arbitrary values to this type.
|
|
196
196
|
*
|
|
197
|
-
* ####Example:
|
|
197
|
+
* #### Example:
|
|
198
198
|
*
|
|
199
199
|
* // Disallow `null` for numbers, and don't try to cast any values to
|
|
200
200
|
* // numbers, so even strings like '123' will cause a CastError.
|
|
@@ -227,7 +227,7 @@ SchemaType.cast = function cast(caster) {
|
|
|
227
227
|
* Get/set the function used to cast arbitrary values to this particular schematype instance.
|
|
228
228
|
* Overrides `SchemaType.cast()`.
|
|
229
229
|
*
|
|
230
|
-
* ####Example:
|
|
230
|
+
* #### Example:
|
|
231
231
|
*
|
|
232
232
|
* // Disallow `null` for numbers, and don't try to cast any values to
|
|
233
233
|
* // numbers, so even strings like '123' will cause a CastError.
|
|
@@ -273,7 +273,7 @@ SchemaType.prototype.cast = function cast() {
|
|
|
273
273
|
/**
|
|
274
274
|
* Sets a default option for this schema type.
|
|
275
275
|
*
|
|
276
|
-
* ####Example:
|
|
276
|
+
* #### Example:
|
|
277
277
|
*
|
|
278
278
|
* // Make all strings be trimmed by default
|
|
279
279
|
* mongoose.SchemaTypes.String.set('trim', true);
|
|
@@ -297,7 +297,7 @@ SchemaType.set = function set(option, value) {
|
|
|
297
297
|
/**
|
|
298
298
|
* Attaches a getter for all instances of this schema type.
|
|
299
299
|
*
|
|
300
|
-
* ####Example:
|
|
300
|
+
* #### Example:
|
|
301
301
|
*
|
|
302
302
|
* // Make all numbers round down
|
|
303
303
|
* mongoose.Number.get(function(v) { return Math.floor(v); });
|
|
@@ -318,7 +318,7 @@ SchemaType.get = function(getter) {
|
|
|
318
318
|
/**
|
|
319
319
|
* Sets a default value for this SchemaType.
|
|
320
320
|
*
|
|
321
|
-
* ####Example:
|
|
321
|
+
* #### Example:
|
|
322
322
|
*
|
|
323
323
|
* const schema = new Schema({ n: { type: Number, default: 10 })
|
|
324
324
|
* const M = db.model('M', schema)
|
|
@@ -327,7 +327,7 @@ SchemaType.get = function(getter) {
|
|
|
327
327
|
*
|
|
328
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.
|
|
329
329
|
*
|
|
330
|
-
* ####Example:
|
|
330
|
+
* #### Example:
|
|
331
331
|
*
|
|
332
332
|
* // values are cast:
|
|
333
333
|
* const schema = new Schema({ aNumber: { type: Number, default: 4.815162342 }})
|
|
@@ -381,7 +381,7 @@ SchemaType.prototype.default = function(val) {
|
|
|
381
381
|
/**
|
|
382
382
|
* Declares the index options for this schematype.
|
|
383
383
|
*
|
|
384
|
-
* ####Example:
|
|
384
|
+
* #### Example:
|
|
385
385
|
*
|
|
386
386
|
* const s = new Schema({ name: { type: String, index: true })
|
|
387
387
|
* const s = new Schema({ loc: { type: [Number], index: 'hashed' })
|
|
@@ -392,7 +392,7 @@ SchemaType.prototype.default = function(val) {
|
|
|
392
392
|
* s.path('my.date').index({ expires: 60 });
|
|
393
393
|
* s.path('my.path').index({ unique: true, sparse: true });
|
|
394
394
|
*
|
|
395
|
-
* ####
|
|
395
|
+
* #### Note:
|
|
396
396
|
*
|
|
397
397
|
* _Indexes are created [in the background](https://docs.mongodb.com/manual/core/index-creation/#index-creation-background)
|
|
398
398
|
* by default. If `background` is set to `false`, MongoDB will not execute any
|
|
@@ -413,7 +413,7 @@ SchemaType.prototype.index = function(options) {
|
|
|
413
413
|
/**
|
|
414
414
|
* Declares an unique index.
|
|
415
415
|
*
|
|
416
|
-
* ####Example:
|
|
416
|
+
* #### Example:
|
|
417
417
|
*
|
|
418
418
|
* const s = new Schema({ name: { type: String, unique: true }});
|
|
419
419
|
* s.path('name').index({ unique: true });
|
|
@@ -451,7 +451,7 @@ SchemaType.prototype.unique = function(bool) {
|
|
|
451
451
|
/**
|
|
452
452
|
* Declares a full text index.
|
|
453
453
|
*
|
|
454
|
-
* ###Example:
|
|
454
|
+
* ### Example:
|
|
455
455
|
*
|
|
456
456
|
* const s = new Schema({name : {type: String, text : true })
|
|
457
457
|
* s.path('name').index({text : true});
|
|
@@ -487,7 +487,7 @@ SchemaType.prototype.text = function(bool) {
|
|
|
487
487
|
/**
|
|
488
488
|
* Declares a sparse index.
|
|
489
489
|
*
|
|
490
|
-
* ####Example:
|
|
490
|
+
* #### Example:
|
|
491
491
|
*
|
|
492
492
|
* const s = new Schema({ name: { type: String, sparse: true } });
|
|
493
493
|
* s.path('name').index({ sparse: true });
|
|
@@ -524,7 +524,7 @@ SchemaType.prototype.sparse = function(bool) {
|
|
|
524
524
|
* Defines this path as immutable. Mongoose prevents you from changing
|
|
525
525
|
* immutable paths unless the parent document has [`isNew: true`](/docs/api.html#document_Document-isNew).
|
|
526
526
|
*
|
|
527
|
-
* ####Example:
|
|
527
|
+
* #### Example:
|
|
528
528
|
*
|
|
529
529
|
* const schema = new Schema({
|
|
530
530
|
* name: { type: String, immutable: true },
|
|
@@ -542,7 +542,7 @@ SchemaType.prototype.sparse = function(bool) {
|
|
|
542
542
|
* Mongoose also prevents changing immutable properties using `updateOne()`
|
|
543
543
|
* and `updateMany()` based on [strict mode](/docs/guide.html#strict).
|
|
544
544
|
*
|
|
545
|
-
* ####Example:
|
|
545
|
+
* #### Example:
|
|
546
546
|
*
|
|
547
547
|
* // Mongoose will strip out the `name` update, because `name` is immutable
|
|
548
548
|
* Model.updateOne({}, { $set: { name: 'test2' }, $inc: { age: 1 } });
|
|
@@ -576,7 +576,7 @@ SchemaType.prototype.immutable = function(bool) {
|
|
|
576
576
|
* Mongoose calls this function with one parameter: the current `value` of the path. Mongoose
|
|
577
577
|
* then uses the return value in the JSON output.
|
|
578
578
|
*
|
|
579
|
-
* ####Example:
|
|
579
|
+
* #### Example:
|
|
580
580
|
*
|
|
581
581
|
* const schema = new Schema({
|
|
582
582
|
* date: { type: Date, transform: v => v.getFullYear() }
|
|
@@ -605,19 +605,21 @@ SchemaType.prototype.transform = function(fn) {
|
|
|
605
605
|
/**
|
|
606
606
|
* Adds a setter to this schematype.
|
|
607
607
|
*
|
|
608
|
-
* ####Example:
|
|
608
|
+
* #### Example:
|
|
609
609
|
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
610
|
+
* ```javascript
|
|
611
|
+
* function capitalize (val) {
|
|
612
|
+
* if (typeof val !== 'string') val = '';
|
|
613
|
+
* return val.charAt(0).toUpperCase() + val.substring(1);
|
|
614
|
+
* }
|
|
614
615
|
*
|
|
615
|
-
*
|
|
616
|
-
*
|
|
616
|
+
* // defining within the schema
|
|
617
|
+
* const s = new Schema({ name: { type: String, set: capitalize }});
|
|
617
618
|
*
|
|
618
|
-
*
|
|
619
|
-
*
|
|
620
|
-
*
|
|
619
|
+
* // or with the SchemaType
|
|
620
|
+
* const s = new Schema({ name: String })
|
|
621
|
+
* s.path('name').set(capitalize);
|
|
622
|
+
* ```
|
|
621
623
|
*
|
|
622
624
|
* Setters allow you to transform the data before it gets to the raw mongodb
|
|
623
625
|
* document or query.
|
|
@@ -629,52 +631,58 @@ SchemaType.prototype.transform = function(fn) {
|
|
|
629
631
|
*
|
|
630
632
|
* You can set up email lower case normalization easily via a Mongoose setter.
|
|
631
633
|
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
634
|
+
* ```javascript
|
|
635
|
+
* function toLower(v) {
|
|
636
|
+
* return v.toLowerCase();
|
|
637
|
+
* }
|
|
635
638
|
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
+
* const UserSchema = new Schema({
|
|
640
|
+
* email: { type: String, set: toLower }
|
|
641
|
+
* });
|
|
639
642
|
*
|
|
640
|
-
*
|
|
643
|
+
* const User = db.model('User', UserSchema);
|
|
641
644
|
*
|
|
642
|
-
*
|
|
643
|
-
*
|
|
645
|
+
* const user = new User({email: 'AVENUE@Q.COM'});
|
|
646
|
+
* console.log(user.email); // 'avenue@q.com'
|
|
644
647
|
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
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
|
+
* ```
|
|
650
654
|
*
|
|
651
655
|
* As you can see above, setters allow you to transform the data before it
|
|
652
656
|
* stored in MongoDB, or before executing a query.
|
|
653
657
|
*
|
|
654
658
|
* _NOTE: we could have also just used the built-in `lowercase: true` SchemaType option instead of defining our own function._
|
|
655
659
|
*
|
|
656
|
-
*
|
|
660
|
+
* ```javascript
|
|
661
|
+
* new Schema({ email: { type: String, lowercase: true }})
|
|
662
|
+
* ```
|
|
657
663
|
*
|
|
658
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.
|
|
659
665
|
*
|
|
660
|
-
*
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
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
|
+
* }
|
|
667
674
|
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
675
|
+
* const VirusSchema = new Schema({
|
|
676
|
+
* name: { type: String, required: true, set: inspector },
|
|
677
|
+
* taxonomy: { type: String, set: inspector }
|
|
678
|
+
* })
|
|
672
679
|
*
|
|
673
|
-
*
|
|
674
|
-
*
|
|
680
|
+
* const Virus = db.model('Virus', VirusSchema);
|
|
681
|
+
* const v = new Virus({ name: 'Parvoviridae', taxonomy: 'Parvovirinae' });
|
|
675
682
|
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
683
|
+
* console.log(v.name); // name is required
|
|
684
|
+
* console.log(v.taxonomy); // Parvovirinae
|
|
685
|
+
* ```
|
|
678
686
|
*
|
|
679
687
|
* You can also use setters to modify other properties on the document. If
|
|
680
688
|
* you're setting a property `name` on a document, the setter will run with
|
|
@@ -710,7 +718,7 @@ SchemaType.prototype.set = function(fn) {
|
|
|
710
718
|
/**
|
|
711
719
|
* Adds a getter to this schematype.
|
|
712
720
|
*
|
|
713
|
-
* ####Example:
|
|
721
|
+
* #### Example:
|
|
714
722
|
*
|
|
715
723
|
* function dob (val) {
|
|
716
724
|
* if (!val) return val;
|
|
@@ -786,7 +794,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
786
794
|
*
|
|
787
795
|
* The error message argument is optional. If not passed, the [default generic error message template](#error_messages_MongooseError-messages) will be used.
|
|
788
796
|
*
|
|
789
|
-
* ####Examples:
|
|
797
|
+
* #### Examples:
|
|
790
798
|
*
|
|
791
799
|
* // make sure every value is equal to "something"
|
|
792
800
|
* function validator (val) {
|
|
@@ -812,7 +820,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
812
820
|
* const schema = new Schema({ name: 'string' });
|
|
813
821
|
* schema.path('name').validate(validator, 'validation of `{PATH}` failed with value `{VALUE}`');
|
|
814
822
|
*
|
|
815
|
-
* ####Error message templates:
|
|
823
|
+
* #### Error message templates:
|
|
816
824
|
*
|
|
817
825
|
* From the examples above, you may have noticed that error messages support
|
|
818
826
|
* basic templating. There are a few other template keywords besides `{PATH}`
|
|
@@ -839,7 +847,7 @@ SchemaType.prototype.get = function(fn) {
|
|
|
839
847
|
* message: function(props) { return props.reason.message; }
|
|
840
848
|
* });
|
|
841
849
|
*
|
|
842
|
-
* ####Asynchronous validation:
|
|
850
|
+
* #### Asynchronous validation:
|
|
843
851
|
*
|
|
844
852
|
* Mongoose supports validators that return a promise. A validator that returns
|
|
845
853
|
* a promise is called an _async validator_. Async validators run in
|
|
@@ -932,7 +940,7 @@ SchemaType.prototype.validate = function(obj, message, type) {
|
|
|
932
940
|
* Adds a required validator to this SchemaType. The validator gets added
|
|
933
941
|
* to the front of this SchemaType's validators array using `unshift()`.
|
|
934
942
|
*
|
|
935
|
-
* ####Example:
|
|
943
|
+
* #### Example:
|
|
936
944
|
*
|
|
937
945
|
* const s = new Schema({ born: { type: Date, required: true })
|
|
938
946
|
*
|
|
@@ -1071,7 +1079,7 @@ SchemaType.prototype.required = function(required, message) {
|
|
|
1071
1079
|
* Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
|
|
1072
1080
|
* looks at to determine the foreign collection it should query.
|
|
1073
1081
|
*
|
|
1074
|
-
* ####Example:
|
|
1082
|
+
* #### Example:
|
|
1075
1083
|
* const userSchema = new Schema({ name: String });
|
|
1076
1084
|
* const User = mongoose.model('User', userSchema);
|
|
1077
1085
|
*
|
|
@@ -1120,7 +1128,6 @@ SchemaType.prototype.getDefault = function(scope, init) {
|
|
|
1120
1128
|
ret = this.defaultValue;
|
|
1121
1129
|
}
|
|
1122
1130
|
|
|
1123
|
-
|
|
1124
1131
|
if (ret !== null && ret !== undefined) {
|
|
1125
1132
|
if (typeof ret === 'object' && (!this.options || !this.options.shared)) {
|
|
1126
1133
|
ret = utils.clone(ret);
|
|
@@ -1213,7 +1220,7 @@ SchemaType.prototype.applyGetters = function(value, scope) {
|
|
|
1213
1220
|
*
|
|
1214
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.
|
|
1215
1222
|
*
|
|
1216
|
-
* ####Example:
|
|
1223
|
+
* #### Example:
|
|
1217
1224
|
*
|
|
1218
1225
|
* T = db.model('T', new Schema({ x: { type: String, select: true }}));
|
|
1219
1226
|
* T.find(..); // field x will always be selected ..
|
|
@@ -1342,7 +1349,7 @@ function _validate(ok, validatorProperties) {
|
|
|
1342
1349
|
/**
|
|
1343
1350
|
* Performs a validation of `value` using the validators declared for this SchemaType.
|
|
1344
1351
|
*
|
|
1345
|
-
* ####Note:
|
|
1352
|
+
* #### Note:
|
|
1346
1353
|
*
|
|
1347
1354
|
* This method ignores the asynchronous validators.
|
|
1348
1355
|
*
|
|
@@ -1620,7 +1627,7 @@ SchemaType.prototype._castForQuery = function(val) {
|
|
|
1620
1627
|
* Override the function the required validator uses to check whether a value
|
|
1621
1628
|
* passes the `required` check. Override this on the individual SchemaType.
|
|
1622
1629
|
*
|
|
1623
|
-
* ####Example:
|
|
1630
|
+
* #### Example:
|
|
1624
1631
|
*
|
|
1625
1632
|
* // Use this to allow empty strings to pass the `required` validator
|
|
1626
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
|
*
|
|
@@ -100,7 +100,7 @@ const methods = {
|
|
|
100
100
|
/**
|
|
101
101
|
* Atomically shifts the array at most one time per document `save()`.
|
|
102
102
|
*
|
|
103
|
-
* ####
|
|
103
|
+
* #### Note:
|
|
104
104
|
*
|
|
105
105
|
* _Calling this multiple times on an array before saving sends the same command as calling it once._
|
|
106
106
|
* _This update is implemented using the MongoDB [$pop](https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop) method which enforces this restriction._
|
|
@@ -149,7 +149,7 @@ const methods = {
|
|
|
149
149
|
*
|
|
150
150
|
* #### NOTE:
|
|
151
151
|
*
|
|
152
|
-
* _Calling this
|
|
152
|
+
* _Calling this multiple times on an array before saving sends the same command as calling it once._
|
|
153
153
|
* _This update is implemented using the MongoDB [$pop](https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop) method which enforces this restriction._
|
|
154
154
|
*
|
|
155
155
|
* doc.array = [1,2,3];
|
|
@@ -365,7 +365,7 @@ const methods = {
|
|
|
365
365
|
/**
|
|
366
366
|
* Adds values to the array if not already present.
|
|
367
367
|
*
|
|
368
|
-
* ####Example:
|
|
368
|
+
* #### Example:
|
|
369
369
|
*
|
|
370
370
|
* console.log(doc.array) // [2,3,4]
|
|
371
371
|
* const added = doc.array.addToSet(4,5);
|
|
@@ -498,7 +498,7 @@ const methods = {
|
|
|
498
498
|
/**
|
|
499
499
|
* Pushes items to the array non-atomically.
|
|
500
500
|
*
|
|
501
|
-
* ####
|
|
501
|
+
* #### Note:
|
|
502
502
|
*
|
|
503
503
|
* _marks the entire array as modified, which if saved, will store it as a `$set` operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it._
|
|
504
504
|
*
|
|
@@ -519,7 +519,7 @@ const methods = {
|
|
|
519
519
|
/**
|
|
520
520
|
* Wraps [`Array#pop`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/pop) with proper change tracking.
|
|
521
521
|
*
|
|
522
|
-
* ####Note:
|
|
522
|
+
* #### Note:
|
|
523
523
|
*
|
|
524
524
|
* _marks the entire array as modified which will pass the entire thing to $set potentially overwriting any changes that happen between when you retrieved the object and when you save it._
|
|
525
525
|
*
|
|
@@ -541,7 +541,7 @@ const methods = {
|
|
|
541
541
|
* the provided value to an embedded document and comparing using
|
|
542
542
|
* [the `Document.equals()` function.](/docs/api.html#document_Document-equals)
|
|
543
543
|
*
|
|
544
|
-
* ####Examples:
|
|
544
|
+
* #### Examples:
|
|
545
545
|
*
|
|
546
546
|
* doc.array.pull(ObjectId)
|
|
547
547
|
* doc.array.pull({ _id: 'someId' })
|
|
@@ -611,7 +611,7 @@ const methods = {
|
|
|
611
611
|
/**
|
|
612
612
|
* Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking.
|
|
613
613
|
*
|
|
614
|
-
* ####Example:
|
|
614
|
+
* #### Example:
|
|
615
615
|
*
|
|
616
616
|
* const schema = Schema({ nums: [Number] });
|
|
617
617
|
* const Model = mongoose.model('Test', schema);
|
|
@@ -706,7 +706,7 @@ const methods = {
|
|
|
706
706
|
/**
|
|
707
707
|
* Sets the casted `val` at index `i` and marks the array modified.
|
|
708
708
|
*
|
|
709
|
-
* ####Example:
|
|
709
|
+
* #### Example:
|
|
710
710
|
*
|
|
711
711
|
* // given documents based on the following
|
|
712
712
|
* const Doc = mongoose.model('Doc', new Schema({ array: [Number] }));
|
|
@@ -745,14 +745,14 @@ const methods = {
|
|
|
745
745
|
/**
|
|
746
746
|
* Wraps [`Array#shift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking.
|
|
747
747
|
*
|
|
748
|
-
* ####Example:
|
|
748
|
+
* #### Example:
|
|
749
749
|
*
|
|
750
750
|
* doc.array = [2,3];
|
|
751
751
|
* const res = doc.array.shift();
|
|
752
752
|
* console.log(res) // 2
|
|
753
753
|
* console.log(doc.array) // [3]
|
|
754
754
|
*
|
|
755
|
-
* ####Note:
|
|
755
|
+
* #### Note:
|
|
756
756
|
*
|
|
757
757
|
* _marks the entire array as modified, which if saved, will store it as a `$set` operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it._
|
|
758
758
|
*
|
|
@@ -772,7 +772,7 @@ const methods = {
|
|
|
772
772
|
/**
|
|
773
773
|
* Wraps [`Array#sort`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort) with proper change tracking.
|
|
774
774
|
*
|
|
775
|
-
* ####
|
|
775
|
+
* #### Note:
|
|
776
776
|
*
|
|
777
777
|
* _marks the entire array as modified, which if saved, will store it as a `$set` operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it._
|
|
778
778
|
*
|
|
@@ -792,7 +792,7 @@ const methods = {
|
|
|
792
792
|
/**
|
|
793
793
|
* Wraps [`Array#splice`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice) with proper change tracking and casting.
|
|
794
794
|
*
|
|
795
|
-
* ####Note:
|
|
795
|
+
* #### Note:
|
|
796
796
|
*
|
|
797
797
|
* _marks the entire array as modified, which if saved, will store it as a `$set` operation, potentially overwritting any changes that happen between when you retrieved the object and when you save it._
|
|
798
798
|
*
|
|
@@ -869,7 +869,7 @@ const methods = {
|
|
|
869
869
|
/**
|
|
870
870
|
* Wraps [`Array#unshift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking.
|
|
871
871
|
*
|
|
872
|
-
* ####Note:
|
|
872
|
+
* #### Note:
|
|
873
873
|
*
|
|
874
874
|
* _marks the entire array as modified, which if saved, will store it as a `$set` operation, potentially overwriting any changes that happen between when you retrieved the object and when you save it._
|
|
875
875
|
*
|
package/lib/types/buffer.js
CHANGED
|
@@ -113,7 +113,7 @@ MongooseBuffer.mixin = {
|
|
|
113
113
|
/**
|
|
114
114
|
* Copies the buffer.
|
|
115
115
|
*
|
|
116
|
-
* ####Note:
|
|
116
|
+
* #### Note:
|
|
117
117
|
*
|
|
118
118
|
* `Buffer#copy` does not mark `target` as modified so you must copy from a `MongooseBuffer` for it to work as expected. This is a work around since `copy` modifies the target, not this.
|
|
119
119
|
*
|
package/lib/types/decimal128.js
CHANGED
package/lib/types/objectid.js
CHANGED
package/lib/types/subdocument.js
CHANGED
|
@@ -58,7 +58,7 @@ Subdocument.prototype.toBSON = function() {
|
|
|
58
58
|
/**
|
|
59
59
|
* Used as a stub for middleware
|
|
60
60
|
*
|
|
61
|
-
* ####
|
|
61
|
+
* #### Note:
|
|
62
62
|
*
|
|
63
63
|
* _This is a no-op. Does not actually save the doc to the db._
|
|
64
64
|
*
|
|
@@ -116,7 +116,7 @@ Subdocument.prototype.$__pathRelativeToParent = function(p) {
|
|
|
116
116
|
/**
|
|
117
117
|
* Used as a stub for middleware
|
|
118
118
|
*
|
|
119
|
-
* ####
|
|
119
|
+
* #### Note:
|
|
120
120
|
*
|
|
121
121
|
* _This is a no-op. Does not actually save the doc to the db._
|
|
122
122
|
*
|
|
@@ -269,6 +269,35 @@ Subdocument.prototype.ownerDocument = function() {
|
|
|
269
269
|
return this.$__.ownerDocument;
|
|
270
270
|
};
|
|
271
271
|
|
|
272
|
+
/*!
|
|
273
|
+
* ignore
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
Subdocument.prototype.$__fullPathWithIndexes = function() {
|
|
277
|
+
let parent = this; // eslint-disable-line consistent-this
|
|
278
|
+
const paths = [];
|
|
279
|
+
const seenDocs = new Set([parent]);
|
|
280
|
+
|
|
281
|
+
while (true) {
|
|
282
|
+
if (typeof parent.$__pathRelativeToParent !== 'function') {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
paths.unshift(parent.$__pathRelativeToParent(void 0, false));
|
|
286
|
+
const _parent = parent.$parent();
|
|
287
|
+
if (_parent == null) {
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
parent = _parent;
|
|
291
|
+
if (seenDocs.has(parent)) {
|
|
292
|
+
throw new Error('Infinite subdocument loop: subdoc with _id ' + parent._id + ' is a parent of itself');
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
seenDocs.add(parent);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return paths.join('.');
|
|
299
|
+
};
|
|
300
|
+
|
|
272
301
|
/**
|
|
273
302
|
* Returns this sub-documents parent document.
|
|
274
303
|
*
|
package/lib/validoptions.js
CHANGED
package/lib/virtualtype.js
CHANGED
|
@@ -7,7 +7,7 @@ const utils = require('./utils');
|
|
|
7
7
|
*
|
|
8
8
|
* This is what mongoose uses to define virtual attributes via `Schema.prototype.virtual`.
|
|
9
9
|
*
|
|
10
|
-
* ####Example:
|
|
10
|
+
* #### Example:
|
|
11
11
|
*
|
|
12
12
|
* const fullname = schema.virtual('fullname');
|
|
13
13
|
* fullname instanceof mongoose.VirtualType // true
|
|
@@ -78,7 +78,7 @@ VirtualType.prototype.clone = function() {
|
|
|
78
78
|
* - `virtual`: the virtual object you called `.get()` on
|
|
79
79
|
* - `doc`: the document this virtual is attached to. Equivalent to `this`.
|
|
80
80
|
*
|
|
81
|
-
* ####Example:
|
|
81
|
+
* #### Example:
|
|
82
82
|
*
|
|
83
83
|
* const virtual = schema.virtual('fullname');
|
|
84
84
|
* virtual.get(function(value, virtual, doc) {
|
|
@@ -104,7 +104,7 @@ VirtualType.prototype.get = function(fn) {
|
|
|
104
104
|
* - `virtual`: the virtual object you're calling `.set()` on
|
|
105
105
|
* - `doc`: the document this virtual is attached to. Equivalent to `this`.
|
|
106
106
|
*
|
|
107
|
-
* ####Example:
|
|
107
|
+
* #### Example:
|
|
108
108
|
*
|
|
109
109
|
* const virtual = schema.virtual('fullname');
|
|
110
110
|
* virtual.set(function(value, virtual, doc) {
|