mongoose 6.1.9 → 6.2.2
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 +154 -0
- package/CHANGELOG.md +59 -0
- package/dist/browser.umd.js +233 -222
- package/index.js +5 -1
- package/lib/aggregate.js +23 -28
- package/lib/browserDocument.js +1 -1
- package/lib/cast/number.js +2 -3
- package/lib/cast.js +9 -7
- package/lib/connection.js +76 -24
- package/lib/cursor/AggregationCursor.js +12 -7
- package/lib/cursor/QueryCursor.js +11 -6
- package/lib/document.js +107 -107
- package/lib/drivers/node-mongodb-native/collection.js +12 -4
- package/lib/drivers/node-mongodb-native/connection.js +11 -0
- package/lib/error/cast.js +3 -2
- package/lib/error/index.js +11 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/helpers/clone.js +51 -29
- package/lib/helpers/common.js +2 -2
- package/lib/helpers/cursor/eachAsync.js +18 -15
- package/lib/helpers/document/compile.js +7 -4
- package/lib/helpers/getFunctionName.js +6 -4
- package/lib/helpers/isMongooseObject.js +9 -8
- package/lib/helpers/isObject.js +4 -4
- package/lib/helpers/model/discriminator.js +2 -1
- package/lib/helpers/path/parentPaths.js +10 -5
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +4 -2
- package/lib/helpers/populate/assignVals.js +8 -4
- package/lib/helpers/populate/getModelsMapForPopulate.js +4 -4
- package/lib/helpers/populate/markArraySubdocsPopulated.js +3 -1
- package/lib/helpers/populate/modelNamesFromRefPath.js +4 -3
- package/lib/helpers/printJestWarning.js +2 -2
- package/lib/helpers/projection/applyProjection.js +77 -0
- package/lib/helpers/projection/hasIncludedChildren.js +36 -0
- package/lib/helpers/projection/isExclusive.js +5 -2
- package/lib/helpers/projection/isInclusive.js +5 -1
- package/lib/helpers/query/cast$expr.js +279 -0
- package/lib/helpers/query/castUpdate.js +6 -2
- package/lib/helpers/query/isOperator.js +5 -2
- package/lib/helpers/schema/applyPlugins.js +11 -0
- package/lib/helpers/schema/getPath.js +4 -2
- package/lib/helpers/timestamps/setupTimestamps.js +3 -8
- package/lib/index.js +28 -26
- package/lib/internal.js +1 -1
- package/lib/model.js +161 -122
- package/lib/options/SchemaTypeOptions.js +1 -1
- package/lib/plugins/trackTransaction.js +5 -4
- package/lib/query.js +159 -146
- package/lib/queryhelpers.js +10 -10
- package/lib/schema/SubdocumentPath.js +4 -3
- package/lib/schema/array.js +30 -21
- package/lib/schema/buffer.js +1 -1
- package/lib/schema/date.js +1 -1
- package/lib/schema/decimal128.js +1 -1
- package/lib/schema/documentarray.js +9 -11
- package/lib/schema/number.js +1 -1
- package/lib/schema/objectid.js +2 -2
- package/lib/schema/string.js +4 -4
- package/lib/schema.js +9 -8
- package/lib/schematype.js +77 -30
- package/lib/types/ArraySubdocument.js +2 -1
- package/lib/types/DocumentArray/index.js +10 -27
- package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/DocumentArray/methods/index.js +15 -3
- package/lib/types/array/index.js +22 -21
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +22 -23
- package/lib/types/buffer.js +3 -3
- package/lib/types/map.js +2 -3
- package/lib/utils.js +10 -7
- package/package.json +19 -151
- package/tools/repl.js +1 -1
- package/tsconfig.json +8 -0
- package/types/PipelineStage.d.ts +272 -0
- package/{index.d.ts → types/index.d.ts} +156 -357
- package/lib/types/array/ArrayWrapper.js +0 -981
|
@@ -15,7 +15,7 @@ const arrayPathSymbol = require('../../helpers/symbols').arrayPathSymbol;
|
|
|
15
15
|
const arraySchemaSymbol = require('../../helpers/symbols').arraySchemaSymbol;
|
|
16
16
|
|
|
17
17
|
const _basePush = Array.prototype.push;
|
|
18
|
-
|
|
18
|
+
const numberRE = /^\d+$/;
|
|
19
19
|
/**
|
|
20
20
|
* DocumentArray constructor
|
|
21
21
|
*
|
|
@@ -25,11 +25,11 @@ const _basePush = Array.prototype.push;
|
|
|
25
25
|
* @api private
|
|
26
26
|
* @return {MongooseDocumentArray}
|
|
27
27
|
* @inherits MongooseArray
|
|
28
|
-
* @see
|
|
28
|
+
* @see https://bit.ly/f6CnZU
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
31
|
function MongooseDocumentArray(values, path, doc) {
|
|
32
|
-
const
|
|
32
|
+
const __array = [];
|
|
33
33
|
|
|
34
34
|
const internals = {
|
|
35
35
|
[arrayAtomicsSymbol]: {},
|
|
@@ -45,10 +45,11 @@ function MongooseDocumentArray(values, path, doc) {
|
|
|
45
45
|
internals[arrayAtomicsSymbol] = Object.assign({}, values[arrayAtomicsSymbol]);
|
|
46
46
|
}
|
|
47
47
|
values.forEach(v => {
|
|
48
|
-
_basePush.call(
|
|
48
|
+
_basePush.call(__array, v);
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
internals[arrayPathSymbol] = path;
|
|
52
|
+
internals.__array = __array;
|
|
52
53
|
|
|
53
54
|
// Because doc comes from the context of another function, doc === global
|
|
54
55
|
// can happen if there was a null somewhere up the chain (see #3020 && #3034)
|
|
@@ -69,7 +70,7 @@ function MongooseDocumentArray(values, path, doc) {
|
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
const proxy = new Proxy(
|
|
73
|
+
const proxy = new Proxy(__array, {
|
|
73
74
|
get: function(target, prop) {
|
|
74
75
|
if (prop === 'isMongooseArray' ||
|
|
75
76
|
prop === 'isMongooseArrayProxy' ||
|
|
@@ -77,12 +78,6 @@ function MongooseDocumentArray(values, path, doc) {
|
|
|
77
78
|
prop === 'isMongooseDocumentArrayProxy') {
|
|
78
79
|
return true;
|
|
79
80
|
}
|
|
80
|
-
if (prop === '__array') {
|
|
81
|
-
return arr;
|
|
82
|
-
}
|
|
83
|
-
if (prop === 'set') {
|
|
84
|
-
return set;
|
|
85
|
-
}
|
|
86
81
|
if (internals.hasOwnProperty(prop)) {
|
|
87
82
|
return internals[prop];
|
|
88
83
|
}
|
|
@@ -93,15 +88,15 @@ function MongooseDocumentArray(values, path, doc) {
|
|
|
93
88
|
return ArrayMethods[prop];
|
|
94
89
|
}
|
|
95
90
|
|
|
96
|
-
return
|
|
91
|
+
return __array[prop];
|
|
97
92
|
},
|
|
98
93
|
set: function(target, prop, value) {
|
|
99
|
-
if (typeof prop === 'string' &&
|
|
100
|
-
set.call(proxy, prop, value);
|
|
94
|
+
if (typeof prop === 'string' && numberRE.test(prop)) {
|
|
95
|
+
DocumentArrayMethods.set.call(proxy, prop, value, false);
|
|
101
96
|
} else if (internals.hasOwnProperty(prop)) {
|
|
102
97
|
internals[prop] = value;
|
|
103
98
|
} else {
|
|
104
|
-
|
|
99
|
+
__array[prop] = value;
|
|
105
100
|
}
|
|
106
101
|
|
|
107
102
|
return true;
|
|
@@ -111,18 +106,6 @@ function MongooseDocumentArray(values, path, doc) {
|
|
|
111
106
|
return proxy;
|
|
112
107
|
}
|
|
113
108
|
|
|
114
|
-
function set(i, val, skipModified) {
|
|
115
|
-
const arr = this.__array;
|
|
116
|
-
if (skipModified) {
|
|
117
|
-
arr[i] = val;
|
|
118
|
-
return arr;
|
|
119
|
-
}
|
|
120
|
-
const value = DocumentArrayMethods._cast.call(this, val, i);
|
|
121
|
-
arr[i] = value;
|
|
122
|
-
DocumentArrayMethods._markModified.call(this, i);
|
|
123
|
-
return arr;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
109
|
/*!
|
|
127
110
|
* Module exports.
|
|
128
111
|
*/
|
|
@@ -36,7 +36,7 @@ const methods = {
|
|
|
36
36
|
}
|
|
37
37
|
let Constructor = this[arraySchemaSymbol].casterConstructor;
|
|
38
38
|
const isInstance = Constructor.$isMongooseDocumentArray ?
|
|
39
|
-
|
|
39
|
+
utils.isMongooseDocumentArray(value) :
|
|
40
40
|
value instanceof Constructor;
|
|
41
41
|
if (isInstance ||
|
|
42
42
|
// Hack re: #5001, see #5005
|
|
@@ -295,7 +295,7 @@ const methods = {
|
|
|
295
295
|
break;
|
|
296
296
|
}
|
|
297
297
|
|
|
298
|
-
if (_arr[i]
|
|
298
|
+
if (utils.isMongooseArray(_arr[i])) {
|
|
299
299
|
notify(val, _arr[i]);
|
|
300
300
|
} else if (_arr[i]) {
|
|
301
301
|
_arr[i].emit(event, val);
|
|
@@ -304,6 +304,18 @@ const methods = {
|
|
|
304
304
|
};
|
|
305
305
|
},
|
|
306
306
|
|
|
307
|
+
set(i, val, skipModified) {
|
|
308
|
+
const arr = this.__array;
|
|
309
|
+
if (skipModified) {
|
|
310
|
+
arr[i] = val;
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
const value = methods._cast.call(this, val, i);
|
|
314
|
+
arr[i] = value;
|
|
315
|
+
methods._markModified.call(this, i);
|
|
316
|
+
return this;
|
|
317
|
+
},
|
|
318
|
+
|
|
307
319
|
_markModified(elem, embeddedPath) {
|
|
308
320
|
const parent = this[arrayParentSymbol];
|
|
309
321
|
let dirtyPath;
|
|
@@ -326,7 +338,7 @@ const methods = {
|
|
|
326
338
|
return this;
|
|
327
339
|
}
|
|
328
340
|
|
|
329
|
-
parent.markModified(dirtyPath, arguments.length
|
|
341
|
+
parent.markModified(dirtyPath, arguments.length !== 0 ? elem : parent);
|
|
330
342
|
}
|
|
331
343
|
|
|
332
344
|
return this;
|
package/lib/types/array/index.js
CHANGED
|
@@ -25,11 +25,14 @@ const arraySchemaSymbol = require('../../helpers/symbols').arraySchemaSymbol;
|
|
|
25
25
|
* @param {Document} doc parent document
|
|
26
26
|
* @api private
|
|
27
27
|
* @inherits Array
|
|
28
|
-
* @see
|
|
28
|
+
* @see https://bit.ly/f6CnZU
|
|
29
29
|
*/
|
|
30
|
+
const _basePush = Array.prototype.push;
|
|
31
|
+
const numberRE = /^\d+$/;
|
|
30
32
|
|
|
31
33
|
function MongooseArray(values, path, doc, schematype) {
|
|
32
|
-
let
|
|
34
|
+
let __array;
|
|
35
|
+
|
|
33
36
|
if (Array.isArray(values)) {
|
|
34
37
|
const len = values.length;
|
|
35
38
|
|
|
@@ -38,21 +41,21 @@ function MongooseArray(values, path, doc, schematype) {
|
|
|
38
41
|
// modifying the array is faster. Seems small, but adds up when you have a document
|
|
39
42
|
// with thousands of nested arrays.
|
|
40
43
|
if (len === 0) {
|
|
41
|
-
|
|
44
|
+
__array = new Array();
|
|
42
45
|
} else if (len === 1) {
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
__array = new Array(1);
|
|
47
|
+
__array[0] = values[0];
|
|
45
48
|
} else if (len < 10000) {
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
__array = new Array();
|
|
50
|
+
_basePush.apply(__array, values);
|
|
48
51
|
} else {
|
|
49
|
-
|
|
52
|
+
__array = new Array();
|
|
50
53
|
for (let i = 0; i < len; ++i) {
|
|
51
|
-
|
|
54
|
+
_basePush.call(__array, values[i]);
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
} else {
|
|
55
|
-
|
|
58
|
+
__array = [];
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
const internals = {
|
|
@@ -63,10 +66,10 @@ function MongooseArray(values, path, doc, schematype) {
|
|
|
63
66
|
[arrayParentSymbol]: void 0,
|
|
64
67
|
isMongooseArray: true,
|
|
65
68
|
isMongooseArrayProxy: true,
|
|
66
|
-
__array:
|
|
69
|
+
__array: __array
|
|
67
70
|
};
|
|
68
71
|
|
|
69
|
-
if (values[arrayAtomicsSymbol] != null) {
|
|
72
|
+
if (values && values[arrayAtomicsSymbol] != null) {
|
|
70
73
|
internals[arrayAtomicsSymbol] = values[arrayAtomicsSymbol];
|
|
71
74
|
}
|
|
72
75
|
|
|
@@ -79,7 +82,7 @@ function MongooseArray(values, path, doc, schematype) {
|
|
|
79
82
|
internals[arraySchemaSymbol] = schematype || doc.schema.path(path);
|
|
80
83
|
}
|
|
81
84
|
|
|
82
|
-
const proxy = new Proxy(
|
|
85
|
+
const proxy = new Proxy(__array, {
|
|
83
86
|
get: function(target, prop) {
|
|
84
87
|
if (internals.hasOwnProperty(prop)) {
|
|
85
88
|
return internals[prop];
|
|
@@ -88,17 +91,15 @@ function MongooseArray(values, path, doc, schematype) {
|
|
|
88
91
|
return mongooseArrayMethods[prop];
|
|
89
92
|
}
|
|
90
93
|
|
|
91
|
-
return
|
|
94
|
+
return __array[prop];
|
|
92
95
|
},
|
|
93
|
-
set: function(target, prop,
|
|
94
|
-
if (typeof prop === 'string' &&
|
|
95
|
-
|
|
96
|
-
arr[prop] = value;
|
|
97
|
-
mongooseArrayMethods._markModified.call(proxy, prop);
|
|
96
|
+
set: function(target, prop, value) {
|
|
97
|
+
if (typeof prop === 'string' && numberRE.test(prop)) {
|
|
98
|
+
mongooseArrayMethods.set.call(proxy, prop, value, false);
|
|
98
99
|
} else if (internals.hasOwnProperty(prop)) {
|
|
99
|
-
internals[prop] =
|
|
100
|
+
internals[prop] = value;
|
|
100
101
|
} else {
|
|
101
|
-
|
|
102
|
+
__array[prop] = value;
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
return true;
|
|
@@ -5,7 +5,6 @@ const ArraySubdocument = require('../../ArraySubdocument');
|
|
|
5
5
|
const MongooseError = require('../../../error/mongooseError');
|
|
6
6
|
const ObjectId = require('../../objectid');
|
|
7
7
|
const cleanModifiedSubpaths = require('../../../helpers/document/cleanModifiedSubpaths');
|
|
8
|
-
const get = require('../../../helpers/get');
|
|
9
8
|
const internalToObjectOptions = require('../../../options').internalToObjectOptions;
|
|
10
9
|
const utils = require('../../../utils');
|
|
11
10
|
|
|
@@ -104,7 +103,7 @@ const methods = {
|
|
|
104
103
|
* ####NOTE:
|
|
105
104
|
*
|
|
106
105
|
* _Calling this multiple times on an array before saving sends the same command as calling it once._
|
|
107
|
-
* _This update is implemented using the MongoDB [$pop](
|
|
106
|
+
* _This update is implemented using the MongoDB [$pop](https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop) method which enforces this restriction._
|
|
108
107
|
*
|
|
109
108
|
* doc.array = [1,2,3];
|
|
110
109
|
*
|
|
@@ -129,7 +128,7 @@ const methods = {
|
|
|
129
128
|
* @memberOf MongooseArray
|
|
130
129
|
* @instance
|
|
131
130
|
* @method $shift
|
|
132
|
-
* @see mongodb
|
|
131
|
+
* @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop
|
|
133
132
|
*/
|
|
134
133
|
|
|
135
134
|
$shift() {
|
|
@@ -151,7 +150,7 @@ const methods = {
|
|
|
151
150
|
* #### NOTE:
|
|
152
151
|
*
|
|
153
152
|
* _Calling this mulitple times on an array before saving sends the same command as calling it once._
|
|
154
|
-
* _This update is implemented using the MongoDB [$pop](
|
|
153
|
+
* _This update is implemented using the MongoDB [$pop](https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop) method which enforces this restriction._
|
|
155
154
|
*
|
|
156
155
|
* doc.array = [1,2,3];
|
|
157
156
|
*
|
|
@@ -176,7 +175,7 @@ const methods = {
|
|
|
176
175
|
* @method $pop
|
|
177
176
|
* @memberOf MongooseArray
|
|
178
177
|
* @instance
|
|
179
|
-
* @see mongodb
|
|
178
|
+
* @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop
|
|
180
179
|
* @method $pop
|
|
181
180
|
* @memberOf MongooseArray
|
|
182
181
|
*/
|
|
@@ -285,7 +284,7 @@ const methods = {
|
|
|
285
284
|
return this;
|
|
286
285
|
}
|
|
287
286
|
|
|
288
|
-
parent.markModified(dirtyPath, arguments.length
|
|
287
|
+
parent.markModified(dirtyPath, arguments.length !== 0 ? elem : parent);
|
|
289
288
|
}
|
|
290
289
|
|
|
291
290
|
return this;
|
|
@@ -393,8 +392,8 @@ const methods = {
|
|
|
393
392
|
type = 'date';
|
|
394
393
|
}
|
|
395
394
|
|
|
396
|
-
const rawValues = values
|
|
397
|
-
const rawArray = this
|
|
395
|
+
const rawValues = utils.isMongooseArray(values) ? values.__array : this;
|
|
396
|
+
const rawArray = utils.isMongooseArray(this) ? this.__array : this;
|
|
398
397
|
|
|
399
398
|
rawValues.forEach(function(v) {
|
|
400
399
|
let found;
|
|
@@ -520,7 +519,7 @@ const methods = {
|
|
|
520
519
|
*
|
|
521
520
|
* ####Note:
|
|
522
521
|
*
|
|
523
|
-
* _marks the entire array as modified which will pass the entire thing to $set potentially
|
|
522
|
+
* _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._
|
|
524
523
|
*
|
|
525
524
|
* @see MongooseArray#$pop #types_array_MongooseArray-%24pop
|
|
526
525
|
* @api public
|
|
@@ -538,7 +537,7 @@ const methods = {
|
|
|
538
537
|
/**
|
|
539
538
|
* Pulls items from the array atomically. Equality is determined by casting
|
|
540
539
|
* the provided value to an embedded document and comparing using
|
|
541
|
-
* [the `Document.equals()` function.](
|
|
540
|
+
* [the `Document.equals()` function.](/docs/api.html#document_Document-equals)
|
|
542
541
|
*
|
|
543
542
|
* ####Examples:
|
|
544
543
|
*
|
|
@@ -560,7 +559,7 @@ const methods = {
|
|
|
560
559
|
* 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.
|
|
561
560
|
*
|
|
562
561
|
* @param {any} [args...]
|
|
563
|
-
* @see mongodb
|
|
562
|
+
* @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pull
|
|
564
563
|
* @api public
|
|
565
564
|
* @method pull
|
|
566
565
|
* @memberOf MongooseArray
|
|
@@ -638,7 +637,7 @@ const methods = {
|
|
|
638
637
|
let atomic = values;
|
|
639
638
|
const isOverwrite = values[0] != null &&
|
|
640
639
|
utils.hasUserDefinedProperty(values[0], '$each');
|
|
641
|
-
const arr = this
|
|
640
|
+
const arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
642
641
|
if (isOverwrite) {
|
|
643
642
|
atomic = values[0];
|
|
644
643
|
values = values[0].$each;
|
|
@@ -660,7 +659,7 @@ const methods = {
|
|
|
660
659
|
if (isOverwrite) {
|
|
661
660
|
atomic.$each = values;
|
|
662
661
|
|
|
663
|
-
if (
|
|
662
|
+
if ((atomics.$push && atomics.$push.$each && atomics.$push.$each.length || 0) !== 0 &&
|
|
664
663
|
atomics.$push.$position != atomic.$position) {
|
|
665
664
|
throw new MongooseError('Cannot call `Array#push()` multiple times ' +
|
|
666
665
|
'with different `$position`');
|
|
@@ -673,7 +672,7 @@ const methods = {
|
|
|
673
672
|
ret = [].push.apply(arr, values);
|
|
674
673
|
}
|
|
675
674
|
} else {
|
|
676
|
-
if (
|
|
675
|
+
if ((atomics.$push && atomics.$push.$each && atomics.$push.$each.length || 0) !== 0 &&
|
|
677
676
|
atomics.$push.$position != null) {
|
|
678
677
|
throw new MongooseError('Cannot call `Array#push()` multiple times ' +
|
|
679
678
|
'with different `$position`');
|
|
@@ -691,7 +690,7 @@ const methods = {
|
|
|
691
690
|
* Alias of [pull](#mongoosearray_MongooseArray-pull)
|
|
692
691
|
*
|
|
693
692
|
* @see MongooseArray#pull #types_array_MongooseArray-pull
|
|
694
|
-
* @see mongodb
|
|
693
|
+
* @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pull
|
|
695
694
|
* @api public
|
|
696
695
|
* @memberOf MongooseArray
|
|
697
696
|
* @instance
|
|
@@ -761,7 +760,7 @@ const methods = {
|
|
|
761
760
|
*/
|
|
762
761
|
|
|
763
762
|
shift() {
|
|
764
|
-
const arr = this
|
|
763
|
+
const arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
765
764
|
const ret = [].shift.call(arr);
|
|
766
765
|
this._registerAtomic('$set', this);
|
|
767
766
|
this._markModified();
|
|
@@ -782,7 +781,7 @@ const methods = {
|
|
|
782
781
|
*/
|
|
783
782
|
|
|
784
783
|
sort() {
|
|
785
|
-
const arr = this
|
|
784
|
+
const arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
786
785
|
const ret = [].sort.apply(arr, arguments);
|
|
787
786
|
this._registerAtomic('$set', this);
|
|
788
787
|
return ret;
|
|
@@ -803,7 +802,7 @@ const methods = {
|
|
|
803
802
|
|
|
804
803
|
splice() {
|
|
805
804
|
let ret;
|
|
806
|
-
const arr = this
|
|
805
|
+
const arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
807
806
|
|
|
808
807
|
_checkManualPopulation(this, Array.prototype.slice.call(arguments, 2));
|
|
809
808
|
|
|
@@ -846,7 +845,7 @@ const methods = {
|
|
|
846
845
|
*/
|
|
847
846
|
|
|
848
847
|
toObject(options) {
|
|
849
|
-
const arr = this
|
|
848
|
+
const arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
850
849
|
if (options && options.depopulate) {
|
|
851
850
|
options = utils.clone(options);
|
|
852
851
|
options._isNested = true;
|
|
@@ -888,7 +887,7 @@ const methods = {
|
|
|
888
887
|
values = this[arraySchemaSymbol].applySetters(values, this[arrayParentSymbol]);
|
|
889
888
|
}
|
|
890
889
|
|
|
891
|
-
const arr = this
|
|
890
|
+
const arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
892
891
|
[].unshift.apply(arr, values);
|
|
893
892
|
this._registerAtomic('$set', this);
|
|
894
893
|
this._markModified();
|
|
@@ -926,9 +925,9 @@ function _isAllSubdocs(docs, ref) {
|
|
|
926
925
|
function _checkManualPopulation(arr, docs) {
|
|
927
926
|
const ref = arr == null ?
|
|
928
927
|
null :
|
|
929
|
-
|
|
928
|
+
arr[arraySchemaSymbol] && arr[arraySchemaSymbol].caster && arr[arraySchemaSymbol].caster.options && arr[arraySchemaSymbol].caster.options.ref || null;
|
|
930
929
|
if (arr.length === 0 &&
|
|
931
|
-
docs.length
|
|
930
|
+
docs.length !== 0) {
|
|
932
931
|
if (_isAllSubdocs(docs, ref)) {
|
|
933
932
|
arr[arrayParentSymbol].$populated(arr[arrayPathSymbol], [], {
|
|
934
933
|
[populateModelSymbol]: docs[0].constructor
|
|
@@ -950,7 +949,7 @@ for (const method of returnVanillaArrayMethods) {
|
|
|
950
949
|
}
|
|
951
950
|
|
|
952
951
|
methods[method] = function() {
|
|
953
|
-
const _arr = this
|
|
952
|
+
const _arr = utils.isMongooseArray(this) ? this.__array : this;
|
|
954
953
|
const arr = [].concat(_arr);
|
|
955
954
|
|
|
956
955
|
return arr[method].apply(arr, arguments);
|
package/lib/types/buffer.js
CHANGED
|
@@ -17,7 +17,7 @@ const utils = require('../utils');
|
|
|
17
17
|
* @param {Number} offset
|
|
18
18
|
* @api private
|
|
19
19
|
* @inherits Buffer
|
|
20
|
-
* @see
|
|
20
|
+
* @see https://bit.ly/f6CnZU
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
function MongooseBuffer(value, encode, offset) {
|
|
@@ -173,7 +173,7 @@ MongooseBuffer.mixin = {
|
|
|
173
173
|
*
|
|
174
174
|
* doc.buffer.toObject(bson.BSON_BINARY_SUBTYPE_USER_DEFINED);
|
|
175
175
|
*
|
|
176
|
-
* @see
|
|
176
|
+
* @see https://bsonspec.org/#/specification
|
|
177
177
|
* @param {Hex} [subtype]
|
|
178
178
|
* @return {Binary}
|
|
179
179
|
* @api public
|
|
@@ -245,7 +245,7 @@ MongooseBuffer.mixin.equals = function(other) {
|
|
|
245
245
|
*
|
|
246
246
|
* doc.buffer.subtype(bson.BSON_BINARY_SUBTYPE_UUID);
|
|
247
247
|
*
|
|
248
|
-
* @see
|
|
248
|
+
* @see https://bsonspec.org/#/specification
|
|
249
249
|
* @param {Hex} subtype
|
|
250
250
|
* @api public
|
|
251
251
|
* @method subtype
|
package/lib/types/map.js
CHANGED
|
@@ -4,7 +4,6 @@ const Mixed = require('../schema/mixed');
|
|
|
4
4
|
const ObjectId = require('./objectid');
|
|
5
5
|
const clone = require('../helpers/clone');
|
|
6
6
|
const deepEqual = require('../utils').deepEqual;
|
|
7
|
-
const get = require('../helpers/get');
|
|
8
7
|
const getConstructorName = require('../helpers/getConstructorName');
|
|
9
8
|
const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
|
|
10
9
|
const util = require('util');
|
|
@@ -131,7 +130,7 @@ class MongooseMap extends Map {
|
|
|
131
130
|
}
|
|
132
131
|
|
|
133
132
|
toObject(options) {
|
|
134
|
-
if (
|
|
133
|
+
if (options && options.flattenMaps) {
|
|
135
134
|
const ret = {};
|
|
136
135
|
const keys = this.keys();
|
|
137
136
|
for (const key of keys) {
|
|
@@ -148,7 +147,7 @@ class MongooseMap extends Map {
|
|
|
148
147
|
}
|
|
149
148
|
|
|
150
149
|
toJSON(options) {
|
|
151
|
-
if (
|
|
150
|
+
if (typeof (options && options.flattenMaps) === 'boolean' ? options.flattenMaps : true) {
|
|
152
151
|
const ret = {};
|
|
153
152
|
const keys = this.keys();
|
|
154
153
|
for (const key of keys) {
|
package/lib/utils.js
CHANGED
|
@@ -12,6 +12,8 @@ const PopulateOptions = require('./options/PopulateOptions');
|
|
|
12
12
|
const clone = require('./helpers/clone');
|
|
13
13
|
const immediate = require('./helpers/immediate');
|
|
14
14
|
const isObject = require('./helpers/isObject');
|
|
15
|
+
const isMongooseArray = require('./types/array/isMongooseArray');
|
|
16
|
+
const isMongooseDocumentArray = require('./types/DocumentArray/isMongooseDocumentArray');
|
|
15
17
|
const isBsonType = require('./helpers/isBsonType');
|
|
16
18
|
const getFunctionName = require('./helpers/getFunctionName');
|
|
17
19
|
const isMongooseObject = require('./helpers/isMongooseObject');
|
|
@@ -24,6 +26,11 @@ let Document;
|
|
|
24
26
|
|
|
25
27
|
exports.specialProperties = specialProperties;
|
|
26
28
|
|
|
29
|
+
exports.isMongooseArray = isMongooseArray.isMongooseArray;
|
|
30
|
+
exports.isMongooseDocumentArray = isMongooseDocumentArray.isMongooseDocumentArray;
|
|
31
|
+
exports.registerMongooseArray = isMongooseArray.registerMongooseArray;
|
|
32
|
+
exports.registerMongooseDocumentArray = isMongooseDocumentArray.registerMongooseDocumentArray;
|
|
33
|
+
|
|
27
34
|
/*!
|
|
28
35
|
* Produces a collection name from model `name`. By default, just returns
|
|
29
36
|
* the model name
|
|
@@ -488,13 +495,9 @@ exports.expires = function expires(object) {
|
|
|
488
495
|
return;
|
|
489
496
|
}
|
|
490
497
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
} else {
|
|
495
|
-
when = Math.round(ms(object.expires) / 1000);
|
|
496
|
-
}
|
|
497
|
-
object.expireAfterSeconds = when;
|
|
498
|
+
object.expireAfterSeconds = (typeof object.expires !== 'string')
|
|
499
|
+
? object.expires
|
|
500
|
+
: Math.round(ms(object.expires) / 1000);
|
|
498
501
|
delete object.expires;
|
|
499
502
|
};
|
|
500
503
|
|