mongoose 6.2.1 → 6.2.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.
Files changed (65) hide show
  1. package/.eslintrc.json +5 -1
  2. package/.lgtm.yml +3 -0
  3. package/CHANGELOG.md +54 -0
  4. package/dist/browser.umd.js +156 -152
  5. package/index.js +5 -1
  6. package/lib/aggregate.js +22 -27
  7. package/lib/browserDocument.js +1 -1
  8. package/lib/cast/number.js +2 -3
  9. package/lib/cast.js +7 -4
  10. package/lib/connection.js +43 -21
  11. package/lib/cursor/AggregationCursor.js +12 -7
  12. package/lib/cursor/QueryCursor.js +11 -6
  13. package/lib/document.js +58 -72
  14. package/lib/drivers/node-mongodb-native/collection.js +12 -4
  15. package/lib/drivers/node-mongodb-native/connection.js +11 -0
  16. package/lib/error/cast.js +3 -2
  17. package/lib/helpers/clone.js +11 -2
  18. package/lib/helpers/cursor/eachAsync.js +18 -15
  19. package/lib/helpers/document/cleanModifiedSubpaths.js +1 -0
  20. package/lib/helpers/document/compile.js +7 -4
  21. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
  22. package/lib/helpers/indexes/getRelatedIndexes.js +59 -0
  23. package/lib/helpers/isAsyncFunction.js +6 -7
  24. package/lib/helpers/populate/assignVals.js +4 -0
  25. package/lib/helpers/printJestWarning.js +2 -2
  26. package/lib/helpers/projection/applyProjection.js +77 -0
  27. package/lib/helpers/projection/hasIncludedChildren.js +36 -0
  28. package/lib/helpers/projection/isExclusive.js +5 -2
  29. package/lib/helpers/projection/isInclusive.js +5 -1
  30. package/lib/helpers/query/cast$expr.js +14 -19
  31. package/lib/helpers/query/hasDollarKeys.js +7 -3
  32. package/lib/helpers/query/isOperator.js +5 -2
  33. package/lib/helpers/schema/getIndexes.js +6 -2
  34. package/lib/index.js +14 -17
  35. package/lib/internal.js +9 -1
  36. package/lib/model.js +159 -153
  37. package/lib/options/SchemaTypeOptions.js +1 -1
  38. package/lib/plugins/trackTransaction.js +1 -1
  39. package/lib/query.js +159 -147
  40. package/lib/queryhelpers.js +8 -28
  41. package/lib/schema/SubdocumentPath.js +5 -4
  42. package/lib/schema/array.js +13 -6
  43. package/lib/schema/buffer.js +1 -1
  44. package/lib/schema/date.js +1 -1
  45. package/lib/schema/decimal128.js +1 -1
  46. package/lib/schema/documentarray.js +9 -7
  47. package/lib/schema/number.js +1 -1
  48. package/lib/schema/objectid.js +1 -1
  49. package/lib/schema/string.js +4 -4
  50. package/lib/schema.js +12 -8
  51. package/lib/schematype.js +12 -14
  52. package/lib/types/ArraySubdocument.js +1 -1
  53. package/lib/types/DocumentArray/index.js +1 -1
  54. package/lib/types/array/index.js +2 -2
  55. package/lib/types/array/methods/index.js +10 -11
  56. package/lib/types/buffer.js +3 -3
  57. package/lib/types/map.js +3 -4
  58. package/lib/utils.js +9 -3
  59. package/package.json +17 -21
  60. package/tsconfig.json +0 -2
  61. package/types/Connection.d.ts +212 -0
  62. package/types/Error.d.ts +129 -0
  63. package/types/PipelineStage.d.ts +272 -0
  64. package/types/index.d.ts +61 -602
  65. package/lib/types/array/ArrayWrapper.js +0 -981
