-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
index.js
1031 lines (902 loc) · 27.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const Document = require('../../../document');
const ArraySubdocument = require('../../ArraySubdocument');
const MongooseError = require('../../../error/mongooseError');
const cleanModifiedSubpaths = require('../../../helpers/document/cleanModifiedSubpaths');
const clone = require('../../../helpers/clone');
const internalToObjectOptions = require('../../../options').internalToObjectOptions;
const mpath = require('mpath');
const utils = require('../../../utils');
const isBsonType = require('../../../helpers/isBsonType');
const arrayAtomicsSymbol = require('../../../helpers/symbols').arrayAtomicsSymbol;
const arrayParentSymbol = require('../../../helpers/symbols').arrayParentSymbol;
const arrayPathSymbol = require('../../../helpers/symbols').arrayPathSymbol;
const arraySchemaSymbol = require('../../../helpers/symbols').arraySchemaSymbol;
const populateModelSymbol = require('../../../helpers/symbols').populateModelSymbol;
const slicedSymbol = Symbol('mongoose#Array#sliced');
const _basePush = Array.prototype.push;
/*!
* ignore
*/
const methods = {
/**
* Depopulates stored atomic operation values as necessary for direct insertion to MongoDB.
*
* If no atomics exist, we return all array values after conversion.
*
* @return {Array}
* @method $__getAtomics
* @memberOf MongooseArray
* @instance
* @api private
*/
$__getAtomics() {
const ret = [];
const keys = Object.keys(this[arrayAtomicsSymbol] || {});
let i = keys.length;
const opts = Object.assign({}, internalToObjectOptions, { _isNested: true });
if (i === 0) {
ret[0] = ['$set', this.toObject(opts)];
return ret;
}
while (i--) {
const op = keys[i];
let val = this[arrayAtomicsSymbol][op];
// the atomic values which are arrays are not MongooseArrays. we
// need to convert their elements as if they were MongooseArrays
// to handle populated arrays versus DocumentArrays properly.
if (utils.isMongooseObject(val)) {
val = val.toObject(opts);
} else if (Array.isArray(val)) {
val = this.toObject.call(val, opts);
} else if (val != null && Array.isArray(val.$each)) {
val.$each = this.toObject.call(val.$each, opts);
} else if (val != null && typeof val.valueOf === 'function') {
val = val.valueOf();
}
if (op === '$addToSet') {
val = { $each: val };
}
ret.push([op, val]);
}
return ret;
},
/*!
* ignore
*/
$atomics() {
return this[arrayAtomicsSymbol];
},
/*!
* ignore
*/
$parent() {
return this[arrayParentSymbol];
},
/*!
* ignore
*/
$path() {
return this[arrayPathSymbol];
},
/**
* Atomically shifts the array at most one time per document `save()`.
*
* #### Note:
*
* _Calling this multiple times on an array before saving sends the same command as calling it once._
* _This update is implemented using the MongoDB [$pop](https://www.mongodb.com/docs/manual/reference/operator/update/pop/) method which enforces this restriction._
*
* doc.array = [1,2,3];
*
* const shifted = doc.array.$shift();
* console.log(shifted); // 1
* console.log(doc.array); // [2,3]
*
* // no affect
* shifted = doc.array.$shift();
* console.log(doc.array); // [2,3]
*
* doc.save(function (err) {
* if (err) return handleError(err);
*
* // we saved, now $shift works again
* shifted = doc.array.$shift();
* console.log(shifted ); // 2
* console.log(doc.array); // [3]
* })
*
* @api public
* @memberOf MongooseArray
* @instance
* @method $shift
* @see mongodb https://www.mongodb.com/docs/manual/reference/operator/update/pop/
*/
$shift() {
this._registerAtomic('$pop', -1);
this._markModified();
// only allow shifting once
const __array = this.__array;
if (__array._shifted) {
return;
}
__array._shifted = true;
return [].shift.call(__array);
},
/**
* Pops the array atomically at most one time per document `save()`.
*
* #### NOTE:
*
* _Calling this multiple times on an array before saving sends the same command as calling it once._
* _This update is implemented using the MongoDB [$pop](https://www.mongodb.com/docs/manual/reference/operator/update/pop/) method which enforces this restriction._
*
* doc.array = [1,2,3];
*
* const popped = doc.array.$pop();
* console.log(popped); // 3
* console.log(doc.array); // [1,2]
*
* // no affect
* popped = doc.array.$pop();
* console.log(doc.array); // [1,2]
*
* doc.save(function (err) {
* if (err) return handleError(err);
*
* // we saved, now $pop works again
* popped = doc.array.$pop();
* console.log(popped); // 2
* console.log(doc.array); // [1]
* })
*
* @api public
* @method $pop
* @memberOf MongooseArray
* @instance
* @see mongodb https://www.mongodb.com/docs/manual/reference/operator/update/pop/
* @method $pop
* @memberOf MongooseArray
*/
$pop() {
this._registerAtomic('$pop', 1);
this._markModified();
// only allow popping once
if (this._popped) {
return;
}
this._popped = true;
return [].pop.call(this);
},
/*!
* ignore
*/
$schema() {
return this[arraySchemaSymbol];
},
/**
* Casts a member based on this arrays schema.
*
* @param {any} value
* @return value the casted value
* @method _cast
* @api private
* @memberOf MongooseArray
*/
_cast(value) {
let populated = false;
let Model;
const parent = this[arrayParentSymbol];
if (parent) {
populated = parent.$populated(this[arrayPathSymbol], true);
}
if (populated && value !== null && value !== undefined) {
// cast to the populated Models schema
Model = populated.options[populateModelSymbol];
if (Model == null) {
throw new MongooseError('No populated model found for path `' + this[arrayPathSymbol] + '`. This is likely a bug in Mongoose, please report an issue on github.com/Automattic/mongoose.');
}
// only objects are permitted so we can safely assume that
// non-objects are to be interpreted as _id
if (Buffer.isBuffer(value) ||
isBsonType(value, 'ObjectId') || !utils.isObject(value)) {
value = { _id: value };
}
// gh-2399
// we should cast model only when it's not a discriminator
const isDisc = value.schema && value.schema.discriminatorMapping &&
value.schema.discriminatorMapping.key !== undefined;
if (!isDisc) {
value = new Model(value);
}
return this[arraySchemaSymbol].caster.applySetters(value, parent, true);
}
return this[arraySchemaSymbol].caster.applySetters(value, parent, false);
},
/**
* Internal helper for .map()
*
* @api private
* @return {Number}
* @method _mapCast
* @memberOf MongooseArray
*/
_mapCast(val, index) {
return this._cast(val, this.length + index);
},
/**
* Marks this array as modified.
*
* If it bubbles up from an embedded document change, then it takes the following arguments (otherwise, takes 0 arguments)
*
* @param {ArraySubdocument} subdoc the embedded doc that invoked this method on the Array
* @param {String} embeddedPath the path which changed in the subdoc
* @method _markModified
* @api private
* @memberOf MongooseArray
*/
_markModified(elem) {
const parent = this[arrayParentSymbol];
let dirtyPath;
if (parent) {
dirtyPath = this[arrayPathSymbol];
if (arguments.length) {
dirtyPath = dirtyPath + '.' + elem;
}
if (dirtyPath != null && dirtyPath.endsWith('.$')) {
return this;
}
parent.markModified(dirtyPath, arguments.length !== 0 ? elem : parent);
}
return this;
},
/**
* Register an atomic operation with the parent.
*
* @param {Array} op operation
* @param {any} val
* @method _registerAtomic
* @api private
* @memberOf MongooseArray
*/
_registerAtomic(op, val) {
if (this[slicedSymbol]) {
return;
}
if (op === '$set') {
// $set takes precedence over all other ops.
// mark entire array modified.
this[arrayAtomicsSymbol] = { $set: val };
cleanModifiedSubpaths(this[arrayParentSymbol], this[arrayPathSymbol]);
this._markModified();
return this;
}
const atomics = this[arrayAtomicsSymbol];
// reset pop/shift after save
if (op === '$pop' && !('$pop' in atomics)) {
const _this = this;
this[arrayParentSymbol].once('save', function() {
_this._popped = _this._shifted = null;
});
}
// check for impossible $atomic combos (Mongo denies more than one
// $atomic op on a single path
if (atomics.$set || Object.keys(atomics).length && !(op in atomics)) {
// a different op was previously registered.
// save the entire thing.
this[arrayAtomicsSymbol] = { $set: this };
return this;
}
let selector;
if (op === '$pullAll' || op === '$addToSet') {
atomics[op] || (atomics[op] = []);
atomics[op] = atomics[op].concat(val);
} else if (op === '$pullDocs') {
const pullOp = atomics['$pull'] || (atomics['$pull'] = {});
if (val[0] instanceof ArraySubdocument) {
selector = pullOp['$or'] || (pullOp['$or'] = []);
Array.prototype.push.apply(selector, val.map(v => {
return v.toObject({
transform: (doc, ret) => {
if (v == null || v.$__ == null) {
return ret;
}
Object.keys(v.$__.activePaths.getStatePaths('default')).forEach(path => {
mpath.unset(path, ret);
_minimizePath(ret, path);
});
return ret;
},
virtuals: false
});
}));
} else {
selector = pullOp['_id'] || (pullOp['_id'] = { $in: [] });
selector['$in'] = selector['$in'].concat(val);
}
} else if (op === '$push') {
atomics.$push = atomics.$push || { $each: [] };
if (val != null && utils.hasUserDefinedProperty(val, '$each')) {
atomics.$push = val;
} else {
if (val.length === 1) {
atomics.$push.$each.push(val[0]);
} else if (val.length < 10000) {
atomics.$push.$each.push(...val);
} else {
for (const v of val) {
atomics.$push.$each.push(v);
}
}
}
} else {
atomics[op] = val;
}
return this;
},
/**
* Adds values to the array if not already present.
*
* #### Example:
*
* console.log(doc.array) // [2,3,4]
* const added = doc.array.addToSet(4,5);
* console.log(doc.array) // [2,3,4,5]
* console.log(added) // [5]
*
* @param {...any} [args]
* @return {Array} the values that were added
* @memberOf MongooseArray
* @api public
* @method addToSet
*/
addToSet() {
_checkManualPopulation(this, arguments);
const values = [].map.call(arguments, this._mapCast, this);
const added = [];
let type = '';
if (values[0] instanceof ArraySubdocument) {
type = 'doc';
} else if (values[0] instanceof Date) {
type = 'date';
} else if (isBsonType(values[0], 'ObjectId')) {
type = 'ObjectId';
}
const rawValues = utils.isMongooseArray(values) ? values.__array : values;
const rawArray = utils.isMongooseArray(this) ? this.__array : this;
rawValues.forEach(function(v) {
let found;
const val = +v;
switch (type) {
case 'doc':
found = this.some(function(doc) {
return doc.equals(v);
});
break;
case 'date':
found = this.some(function(d) {
return +d === val;
});
break;
case 'ObjectId':
found = this.find(o => o.toString() === v.toString());
break;
default:
found = ~this.indexOf(v);
break;
}
if (!found) {
this._markModified();
rawArray.push(v);
this._registerAtomic('$addToSet', v);
[].push.call(added, v);
}
}, this);
return added;
},
/**
* Returns the number of pending atomic operations to send to the db for this array.
*
* @api private
* @return {Number}
* @method hasAtomics
* @memberOf MongooseArray
*/
hasAtomics() {
if (!utils.isPOJO(this[arrayAtomicsSymbol])) {
return 0;
}
return Object.keys(this[arrayAtomicsSymbol]).length;
},
/**
* Return whether or not the `obj` is included in the array.
*
* @param {Object} obj the item to check
* @param {Number} fromIndex
* @return {Boolean}
* @api public
* @method includes
* @memberOf MongooseArray
*/
includes(obj, fromIndex) {
const ret = this.indexOf(obj, fromIndex);
return ret !== -1;
},
/**
* Return the index of `obj` or `-1` if not found.
*
* @param {Object} obj the item to look for
* @param {Number} fromIndex
* @return {Number}
* @api public
* @method indexOf
* @memberOf MongooseArray
*/
indexOf(obj, fromIndex) {
if (isBsonType(obj, 'ObjectId')) {
obj = obj.toString();
}
fromIndex = fromIndex == null ? 0 : fromIndex;
const len = this.length;
for (let i = fromIndex; i < len; ++i) {
if (obj == this[i]) {
return i;
}
}
return -1;
},
/**
* Helper for console.log
*
* @api public
* @method inspect
* @memberOf MongooseArray
*/
inspect() {
return JSON.stringify(this);
},
/**
* Pushes items to the array non-atomically.
*
* #### Note:
*
* _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._
*
* @param {...any} [args]
* @api public
* @method nonAtomicPush
* @memberOf MongooseArray
*/
nonAtomicPush() {
const values = [].map.call(arguments, this._mapCast, this);
this._markModified();
const ret = [].push.apply(this, values);
this._registerAtomic('$set', this);
return ret;
},
/**
* Wraps [`Array#pop`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/pop) with proper change tracking.
*
* #### Note:
*
* _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._
*
* @see MongooseArray#$pop https://mongoosejs.com/docs/api/array.html#MongooseArray.prototype.$pop()
* @api public
* @method pop
* @memberOf MongooseArray
*/
pop() {
this._markModified();
const ret = [].pop.call(this);
this._registerAtomic('$set', this);
return ret;
},
/**
* Pulls items from the array atomically. Equality is determined by casting
* the provided value to an embedded document and comparing using
* [the `Document.equals()` function.](https://mongoosejs.com/docs/api/document.html#Document.prototype.equals())
*
* #### Example:
*
* doc.array.pull(ObjectId)
* doc.array.pull({ _id: 'someId' })
* doc.array.pull(36)
* doc.array.pull('tag 1', 'tag 2')
*
* To remove a document from a subdocument array we may pass an object with a matching `_id`.
*
* doc.subdocs.push({ _id: 4815162342 })
* doc.subdocs.pull({ _id: 4815162342 }) // removed
*
* Or we may passing the _id directly and let mongoose take care of it.
*
* doc.subdocs.push({ _id: 4815162342 })
* doc.subdocs.pull(4815162342); // works
*
* The first pull call will result in a atomic operation on the database, if pull is called repeatedly without saving the document, a $set operation is used on the complete array instead, overwriting possible changes that happened on the database in the meantime.
*
* @param {...any} [args]
* @see mongodb https://www.mongodb.com/docs/manual/reference/operator/update/pull/
* @api public
* @method pull
* @memberOf MongooseArray
*/
pull() {
const values = [].map.call(arguments, (v, i) => this._cast(v, i, { defaults: false }), this);
let cur = this[arrayParentSymbol].get(this[arrayPathSymbol]);
if (utils.isMongooseArray(cur)) {
cur = cur.__array;
}
let i = cur.length;
let mem;
this._markModified();
while (i--) {
mem = cur[i];
if (mem instanceof Document) {
const some = values.some(function(v) {
return mem.equals(v);
});
if (some) {
cur.splice(i, 1);
}
} else if (~this.indexOf.call(values, mem)) {
cur.splice(i, 1);
}
}
if (values[0] instanceof ArraySubdocument) {
this._registerAtomic('$pullDocs', values.map(function(v) {
const _id = v.$__getValue('_id');
if (_id === undefined || v.$isDefault('_id')) {
return v;
}
return _id;
}));
} else {
this._registerAtomic('$pullAll', values);
}
// Might have modified child paths and then pulled, like
// `doc.children[1].name = 'test';` followed by
// `doc.children.remove(doc.children[0]);`. In this case we fall back
// to a `$set` on the whole array. See #3511
if (cleanModifiedSubpaths(this[arrayParentSymbol], this[arrayPathSymbol]) > 0) {
this._registerAtomic('$set', this);
}
return this;
},
/**
* Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking.
*
* #### Example:
*
* const schema = Schema({ nums: [Number] });
* const Model = mongoose.model('Test', schema);
*
* const doc = await Model.create({ nums: [3, 4] });
* doc.nums.push(5); // Add 5 to the end of the array
* await doc.save();
*
* // You can also pass an object with `$each` as the
* // first parameter to use MongoDB's `$position`
* doc.nums.push({
* $each: [1, 2],
* $position: 0
* });
* doc.nums; // [1, 2, 3, 4, 5]
*
* @param {...Object} [args]
* @api public
* @method push
* @memberOf MongooseArray
*/
push() {
let values = arguments;
let atomic = values;
const isOverwrite = values[0] != null &&
utils.hasUserDefinedProperty(values[0], '$each');
const arr = utils.isMongooseArray(this) ? this.__array : this;
if (isOverwrite) {
atomic = values[0];
values = values[0].$each;
}
if (this[arraySchemaSymbol] == null) {
return _basePush.apply(this, values);
}
_checkManualPopulation(this, values);
values = [].map.call(values, this._mapCast, this);
let ret;
const atomics = this[arrayAtomicsSymbol];
this._markModified();
if (isOverwrite) {
atomic.$each = values;
if ((atomics.$push && atomics.$push.$each && atomics.$push.$each.length || 0) !== 0 &&
atomics.$push.$position != atomic.$position) {
if (atomic.$position != null) {
[].splice.apply(arr, [atomic.$position, 0].concat(values));
ret = arr.length;
} else {
ret = [].push.apply(arr, values);
}
this._registerAtomic('$set', this);
} else if (atomic.$position != null) {
[].splice.apply(arr, [atomic.$position, 0].concat(values));
ret = this.length;
} else {
ret = [].push.apply(arr, values);
}
} else {
atomic = values;
ret = _basePush.apply(arr, values);
}
this._registerAtomic('$push', atomic);
return ret;
},
/**
* Alias of [pull](https://mongoosejs.com/docs/api/array.html#MongooseArray.prototype.pull())
*
* @see MongooseArray#pull https://mongoosejs.com/docs/api/array.html#MongooseArray.prototype.pull()
* @see mongodb https://www.mongodb.com/docs/manual/reference/operator/update/pull/
* @api public
* @memberOf MongooseArray
* @instance
* @method remove
*/
remove() {
return this.pull.apply(this, arguments);
},
/**
* Sets the casted `val` at index `i` and marks the array modified.
*
* #### Example:
*
* // given documents based on the following
* const Doc = mongoose.model('Doc', new Schema({ array: [Number] }));
*
* const doc = new Doc({ array: [2,3,4] })
*
* console.log(doc.array) // [2,3,4]
*
* doc.array.set(1,"5");
* console.log(doc.array); // [2,5,4] // properly cast to number
* doc.save() // the change is saved
*
* // VS not using array#set
* doc.array[1] = "5";
* console.log(doc.array); // [2,"5",4] // no casting
* doc.save() // change is not saved
*
* @return {Array} this
* @api public
* @method set
* @memberOf MongooseArray
*/
set(i, val, skipModified) {
const arr = this.__array;
if (skipModified) {
arr[i] = val;
return this;
}
const value = methods._cast.call(this, val, i);
methods._markModified.call(this, i);
arr[i] = value;
return this;
},
/**
* Wraps [`Array#shift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking.
*
* #### Example:
*
* doc.array = [2,3];
* const res = doc.array.shift();
* console.log(res) // 2
* console.log(doc.array) // [3]
*
* #### Note:
*
* _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._
*
* @api public
* @method shift
* @memberOf MongooseArray
*/
shift() {
const arr = utils.isMongooseArray(this) ? this.__array : this;
this._markModified();
const ret = [].shift.call(arr);
this._registerAtomic('$set', this);
return ret;
},
/**
* Wraps [`Array#sort`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort) with proper change tracking.
*
* #### Note:
*
* _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._
*
* @api public
* @method sort
* @memberOf MongooseArray
* @see MasteringJS: Array sort https://masteringjs.io/tutorials/fundamentals/array-sort
*/
sort() {
const arr = utils.isMongooseArray(this) ? this.__array : this;
const ret = [].sort.apply(arr, arguments);
this._registerAtomic('$set', this);
return ret;
},
/**
* Wraps [`Array#splice`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice) with proper change tracking and casting.
*
* #### Note:
*
* _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._
*
* @api public
* @method splice
* @memberOf MongooseArray
* @see MasteringJS: Array splice https://masteringjs.io/tutorials/fundamentals/array-splice
*/
splice() {
let ret;
const arr = utils.isMongooseArray(this) ? this.__array : this;
this._markModified();
_checkManualPopulation(this, Array.prototype.slice.call(arguments, 2));
if (arguments.length) {
let vals;
if (this[arraySchemaSymbol] == null) {
vals = arguments;
} else {
vals = [];
for (let i = 0; i < arguments.length; ++i) {
vals[i] = i < 2 ?
arguments[i] :
this._cast(arguments[i], arguments[0] + (i - 2));
}
}
ret = [].splice.apply(arr, vals);
this._registerAtomic('$set', this);
}
return ret;
},
/*!
* ignore
*/
toBSON() {
return this.toObject(internalToObjectOptions);
},
/**
* Returns a native js Array.
*
* @param {Object} options
* @return {Array}
* @api public
* @method toObject
* @memberOf MongooseArray
*/
toObject(options) {
const arr = utils.isMongooseArray(this) ? this.__array : this;
if (options && options.depopulate) {
options = clone(options);
options._isNested = true;
// Ensure return value is a vanilla array, because in Node.js 6+ `map()`
// is smart enough to use the inherited array's constructor.
return [].concat(arr).map(function(doc) {
return doc instanceof Document
? doc.toObject(options)
: doc;
});
}
return [].concat(arr);
},
$toObject() {
return this.constructor.prototype.toObject.apply(this, arguments);
},
/**
* Wraps [`Array#unshift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking.
*
* #### Note:
*
* _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._
*
* @api public
* @method unshift
* @memberOf MongooseArray
*/
unshift() {
_checkManualPopulation(this, arguments);
let values;
if (this[arraySchemaSymbol] == null) {
values = arguments;
} else {
values = [].map.call(arguments, this._cast, this);
}
const arr = utils.isMongooseArray(this) ? this.__array : this;
this._markModified();
[].unshift.apply(arr, values);
this._registerAtomic('$set', this);
return this.length;
}
};
/*!
* ignore
*/
function _isAllSubdocs(docs, ref) {
if (!ref) {
return false;
}
for (const arg of docs) {
if (arg == null) {
return false;
}
const model = arg.constructor;
if (!(arg instanceof Document) ||
(model.modelName !== ref && model.baseModelName !== ref)) {
return false;
}
}
return true;
}
/*!
* Minimize _just_ empty objects along the path chain specified
* by `parts`, ignoring all other paths. Useful in cases where
* you want to minimize after unsetting a path.
*
* #### Example:
*
* const obj = { foo: { bar: { baz: {} } }, a: {} };
* _minimizePath(obj, 'foo.bar.baz');
* obj; // { a: {} }
*/
function _minimizePath(obj, parts, i) {
if (typeof parts === 'string') {
if (parts.indexOf('.') === -1) {
return;
}
parts = mpath.stringToParts(parts);
}
i = i || 0;
if (i >= parts.length) {
return;
}
if (obj == null || typeof obj !== 'object') {
return;
}
_minimizePath(obj[parts[0]], parts, i + 1);
if (obj[parts[0]] != null && typeof obj[parts[0]] === 'object' && Object.keys(obj[parts[0]]).length === 0) {
delete obj[parts[0]];
}
}
/*!
* ignore
*/
function _checkManualPopulation(arr, docs) {
const ref = arr == null ?
null :
arr[arraySchemaSymbol] && arr[arraySchemaSymbol].caster && arr[arraySchemaSymbol].caster.options && arr[arraySchemaSymbol].caster.options.ref || null;