mongoose 6.3.3 → 6.3.4

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/lib/document.js CHANGED
@@ -1323,6 +1323,10 @@ Document.prototype.$set = function $set(path, val, type, options) {
1323
1323
 
1324
1324
  if (!schema) {
1325
1325
  this.$__set(pathToMark, path, options, constructing, parts, schema, val, priorVal);
1326
+
1327
+ if (pathType === 'nested' && val == null) {
1328
+ cleanModifiedSubpaths(this, path);
1329
+ }
1326
1330
  return this;
1327
1331
  }
1328
1332
 
@@ -118,6 +118,7 @@ module.exports = clone;
118
118
 
119
119
  function cloneObject(obj, options, isArrayChild) {
120
120
  const minimize = options && options.minimize;
121
+ const omitUndefined = options && options.omitUndefined;
121
122
  const ret = {};
122
123
  let hasKeys;
123
124
 
@@ -138,7 +139,7 @@ function cloneObject(obj, options, isArrayChild) {
138
139
  // Don't pass `isArrayChild` down
139
140
  const val = clone(obj[key], options, false);
140
141
 
141
- if (minimize === false && typeof val === 'undefined') {
142
+ if ((minimize === false || omitUndefined) && typeof val === 'undefined') {
142
143
  delete ret[key];
143
144
  } else if (minimize !== true || (typeof val !== 'undefined')) {
144
145
  hasKeys || (hasKeys = true);
package/lib/model.js CHANGED
@@ -768,6 +768,7 @@ Model.prototype.$__delta = function() {
768
768
  transform: false,
769
769
  virtuals: false,
770
770
  getters: false,
771
+ omitUndefined: true,
771
772
  _isNested: true
772
773
  });
773
774
  operand(this, where, delta, data, value);
@@ -1488,61 +1489,68 @@ Model.syncIndexes = function syncIndexes(options, callback) {
1488
1489
  * the result of this function would be the result of
1489
1490
  * Model.syncIndexes().
1490
1491
  *
1491
- * @param {Object} options not used at all.
1492
+ * @param {Object} [options]
1492
1493
  * @param {Function} callback optional callback
1493
- * @returns {Promise} which containts an object, {toDrop, toCreate}, which
1494
- * are indexes that would be dropped in mongodb and indexes that would be created in mongodb.
1494
+ * @returns {Promise} which contains an object, {toDrop, toCreate}, which
1495
+ * are indexes that would be dropped in MongoDB and indexes that would be created in MongoDB.
1495
1496
  */
1496
1497
 
1497
1498
  Model.diffIndexes = function diffIndexes(options, callback) {
1499
+ if (typeof options === 'function') {
1500
+ callback = options;
1501
+ options = null;
1502
+ }
1498
1503
  const toDrop = [];
1499
1504
  const toCreate = [];
1500
1505
  callback = this.$handleCallbackError(callback);
1501
1506
  return this.db.base._promiseOrCallback(callback, cb => {
1502
1507
  cb = this.$wrapCallback(cb);
1503
- this.listIndexes((err, indexes) => {
1504
- if (indexes === undefined) {
1505
- indexes = [];
1508
+ this.listIndexes((err, dbIndexes) => {
1509
+ if (dbIndexes === undefined) {
1510
+ dbIndexes = [];
1506
1511
  }
1507
- const schemaIndexes = this.schema.indexes();
1508
- // Iterate through the indexes created in mongodb and
1509
- // compare against the indexes in the schema.
1510
- for (const index of indexes) {
1512
+ dbIndexes = getRelatedDBIndexes(this, dbIndexes);
1513
+ const schemaIndexes = getRelatedSchemaIndexes(this, this.schema.indexes());
1514
+
1515
+ for (const dbIndex of dbIndexes) {
1511
1516
  let found = false;
1512
1517
  // Never try to drop `_id` index, MongoDB server doesn't allow it
1513
- if (isDefaultIdIndex(index)) {
1518
+ if (isDefaultIdIndex(dbIndex)) {
1514
1519
  continue;
1515
1520
  }
1516
1521
 
1517
- for (const schemaIndex of schemaIndexes) {
1518
- const key = schemaIndex[0];
1519
- const options = decorateDiscriminatorIndexOptions(this.schema, utils.clone(schemaIndex[1]));
1520
- if (isIndexEqual(key, options, index)) {
1522
+ for (const [schemaIndexKeysObject, schemaIndexOptions] of schemaIndexes) {
1523
+ const options = decorateDiscriminatorIndexOptions(this.schema, utils.clone(schemaIndexOptions));
1524
+ applySchemaCollation(schemaIndexKeysObject, options, this.schema.options);
1525
+
1526
+ if (isIndexEqual(schemaIndexKeysObject, options, dbIndex)) {
1521
1527
  found = true;
1522
1528
  }
1523
1529
  }
1524
1530
 
1525
1531
  if (!found) {
1526
- toDrop.push(index.name);
1532
+ toDrop.push(dbIndex.name);
1527
1533
  }
1528
1534
  }
1529
1535
  // Iterate through the indexes created on the schema and
1530
1536
  // compare against the indexes in mongodb.
1531
- for (const schemaIndex of schemaIndexes) {
1532
- let found = false;
1533
- const key = schemaIndex[0];
1534
- const options = decorateDiscriminatorIndexOptions(this.schema, utils.clone(schemaIndex[1]));
1535
- for (const index of indexes) {
1536
- if (isDefaultIdIndex(index)) {
1537
- continue;
1537
+ if (!options || options.toCreate !== false) {
1538
+ for (const schemaIndex of schemaIndexes) {
1539
+ let found = false;
1540
+ const key = schemaIndex[0];
1541
+ const options = decorateDiscriminatorIndexOptions(this.schema, utils.clone(schemaIndex[1]));
1542
+ for (const index of dbIndexes) {
1543
+ if (isDefaultIdIndex(index)) {
1544
+ continue;
1545
+ }
1546
+ if (isIndexEqual(key, options, index)) {
1547
+ found = true;
1548
+ }
1538
1549
  }
1539
- if (isIndexEqual(key, options, index)) {
1540
- found = true;
1550
+ if (!found) {
1551
+ toCreate.push(key);
1541
1552
  }
1542
1553
  }
1543
- if (!found) {
1544
- toCreate.push(key);
1545
- }
1546
1554
  }
1547
1555
  cb(null, { toDrop, toCreate });
1548
1556
  });
@@ -1568,36 +1576,12 @@ Model.cleanIndexes = function cleanIndexes(callback) {
1568
1576
  return this.db.base._promiseOrCallback(callback, cb => {
1569
1577
  const collection = this.$__collection;
1570
1578
 
1571
- this.listIndexes((err, dbIndexes) => {
1579
+ this.diffIndexes({ toCreate: false }, (err, res) => {
1572
1580
  if (err != null) {
1573
1581
  return cb(err);
1574
1582
  }
1575
1583
 
1576
- dbIndexes = getRelatedDBIndexes(this, dbIndexes);
1577
- const schemaIndexes = getRelatedSchemaIndexes(this, this.schema.indexes());
1578
-
1579
- const toDrop = [];
1580
-
1581
- for (const dbIndex of dbIndexes) {
1582
- let found = false;
1583
- // Never try to drop `_id` index, MongoDB server doesn't allow it
1584
- if (isDefaultIdIndex(dbIndex)) {
1585
- continue;
1586
- }
1587
-
1588
- for (const [schemaIndexKeysObject, schemaIndexOptions] of schemaIndexes) {
1589
- const options = decorateDiscriminatorIndexOptions(this.schema, utils.clone(schemaIndexOptions));
1590
- applySchemaCollation(schemaIndexKeysObject, options, this.schema.options);
1591
-
1592
- if (isIndexEqual(schemaIndexKeysObject, options, dbIndex)) {
1593
- found = true;
1594
- }
1595
- }
1596
-
1597
- if (!found) {
1598
- toDrop.push(dbIndex.name);
1599
- }
1600
- }
1584
+ const toDrop = res.toDrop;
1601
1585
 
1602
1586
  if (toDrop.length === 0) {
1603
1587
  return cb(null, []);
@@ -226,7 +226,9 @@ exports.applyPaths = function applyPaths(fields, schema) {
226
226
  const addedPaths = [];
227
227
  schema.eachPath(function(path, type) {
228
228
  if (prefix) path = prefix + '.' + path;
229
-
229
+ if (type.$isSchemaMap || path.endsWith('.$*')) {
230
+ return;
231
+ }
230
232
  let addedPath = analyzePath(path, type);
231
233
  // arrays
232
234
  if (addedPath == null && !Array.isArray(type) && type.$isMongooseArray && !type.$isMongooseDocumentArray) {
@@ -248,7 +250,6 @@ exports.applyPaths = function applyPaths(fields, schema) {
248
250
  }
249
251
  }
250
252
  });
251
-
252
253
  stack.pop();
253
254
  return addedPaths;
254
255
  }
package/lib/schema.js CHANGED
@@ -1235,6 +1235,15 @@ function createMapNestedSchemaType(schema, schemaType, path, obj, options) {
1235
1235
  _mapType = { [schema.options.typeKey]: obj.of };
1236
1236
  }
1237
1237
 
1238
+ if (_mapType[schema.options.typeKey] && _mapType[schema.options.typeKey].instanceOfSchema) {
1239
+ const subdocumentSchema = _mapType[schema.options.typeKey];
1240
+ subdocumentSchema.eachPath((subpath, type) => {
1241
+ if (type.options.select === true || type.options.select === false) {
1242
+ throw new MongooseError('Cannot use schema-level projections (`select: true` or `select: false`) within maps at path "' + path + '.' + subpath + '"');
1243
+ }
1244
+ });
1245
+ }
1246
+
1238
1247
  if (utils.hasUserDefinedProperty(obj, 'ref')) {
1239
1248
  _mapType.ref = obj.ref;
1240
1249
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.3.3",
4
+ "version": "6.3.4",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -23,7 +23,7 @@
23
23
  "kareem": "2.3.5",
24
24
  "mongodb": "4.5.0",
25
25
  "mpath": "0.9.0",
26
- "mquery": "4.0.2",
26
+ "mquery": "4.0.3",
27
27
  "ms": "2.1.3",
28
28
  "sift": "16.0.0"
29
29
  },
@@ -83,8 +83,8 @@
83
83
  "docs:merge:legacy": "git merge 5.x",
84
84
  "docs:test": "npm run docs:generate && npm run docs:generate:search",
85
85
  "docs:view": "node website.js && node static.js",
86
- "docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
87
- "docs:prepare:publish:legacy": "npm run docs:checkout:legacy && docs:merge:legacy && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && docs:checkout:gh-pages && docs:copy:tmp:legacy",
86
+ "docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
87
+ "docs:prepare:publish:legacy": "npm run docs:checkout:legacy && npm run docs:merge:legacy && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && docs:checkout:gh-pages && docs:copy:tmp:legacy",
88
88
  "lint": "eslint .",
89
89
  "lint-js": "eslint . --ext .js",
90
90
  "lint-ts": "eslint . --ext .ts",
@@ -72,7 +72,7 @@ declare module 'mongoose' {
72
72
  * use this function to clean up any models you created in your tests to
73
73
  * prevent OverwriteModelErrors.
74
74
  */
75
- deleteModel(name: string): this;
75
+ deleteModel(name: string | RegExp): this;
76
76
 
77
77
  /**
78
78
  * Helper for `dropCollection()`. Will delete the given collection, including