@@ -26,9 +26,9 @@ exports.preparePopulationOptions = function preparePopulationOptions(query, opti
26
26
 
27
27
  // lean options should trickle through all queries
28
28
  if (options.lean != null) {
29
- pop.
30
- filter(p => get(p, 'options.lean') == null).
31
- forEach(makeLean(options.lean));
29
+ pop
30
+ .filter(p => (p && p.options && p.options.lean) == null)
31
+ .forEach(makeLean(options.lean));
32
32
  }
33
33
 
34
34
  pop.forEach(opts => {
@@ -53,12 +53,12 @@ exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query,
53
53
 
54
54
  // lean options should trickle through all queries
55
55
  if (options.lean != null) {
56
- pop.
57
- filter(p => get(p, 'options.lean') == null).
58
- forEach(makeLean(options.lean));
56
+ pop
57
+ .filter(p => (p && p.options && p.options.lean) == null)
58
+ .forEach(makeLean(options.lean));
59
59
  }
60
60
 
61
- const session = get(query, 'options.session', null);
61
+ const session = query && query.options && query.options.session || null;
62
62
  if (session != null) {
63
63
  pop.forEach(path => {
64
64
  if (path.options == null) {
@@ -272,7 +272,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
272
272
  // Special case: if user has included a parent path of a discriminator key,
273
273
  // don't explicitly project in the discriminator key because that will
274
274
  // project out everything else under the parent path
275
- if (!exclude && get(type, 'options.$skipDiscriminatorCheck', false)) {
275
+ if (!exclude && (type && type.options && type.options.$skipDiscriminatorCheck || false)) {
276
276
  let cur = '';
277
277
  for (let i = 0; i < pieces.length; ++i) {
278
278
  cur += (cur.length === 0 ? '' : '.') + pieces[i];
@@ -308,23 +308,3 @@ function makeLean(val) {
308
308
  option.options.lean = val;
309
309
  };
310
310
  }
311
-
312
- /*!
313
- * Handle the `WriteOpResult` from the server
314
- */
315
-
316
- exports.handleDeleteWriteOpResult = function handleDeleteWriteOpResult(callback) {
317
- return function _handleDeleteWriteOpResult(error, res) {
318
- if (error) {
319
- return callback(error);
320
- }
321
- const mongooseResult = Object.assign({}, res.result);
322
- if (get(res, 'result.n', null) != null) {
323
- mongooseResult.deletedCount = res.result.n;
324
- }
325
- if (res.deletedCount != null) {
326
- mongooseResult.deletedCount = res.deletedCount;
327
- }
328
- return callback(null, mongooseResult);
329
- };
330
- };
@@ -13,7 +13,6 @@ const $exists = require('./operators/exists');
13
13
  const castToNumber = require('./operators/helpers').castToNumber;
14
14
  const discriminator = require('../helpers/model/discriminator');
15
15
  const geospatial = require('./operators/geospatial');
16
- const get = require('../helpers/get');
17
16
  const getConstructor = require('../helpers/discriminator/getConstructor');
18
17
  const handleIdOption = require('../helpers/schema/handleIdOption');
19
18
  const internalToObjectOptions = require('../options').internalToObjectOptions;
@@ -164,7 +163,7 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
164
163
  let subdoc;
165
164
 
166
165
  // Only pull relevant selected paths and pull out the base path
167
- const parentSelected = get(doc, '$__.selected', {});
166
+ const parentSelected = doc && doc.$__ && doc.$__.selected || {};
168
167
  const path = this.path;
169
168
  const selected = Object.keys(parentSelected).reduce((obj, key) => {
170
169
  if (key.startsWith(path + '.')) {
@@ -240,7 +239,7 @@ SubdocumentPath.prototype.doValidate = function(value, fn, scope, options) {
240
239
  const Constructor = getConstructor(this.caster, value);
241
240
 
242
241
  if (value && !(value instanceof Constructor)) {
243
- value = new Constructor(value, null, scope);
242
+ value = new Constructor(value, null, (scope != null && scope.$__ != null) ? scope : null);
244
243
  }
245
244
 
246
245
  if (options && options.skipSchemaValidators) {
@@ -301,7 +300,9 @@ SubdocumentPath.prototype.doValidateSync = function(value, scope, options) {
301
300
  SubdocumentPath.prototype.discriminator = function(name, schema, options) {
302
301
  options = options || {};
303
302
  const value = utils.isPOJO(options) ? options.value : options;
304
- const clone = get(options, 'clone', true);
303
+ const clone = typeof options.clone === 'boolean'
304
+ ? options.clone
305
+ : true;
305
306
 
306
307
  if (schema.instanceOfSchema && clone) {
307
308
  schema = schema.clone();
@@ -13,7 +13,6 @@ const CastError = SchemaType.CastError;
13
13
  const Mixed = require('./mixed');
14
14
  const arrayDepth = require('../helpers/arrayDepth');
15
15
  const cast = require('../cast');
16
- const get = require('../helpers/get');
17
16
  const isOperator = require('../helpers/query/isOperator');
18
17
  const util = require('util');
19
18
  const utils = require('../utils');
@@ -218,7 +217,7 @@ SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
218
217
 
219
218
  // `require('util').inherits()` does **not** copy static properties, and
220
219
  // plugins like mongoose-float use `inherits()` for pre-ES6.
221
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
220
+ const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
222
221
  this.constructor.checkRequired() :
223
222
  SchemaArray.checkRequired();
224
223
 
@@ -236,7 +235,9 @@ SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
236
235
  SchemaArray.prototype.enum = function() {
237
236
  let arr = this;
238
237
  while (true) {
239
- const instance = get(arr, 'caster.instance');
238
+ const instance = arr &&
239
+ arr.caster &&
240
+ arr.caster.instance;
240
241
  if (instance === 'Array') {
241
242
  arr = arr.caster;
242
243
  continue;
@@ -583,9 +584,15 @@ function cast$elemMatch(val) {
583
584
 
584
585
  // Is this an embedded discriminator and is the discriminator key set?
585
586
  // If so, use the discriminator schema. See gh-7449
586
- const discriminatorKey = get(this,
587
- 'casterConstructor.schema.options.discriminatorKey');
588
- const discriminators = get(this, 'casterConstructor.schema.discriminators', {});
587
+ const discriminatorKey = this &&
588
+ this.casterConstructor &&
589
+ this.casterConstructor.schema &&
590
+ this.casterConstructor.schema.options &&
591
+ this.casterConstructor.schema.options.discriminatorKey;
592
+ const discriminators = this &&
593
+ this.casterConstructor &&
594
+ this.casterConstructor.schema &&
595
+ this.casterConstructor.schema.discriminators || {};
589
596
  if (discriminatorKey != null &&
590
597
  val[discriminatorKey] != null &&
591
598
  discriminators[val[discriminatorKey]] != null) {
@@ -202,7 +202,7 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
202
202
 
203
203
  /**
204
204
  * Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
205
- * for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html).
205
+ * for this buffer. You can find a [list of allowed subtypes here](https://api.mongodb.com/python/current/api/bson/binary.html).
206
206
  *
207
207
  * ####Example:
208
208
  *
@@ -201,7 +201,7 @@ SchemaDate.prototype.checkRequired = function(value, doc) {
201
201
 
202
202
  // `require('util').inherits()` does **not** copy static properties, and
203
203
  // plugins like mongoose-float use `inherits()` for pre-ES6.
204
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
204
+ const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
205
205
  this.constructor.checkRequired() :
206
206
  SchemaDate.checkRequired();
207
207
  return _checkRequired(value);
@@ -146,7 +146,7 @@ Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
146
146
 
147
147
  // `require('util').inherits()` does **not** copy static properties, and
148
148
  // plugins like mongoose-float use `inherits()` for pre-ES6.
149
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
149
+ const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
150
150
  this.constructor.checkRequired() :
151
151
  Decimal128.checkRequired();
152
152
 
@@ -11,7 +11,6 @@ const SchemaDocumentArrayOptions =
11
11
  require('../options/SchemaDocumentArrayOptions');
12
12
  const SchemaType = require('../schematype');
13
13
  const discriminator = require('../helpers/model/discriminator');
14
- const get = require('../helpers/get');
15
14
  const handleIdOption = require('../helpers/schema/handleIdOption');
16
15
  const util = require('util');
17
16
  const utils = require('../utils');
@@ -68,7 +67,9 @@ function DocumentArrayPath(key, schema, options, schemaOptions) {
68
67
 
69
68
  const parentSchemaType = this;
70
69
  this.$embeddedSchemaType = new SchemaType(key + '.$', {
71
- required: get(this, 'schemaOptions.required', false)
70
+ required: this &&
71
+ this.schemaOptions &&
72
+ this.schemaOptions.required || false
72
73
  });
73
74
  this.$embeddedSchemaType.cast = function(value, doc, init) {
74
75
  return parentSchemaType.cast(value, doc, init)[0];
@@ -173,7 +174,7 @@ DocumentArrayPath.prototype.discriminator = function(name, schema, options) {
173
174
 
174
175
  options = options || {};
175
176
  const tiedValue = utils.isPOJO(options) ? options.value : options;
176
- const clone = get(options, 'clone', true);
177
+ const clone = typeof options.clone === 'boolean' ? options.clone : true;
177
178
 
178
179
  if (schema.instanceOfSchema && clone) {
179
180
  schema = schema.clone();
@@ -364,6 +365,9 @@ DocumentArrayPath.prototype.getDefault = function(scope) {
364
365
  return ret;
365
366
  };
366
367
 
368
+ const _toObjectOptions = Object.freeze({ transform: false, virtuals: false });
369
+ const initDocumentOptions = Object.freeze({ skipId: true, willInit: true });
370
+
367
371
  /**
368
372
  * Casts contents
369
373
  *
@@ -383,7 +387,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
383
387
 
384
388
  let selected;
385
389
  let subdoc;
386
- const _opts = { transform: false, virtuals: false };
390
+
387
391
  options = options || {};
388
392
 
389
393
  if (!Array.isArray(value)) {
@@ -413,9 +417,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
413
417
  }
414
418
 
415
419
  const rawArray = utils.isMongooseDocumentArray(value) ? value.__array : value;
416
-
417
420
  const len = rawArray.length;
418
- const initDocumentOptions = { skipId: true, willInit: true };
419
421
 
420
422
  for (let i = 0; i < len; ++i) {
421
423
  if (!rawArray[i]) {
@@ -463,7 +465,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
463
465
  subdoc = prev.id(rawArray[i]._id);
464
466
  }
465
467
 
466
- if (prev && subdoc && utils.deepEqual(subdoc.toObject(_opts), rawArray[i])) {
468
+ if (prev && subdoc && utils.deepEqual(subdoc.toObject(_toObjectOptions), rawArray[i])) {
467
469
  // handle resetting doc with existing id and same data
468
470
  subdoc.set(rawArray[i]);
469
471
  // if set() is hooked it will have no return value
@@ -170,7 +170,7 @@ SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
170
170
 
171
171
  // `require('util').inherits()` does **not** copy static properties, and
172
172
  // plugins like mongoose-float use `inherits()` for pre-ES6.
173
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
173
+ const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
174
174
  this.constructor.checkRequired() :
175
175
  SchemaNumber.checkRequired();
176
176
 
@@ -204,7 +204,7 @@ ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
204
204
 
205
205
  // `require('util').inherits()` does **not** copy static properties, and
206
206
  // plugins like mongoose-float use `inherits()` for pre-ES6.
207
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
207
+ const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
208
208
  this.constructor.checkRequired() :
209
209
  ObjectId.checkRequired();
210
210
 
@@ -253,7 +253,7 @@ SchemaString.prototype.enum = function() {
253
253
  };
254
254
 
255
255
  /**
256
- * Adds a lowercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
256
+ * Adds a lowercase [setter](https://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
257
257
  *
258
258
  * ####Example:
259
259
  *
@@ -291,7 +291,7 @@ SchemaString.prototype.lowercase = function(shouldApply) {
291
291
  };
292
292
 
293
293
  /**
294
- * Adds an uppercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
294
+ * Adds an uppercase [setter](https://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
295
295
  *
296
296
  * ####Example:
297
297
  *
@@ -327,7 +327,7 @@ SchemaString.prototype.uppercase = function(shouldApply) {
327
327
  };
328
328
 
329
329
  /**
330
- * Adds a trim [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
330
+ * Adds a trim [setter](https://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
331
331
  *
332
332
  * The string value will be [trimmed](https://masteringjs.io/tutorials/fundamentals/trim-string) when set.
333
333
  *
@@ -566,7 +566,7 @@ SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
566
566
 
567
567
  // `require('util').inherits()` does **not** copy static properties, and
568
568
  // plugins like mongoose-float use `inherits()` for pre-ES6.
569
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
569
+ const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
570
570
  this.constructor.checkRequired() :
571
571
  SchemaString.checkRequired();
572
572
 
package/lib/schema.js CHANGED
@@ -86,7 +86,7 @@ let id = 0;
86
86
  *
87
87
  * @param {Object|Schema|Array} [definition] Can be one of: object describing schema paths, or schema to copy, or array of objects and schemas
88
88
  * @param {Object} [options]
89
- * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
89
+ * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
90
90
  * @event `init`: Emitted after the schema is compiled into a `Model`.
91
91
  * @api public
92
92
  */
@@ -425,7 +425,7 @@ Schema.prototype.pick = function(paths, options) {
425
425
 
426
426
  Schema.prototype.defaultOptions = function(options) {
427
427
  this._userProvidedOptions = options == null ? {} : utils.clone(options);
428
- const baseOptions = get(this, 'base.options', {});
428
+ const baseOptions = this.base && this.base.options || {};
429
429
 
430
430
  const strict = 'strict' in baseOptions ? baseOptions.strict : true;
431
431
  options = utils.options({
@@ -519,7 +519,7 @@ Schema.prototype.add = function add(obj, prefix) {
519
519
  if (key === '_id' && val === false) {
520
520
  continue;
521
521
  }
522
- if (val instanceof VirtualType || get(val, 'constructor.name', null) === 'VirtualType') {
522
+ if (val instanceof VirtualType || (val.constructor && val.constructor.name || null) === 'VirtualType') {
523
523
  this.virtual(val);
524
524
  continue;
525
525
  }
@@ -1019,6 +1019,10 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1019
1019
  ? cast[options.typeKey]
1020
1020
  : cast;
1021
1021
 
1022
+ if (Array.isArray(type)) {
1023
+ return new MongooseTypes.Array(path, this.interpretAsType(path, type, options), obj);
1024
+ }
1025
+
1022
1026
  name = typeof type === 'string'
1023
1027
  ? type
1024
1028
  : type.schemaName || utils.getFunctionName(type);
@@ -1036,7 +1040,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1036
1040
  if (!MongooseTypes.hasOwnProperty(name)) {
1037
1041
  throw new TypeError('Invalid schema configuration: ' +
1038
1042
  `\`${name}\` is not a valid type within the array \`${path}\`.` +
1039
- 'See http://bit.ly/mongoose-schematypes for a list of valid schema types.');
1043
+ 'See https://bit.ly/mongoose-schematypes for a list of valid schema types.');
1040
1044
  }
1041
1045
  }
1042
1046
 
@@ -1076,7 +1080,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
1076
1080
  if (MongooseTypes[name] == null) {
1077
1081
  throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
1078
1082
  `a valid type at path \`${path}\`. See ` +
1079
- 'http://bit.ly/mongoose-schematypes for a list of valid schema types.');
1083
+ 'https://bit.ly/mongoose-schematypes for a list of valid schema types.');
1080
1084
  }
1081
1085
 
1082
1086
  const schemaType = new MongooseTypes[name](path, obj);
@@ -1471,8 +1475,8 @@ Schema.prototype.pre = function(name) {
1471
1475
  * @param {Boolean} [options.document] If `name` is a hook for both document and query middleware, set to `true` to run on document middleware.
1472
1476
  * @param {Boolean} [options.query] If `name` is a hook for both document and query middleware, set to `true` to run on query middleware.
1473
1477
  * @param {Function} fn callback
1474
- * @see middleware http://mongoosejs.com/docs/middleware.html
1475
- * @see kareem http://npmjs.org/package/kareem
1478
+ * @see middleware https://mongoosejs.com/docs/middleware.html
1479
+ * @see kareem https://npmjs.org/package/kareem
1476
1480
  * @api public
1477
1481
  */
1478
1482
 
@@ -1619,7 +1623,7 @@ Schema.prototype.static = function(name, fn) {
1619
1623
  * schema.index({ first: 1, last: -1 })
1620
1624
  *
1621
1625
  * @param {Object} fields
1622
- * @param {Object} [options] Options to pass to [MongoDB driver's `createIndex()` function](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#createIndex)
1626
+ * @param {Object} [options] Options to pass to [MongoDB driver's `createIndex()` function](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#createIndex)
1623
1627
  * @param {String | number} [options.expires=null] Mongoose-specific syntactic sugar, uses [ms](https://www.npmjs.com/package/ms) to convert `expires` option into seconds for the `expireAfterSeconds` in the above link.
1624
1628
  * @api public
1625
1629
  */
package/lib/schematype.js CHANGED
@@ -8,7 +8,6 @@ const MongooseError = require('./error/index');
8
8
  const SchemaTypeOptions = require('./options/SchemaTypeOptions');
9
9
  const $exists = require('./schema/operators/exists');
10
10
  const $type = require('./schema/operators/type');
11
- const get = require('./helpers/get');
12
11
  const handleImmutable = require('./helpers/schematype/handleImmutable');
13
12
  const isAsyncFunction = require('./helpers/isAsyncFunction');
14
13
  const immediate = require('./helpers/immediate');
@@ -788,7 +787,7 @@ SchemaType.prototype.get = function(fn) {
788
787
  *
789
788
  * // make sure every value is equal to "something"
790
789
  * function validator (val) {
791
- * return val == 'something';
790
+ * return val === 'something';
792
791
  * }
793
792
  * new Schema({ name: { type: String, validate: validator }});
794
793
  *
@@ -916,7 +915,7 @@ SchemaType.prototype.validate = function(obj, message, type) {
916
915
  if (!utils.isPOJO(arg)) {
917
916
  const msg = 'Invalid validator. Received (' + typeof arg + ') '
918
917
  + arg
919
- + '. See http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
918
+ + '. See https://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
920
919
 
921
920
  throw new Error(msg);
922
921
  }
@@ -1026,7 +1025,7 @@ SchemaType.prototype.required = function(required, message) {
1026
1025
  this.isRequired = true;
1027
1026
 
1028
1027
  this.requiredValidator = function(v) {
1029
- const cachedRequired = get(this, '$__.cachedRequired');
1028
+ const cachedRequired = this && this.$__ && this.$__.cachedRequired;
1030
1029
 
1031
1030
  // no validation when this path wasn't selected in the query.
1032
1031
  if (cachedRequired != null && !this.$__isSelected(_this.path) && !this[documentIsModified](_this.path)) {
@@ -1251,10 +1250,10 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
1251
1250
  return fn(null);
1252
1251
  }
1253
1252
 
1254
- const _this = this;
1255
- validators.forEach(function(v) {
1253
+ for (let i = 0, len = validators.length; i < len; ++i) {
1254
+ const v = validators[i];
1256
1255
  if (err) {
1257
- return;
1256
+ break;
1258
1257
  }
1259
1258
 
1260
1259
  const validator = v.validator;
@@ -1266,16 +1265,16 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
1266
1265
 
1267
1266
  if (validator instanceof RegExp) {
1268
1267
  validate(validator.test(value), validatorProperties);
1269
- return;
1268
+ continue;
1270
1269
  }
1271
1270
 
1272
1271
  if (typeof validator !== 'function') {
1273
- return;
1272
+ continue;
1274
1273
  }
1275
1274
 
1276
- if (value === undefined && validator !== _this.requiredValidator) {
1275
+ if (value === undefined && validator !== this.requiredValidator) {
1277
1276
  validate(true, validatorProperties);
1278
- return;
1277
+ continue;
1279
1278
  }
1280
1279
 
1281
1280
  try {
@@ -1304,8 +1303,7 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
1304
1303
  } else {
1305
1304
  validate(ok, validatorProperties);
1306
1305
  }
1307
-
1308
- });
1306
+ }
1309
1307
 
1310
1308
  function validate(ok, validatorProperties) {
1311
1309
  if (err) {
@@ -1477,7 +1475,7 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init) {
1477
1475
  }
1478
1476
 
1479
1477
  if (value.$__ != null) {
1480
- value.$__.wasPopulated = true;
1478
+ value.$__.wasPopulated = value.$__.wasPopulated || true;
1481
1479
  return value;
1482
1480
  }
1483
1481
 
@@ -31,7 +31,7 @@ function ArraySubdocument(obj, parentArr, skipId, fields, index) {
31
31
  this.$setIndex(index);
32
32
  this.$__parent = this[documentArrayParent];
33
33
 
34
- Subdocument.call(this, obj, fields, this[documentArrayParent], void 0, { isNew: true });
34
+ Subdocument.call(this, obj, fields, this[documentArrayParent], skipId, { isNew: true });
35
35
  }
36
36
 
37
37
  /*!
@@ -25,7 +25,7 @@ const numberRE = /^\d+$/;
25
25
  * @api private
26
26
  * @return {MongooseDocumentArray}
27
27
  * @inherits MongooseArray
28
- * @see http://bit.ly/f6CnZU
28
+ * @see https://bit.ly/f6CnZU
29
29
  */
30
30
 
31
31
  function MongooseDocumentArray(values, path, doc) {
@@ -25,7 +25,7 @@ const arraySchemaSymbol = require('../../helpers/symbols').arraySchemaSymbol;
25
25
  * @param {Document} doc parent document
26
26
  * @api private
27
27
  * @inherits Array
28
- * @see http://bit.ly/f6CnZU
28
+ * @see https://bit.ly/f6CnZU
29
29
  */
30
30
  const _basePush = Array.prototype.push;
31
31
  const numberRE = /^\d+$/;
@@ -51,7 +51,7 @@ function MongooseArray(values, path, doc, schematype) {
51
51
  } else {
52
52
  __array = new Array();
53
53
  for (let i = 0; i < len; ++i) {
54
- _basePush.apply(__array, values[i]);
54
+ _basePush.call(__array, values[i]);
55
55
  }
56
56
  }
57
57
  } else {
@@ -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](http://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop) method which enforces this restriction._
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 http://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop
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](http://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop) method which enforces this restriction._
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 http://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop
178
+ * @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pop
180
179
  * @method $pop
181
180
  * @memberOf MongooseArray
182
181
  */
@@ -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.](./api.html#document_Document-equals)
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 http://www.mongodb.org/display/DOCS/Updating/#Updating-%24pull
562
+ * @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pull
564
563
  * @api public
565
564
  * @method pull
566
565
  * @memberOf MongooseArray
@@ -660,7 +659,7 @@ const methods = {
660
659
  if (isOverwrite) {
661
660
  atomic.$each = values;
662
661
 
663
- if (get(atomics, '$push.$each.length', 0) > 0 &&
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 (get(atomics, '$push.$each.length', 0) > 0 &&
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 http://www.mongodb.org/display/DOCS/Updating/#Updating-%24pull
693
+ * @see mongodb https://www.mongodb.org/display/DOCS/Updating/#Updating-%24pull
695
694
  * @api public
696
695
  * @memberOf MongooseArray
697
696
  * @instance
@@ -926,7 +925,7 @@ function _isAllSubdocs(docs, ref) {
926
925
  function _checkManualPopulation(arr, docs) {
927
926
  const ref = arr == null ?
928
927
  null :
929
- get(arr[arraySchemaSymbol], 'caster.options.ref', null);
928
+ arr[arraySchemaSymbol] && arr[arraySchemaSymbol].caster && arr[arraySchemaSymbol].caster.options && arr[arraySchemaSymbol].caster.options.ref || null;
930
929
  if (arr.length === 0 &&
931
930
  docs.length !== 0) {
932
931
  if (_isAllSubdocs(docs, ref)) {
@@ -17,7 +17,7 @@ const utils = require('../utils');
17
17
  * @param {Number} offset
18
18
  * @api private
19
19
  * @inherits Buffer
20
- * @see http://bit.ly/f6CnZU
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 http://bsonspec.org/#/specification
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 http://bsonspec.org/#/specification
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');
@@ -83,7 +82,7 @@ class MongooseMap extends Map {
83
82
  if (value.$__ == null) {
84
83
  value = new populated.options[populateModelSymbol](value);
85
84
  }
86
- value.$__.wasPopulated = true;
85
+ value.$__.wasPopulated = { value: populated.value };
87
86
  } else {
88
87
  try {
89
88
  value = this.$__schemaType.
@@ -131,7 +130,7 @@ class MongooseMap extends Map {
131
130
  }
132
131
 
133
132
  toObject(options) {
134
- if (get(options, 'flattenMaps')) {
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 (get(options, 'flattenMaps', true)) {
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) {