mongoose 8.24.5 → 8.24.6
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/dist/browser.umd.js +1 -1
- package/lib/cast/double.js +6 -0
- package/lib/cast/int32.js +6 -0
- package/lib/helpers/clone.js +9 -0
- package/lib/helpers/model/castBulkWrite.js +7 -5
- package/lib/helpers/projection/isPathSelectedInclusive.js +1 -11
- package/lib/helpers/query/cast$expr.js +1 -1
- package/lib/queryHelpers.js +17 -0
- package/lib/types/array/methods/index.js +3 -1
- package/package.json +1 -1
package/lib/cast/double.js
CHANGED
|
@@ -30,6 +30,12 @@ module.exports = function castDouble(val) {
|
|
|
30
30
|
assert.ok(false);
|
|
31
31
|
}
|
|
32
32
|
} else if (typeof val === 'object') {
|
|
33
|
+
if (Array.isArray(val)) {
|
|
34
|
+
// `[5].valueOf()` returns the array itself, so without this guard a
|
|
35
|
+
// single-element or empty array would fall through to `Number(tempVal)`
|
|
36
|
+
// below and silently coerce instead of throwing.
|
|
37
|
+
assert.ok(false);
|
|
38
|
+
}
|
|
33
39
|
const tempVal = val.valueOf() ?? val.toString();
|
|
34
40
|
// ex: { a: 'im an object, valueOf: () => 'helloworld' } // throw an error
|
|
35
41
|
if (typeof tempVal === 'string') {
|
package/lib/cast/int32.js
CHANGED
|
@@ -20,6 +20,12 @@ module.exports = function castInt32(val) {
|
|
|
20
20
|
if (val === '') {
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
|
+
if (Array.isArray(val)) {
|
|
24
|
+
// `Number([5])` is `5` and `Number([])` is `0`, so without this guard a
|
|
25
|
+
// single-element or empty array would silently coerce instead of throwing,
|
|
26
|
+
// unlike every other array input and unlike `castNumber()`'s own guard.
|
|
27
|
+
assert.ok(false);
|
|
28
|
+
}
|
|
23
29
|
|
|
24
30
|
const coercedVal = isBsonType(val, 'Long') ? val.toNumber() : Number(val);
|
|
25
31
|
|
package/lib/helpers/clone.js
CHANGED
|
@@ -197,6 +197,11 @@ function cloneObject(obj, options, isArrayChild) {
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
function cloneArray(arr, options) {
|
|
200
|
+
const seen = options?._seen;
|
|
201
|
+
if (seen && seen.has(arr)) {
|
|
202
|
+
return seen.get(arr);
|
|
203
|
+
}
|
|
204
|
+
|
|
200
205
|
let i = 0;
|
|
201
206
|
const len = arr.length;
|
|
202
207
|
|
|
@@ -218,6 +223,10 @@ function cloneArray(arr, options) {
|
|
|
218
223
|
ret = new Array(len);
|
|
219
224
|
}
|
|
220
225
|
|
|
226
|
+
if (seen) {
|
|
227
|
+
seen.set(arr, ret);
|
|
228
|
+
}
|
|
229
|
+
|
|
221
230
|
arr = isMongooseArray(arr) ? arr.__array : arr;
|
|
222
231
|
if (ret.isMongooseDocumentArray) {
|
|
223
232
|
// Create new options object to avoid mutating the shared options.
|
|
@@ -178,13 +178,15 @@ module.exports.castUpdateMany = function castUpdateMany(originalModel, updateMan
|
|
|
178
178
|
const schema = model.schema;
|
|
179
179
|
const strict = options.strict != null ? options.strict : model.schema.options.strict;
|
|
180
180
|
|
|
181
|
+
const update = clone(updateMany['update']);
|
|
182
|
+
|
|
181
183
|
const globalSetDefaultsOnInsert = originalModel.base.options.setDefaultsOnInsert;
|
|
182
184
|
const shouldSetDefaultsOnInsert = updateMany.setDefaultsOnInsert == null ?
|
|
183
185
|
globalSetDefaultsOnInsert :
|
|
184
186
|
updateMany.setDefaultsOnInsert;
|
|
185
187
|
|
|
186
188
|
if (shouldSetDefaultsOnInsert !== false) {
|
|
187
|
-
setDefaultsOnInsert(updateMany['filter'], model.schema,
|
|
189
|
+
setDefaultsOnInsert(updateMany['filter'], model.schema, update, {
|
|
188
190
|
setDefaultsOnInsert: true,
|
|
189
191
|
upsert: updateMany.upsert
|
|
190
192
|
});
|
|
@@ -195,19 +197,19 @@ module.exports.castUpdateMany = function castUpdateMany(originalModel, updateMan
|
|
|
195
197
|
if (model.schema.$timestamps != null && doInitTimestamps) {
|
|
196
198
|
const createdAt = model.schema.$timestamps.createdAt;
|
|
197
199
|
const updatedAt = model.schema.$timestamps.updatedAt;
|
|
198
|
-
applyTimestampsToUpdate(now, createdAt, updatedAt,
|
|
200
|
+
applyTimestampsToUpdate(now, createdAt, updatedAt, update, {
|
|
199
201
|
timestamps: updateMany.timestamps,
|
|
200
202
|
overwriteImmutable: updateMany.overwriteImmutable
|
|
201
203
|
});
|
|
202
204
|
}
|
|
203
205
|
if (doInitTimestamps) {
|
|
204
|
-
applyTimestampsToChildren(now,
|
|
206
|
+
applyTimestampsToChildren(now, update, model.schema);
|
|
205
207
|
}
|
|
206
208
|
|
|
207
209
|
_addDiscriminatorToObject(schema, updateMany['filter']);
|
|
208
210
|
|
|
209
211
|
decorateUpdateWithVersionKey(
|
|
210
|
-
|
|
212
|
+
update,
|
|
211
213
|
updateMany,
|
|
212
214
|
model.schema.options.versionKey
|
|
213
215
|
);
|
|
@@ -217,7 +219,7 @@ module.exports.castUpdateMany = function castUpdateMany(originalModel, updateMan
|
|
|
217
219
|
upsert: updateMany.upsert
|
|
218
220
|
});
|
|
219
221
|
|
|
220
|
-
updateMany['update'] = castUpdate(model.schema,
|
|
222
|
+
updateMany['update'] = castUpdate(model.schema, update, {
|
|
221
223
|
strict: strict,
|
|
222
224
|
upsert: updateMany.upsert,
|
|
223
225
|
arrayFilters: updateMany.arrayFilters,
|
|
@@ -7,19 +7,9 @@
|
|
|
7
7
|
module.exports = function isPathSelectedInclusive(fields, path) {
|
|
8
8
|
const chunks = path.split('.');
|
|
9
9
|
let cur = '';
|
|
10
|
-
let j;
|
|
11
|
-
let keys;
|
|
12
|
-
let numKeys;
|
|
13
10
|
for (let i = 0; i < chunks.length; ++i) {
|
|
14
|
-
cur += cur.length ? '.'
|
|
11
|
+
cur += cur.length ? '.' + chunks[i] : chunks[i];
|
|
15
12
|
if (fields[cur]) {
|
|
16
|
-
keys = Object.keys(fields);
|
|
17
|
-
numKeys = keys.length;
|
|
18
|
-
for (j = 0; j < numKeys; ++j) {
|
|
19
|
-
if (keys[i].indexOf(cur + '.') === 0 && keys[i].indexOf(path) !== 0) {
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
13
|
return true;
|
|
24
14
|
}
|
|
25
15
|
}
|
package/lib/queryHelpers.js
CHANGED
|
@@ -204,6 +204,13 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
204
204
|
switch (exclude) {
|
|
205
205
|
case true:
|
|
206
206
|
for (const fieldName of excluded) {
|
|
207
|
+
// Skip if an ancestor path is already excluded in `fields` (for
|
|
208
|
+
// example the user did `.select('-subd')` and `subd.raw` has
|
|
209
|
+
// schema-level `select: false`) to avoid MongoDB's "Path collision"
|
|
210
|
+
// error from projecting both `subd` and `subd.raw`. See gh-12798
|
|
211
|
+
if (hasExcludedAncestor(fields, fieldName)) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
207
214
|
fields[fieldName] = 0;
|
|
208
215
|
}
|
|
209
216
|
break;
|
|
@@ -370,6 +377,16 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
370
377
|
(type.selected ? selected : excluded).push(path);
|
|
371
378
|
return path;
|
|
372
379
|
}
|
|
380
|
+
|
|
381
|
+
function hasExcludedAncestor(fields, path) {
|
|
382
|
+
let i = -1;
|
|
383
|
+
while ((i = path.indexOf('.', i + 1)) !== -1) {
|
|
384
|
+
if (fields[path.slice(0, i)] === 0) {
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
373
390
|
};
|
|
374
391
|
|
|
375
392
|
/**
|
|
@@ -545,11 +545,13 @@ const methods = {
|
|
|
545
545
|
if (isBsonType(obj, 'ObjectId')) {
|
|
546
546
|
obj = obj.toString();
|
|
547
547
|
}
|
|
548
|
+
// Compare dates by timestamp like `addToSet()` does, so `pull()` works on date arrays
|
|
549
|
+
const isDate = obj instanceof Date;
|
|
548
550
|
|
|
549
551
|
fromIndex = fromIndex == null ? 0 : fromIndex;
|
|
550
552
|
const len = this.length;
|
|
551
553
|
for (let i = fromIndex; i < len; ++i) {
|
|
552
|
-
if (obj == this[i]) {
|
|
554
|
+
if (obj == this[i] || (isDate && this[i] instanceof Date && +obj === +this[i])) {
|
|
553
555
|
return i;
|
|
554
556
|
}
|
|
555
557
|
}
|