mongoose 6.5.3 → 6.6.0

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 (93) hide show
  1. package/.mocharc.yml +2 -0
  2. package/index.js +51 -1
  3. package/lib/aggregate.js +33 -9
  4. package/lib/cast/boolean.js +1 -1
  5. package/lib/cast/number.js +1 -1
  6. package/lib/cast/string.js +1 -1
  7. package/lib/cast.js +3 -0
  8. package/lib/connection.js +19 -11
  9. package/lib/cursor/AggregationCursor.js +17 -6
  10. package/lib/cursor/QueryCursor.js +16 -5
  11. package/lib/document.js +29 -16
  12. package/lib/drivers/browser/objectid.js +2 -1
  13. package/lib/drivers/node-mongodb-native/ReadPreference.js +1 -1
  14. package/lib/drivers/node-mongodb-native/collection.js +7 -3
  15. package/lib/drivers/node-mongodb-native/connection.js +1 -1
  16. package/lib/error/browserMissingSchema.js +1 -1
  17. package/lib/error/cast.js +2 -1
  18. package/lib/error/divergentArray.js +2 -1
  19. package/lib/error/missingSchema.js +2 -1
  20. package/lib/error/notFound.js +2 -1
  21. package/lib/error/overwriteModel.js +2 -1
  22. package/lib/error/validation.js +9 -7
  23. package/lib/error/validator.js +8 -4
  24. package/lib/helpers/clone.js +1 -1
  25. package/lib/helpers/cursor/eachAsync.js +17 -1
  26. package/lib/helpers/discriminator/getConstructor.js +2 -1
  27. package/lib/helpers/discriminator/getDiscriminatorByValue.js +7 -6
  28. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +7 -6
  29. package/lib/helpers/document/compile.js +14 -2
  30. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +5 -1
  31. package/lib/helpers/get.js +2 -1
  32. package/lib/helpers/getConstructorName.js +2 -1
  33. package/lib/helpers/indexes/isIndexEqual.js +6 -6
  34. package/lib/helpers/isBsonType.js +2 -1
  35. package/lib/helpers/isMongooseObject.js +2 -2
  36. package/lib/helpers/isObject.js +1 -1
  37. package/lib/helpers/isSimpleValidator.js +1 -1
  38. package/lib/helpers/model/applyHooks.js +11 -0
  39. package/lib/helpers/model/applyMethods.js +11 -1
  40. package/lib/helpers/model/applyStatics.js +2 -1
  41. package/lib/helpers/model/castBulkWrite.js +7 -2
  42. package/lib/helpers/model/discriminator.js +3 -1
  43. package/lib/helpers/pluralize.js +1 -1
  44. package/lib/helpers/populate/assignRawDocsToIdStructure.js +5 -3
  45. package/lib/helpers/populate/assignVals.js +14 -3
  46. package/lib/helpers/populate/createPopulateQueryFilter.js +8 -2
  47. package/lib/helpers/populate/getModelsMapForPopulate.js +3 -1
  48. package/lib/helpers/populate/getSchemaTypes.js +3 -1
  49. package/lib/helpers/populate/markArraySubdocsPopulated.js +5 -1
  50. package/lib/helpers/projection/hasIncludedChildren.js +4 -1
  51. package/lib/helpers/projection/isPathExcluded.js +3 -2
  52. package/lib/helpers/projection/isSubpath.js +2 -1
  53. package/lib/helpers/query/applyQueryMiddleware.js +3 -2
  54. package/lib/helpers/query/castUpdate.js +14 -8
  55. package/lib/helpers/query/completeMany.js +3 -2
  56. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +7 -1
  57. package/lib/helpers/query/wrapThunk.js +3 -1
  58. package/lib/helpers/schema/getIndexes.js +7 -2
  59. package/lib/helpers/schema/getPath.js +4 -3
  60. package/lib/helpers/schema/idGetter.js +2 -1
  61. package/lib/helpers/setDefaultsOnInsert.js +15 -0
  62. package/lib/index.js +19 -10
  63. package/lib/model.js +217 -138
  64. package/lib/query.js +160 -65
  65. package/lib/queryhelpers.js +4 -4
  66. package/lib/schema/SubdocumentPath.js +1 -1
  67. package/lib/schema/array.js +2 -2
  68. package/lib/schema/date.js +2 -1
  69. package/lib/schema/documentarray.js +3 -2
  70. package/lib/schema/operators/helpers.js +2 -2
  71. package/lib/schema/operators/text.js +1 -1
  72. package/lib/schema/string.js +1 -1
  73. package/lib/schema.js +12 -4
  74. package/lib/schematype.js +17 -4
  75. package/lib/statemachine.js +13 -12
  76. package/lib/types/ArraySubdocument.js +13 -3
  77. package/lib/types/DocumentArray/methods/index.js +9 -5
  78. package/lib/types/array/methods/index.js +5 -4
  79. package/lib/types/map.js +3 -1
  80. package/lib/types/objectid.js +2 -2
  81. package/lib/types/subdocument.js +30 -8
  82. package/lib/utils.js +58 -31
  83. package/package.json +11 -11
  84. package/scripts/create-tarball.js +7 -0
  85. package/types/aggregate.d.ts +9 -4
  86. package/types/connection.d.ts +2 -2
  87. package/types/cursor.d.ts +1 -1
  88. package/types/document.d.ts +1 -1
  89. package/types/indexes.d.ts +2 -2
  90. package/types/middlewares.d.ts +1 -1
  91. package/types/models.d.ts +2 -2
  92. package/types/pipelinestage.d.ts +13 -0
  93. package/types/query.d.ts +2 -2
package/.mocharc.yml ADDED
@@ -0,0 +1,2 @@
1
+ reporter: spec # better to identify failing / slow tests than "dot"
2
+ ui: bdd # explicitly setting, even though it is mocha default
package/index.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * Export lib/mongoose
4
3
  *
@@ -11,3 +10,54 @@ const mongoose = require('./lib/');
11
10
  module.exports = mongoose;
12
11
  module.exports.default = mongoose;
13
12
  module.exports.mongoose = mongoose;
13
+
14
+ // Re-export for ESM support
15
+ module.exports.cast = mongoose.cast;
16
+ module.exports.STATES = mongoose.STATES;
17
+ module.exports.setDriver = mongoose.setDriver;
18
+ module.exports.set = mongoose.set;
19
+ module.exports.get = mongoose.get;
20
+ module.exports.createConnection = mongoose.createConnection;
21
+ module.exports.connect = mongoose.connect;
22
+ module.exports.disconnect = mongoose.disconnect;
23
+ module.exports.startSession = mongoose.startSession;
24
+ module.exports.pluralize = mongoose.pluralize;
25
+ module.exports.model = mongoose.model;
26
+ module.exports.deleteModel = mongoose.deleteModel;
27
+ module.exports.modelNames = mongoose.modelNames;
28
+ module.exports.plugin = mongoose.plugin;
29
+ module.exports.connections = mongoose.connections;
30
+ module.exports.version = mongoose.version;
31
+ module.exports.Mongoose = mongoose.Mongoose;
32
+ module.exports.Schema = mongoose.Schema;
33
+ module.exports.SchemaType = mongoose.SchemaType;
34
+ module.exports.SchemaTypes = mongoose.SchemaTypes;
35
+ module.exports.VirtualType = mongoose.VirtualType;
36
+ module.exports.Types = mongoose.Types;
37
+ module.exports.Query = mongoose.Query;
38
+ module.exports.Promise = mongoose.Promise;
39
+ module.exports.Model = mongoose.Model;
40
+ module.exports.Document = mongoose.Document;
41
+ module.exports.ObjectId = mongoose.ObjectId;
42
+ module.exports.isValidObjectId = mongoose.isValidObjectId;
43
+ module.exports.isObjectIdOrHexString = mongoose.isObjectIdOrHexString;
44
+ module.exports.syncIndexes = mongoose.syncIndexes;
45
+ module.exports.Decimal128 = mongoose.Decimal128;
46
+ module.exports.Mixed = mongoose.Mixed;
47
+ module.exports.Date = mongoose.Date;
48
+ module.exports.Number = mongoose.Number;
49
+ module.exports.Error = mongoose.Error;
50
+ module.exports.now = mongoose.now;
51
+ module.exports.CastError = mongoose.CastError;
52
+ module.exports.SchemaTypeOptions = mongoose.SchemaTypeOptions;
53
+ module.exports.mongo = mongoose.mongo;
54
+ module.exports.mquery = mongoose.mquery;
55
+ module.exports.sanitizeFilter = mongoose.sanitizeFilter;
56
+ module.exports.trusted = mongoose.trusted;
57
+ module.exports.skipMiddlewareFunction = mongoose.skipMiddlewareFunction;
58
+ module.exports.overwriteMiddlewareResult = mongoose.overwriteMiddlewareResult;
59
+
60
+ // The following properties are not exported using ESM because `setDriver()` can mutate these
61
+ // module.exports.connection = mongoose.connection;
62
+ // module.exports.Collection = mongoose.Collection;
63
+ // module.exports.Connection = mongoose.Connection;
package/lib/aggregate.js CHANGED
@@ -43,7 +43,7 @@ const validRedactStringValues = new Set(['$$DESCEND', '$$PRUNE', '$$KEEP']);
43
43
  * new Aggregate([{ $match: { _id: new mongoose.Types.ObjectId('00000000000000000000000a') } }]);
44
44
  *
45
45
  * @see MongoDB https://docs.mongodb.org/manual/applications/aggregation/
46
- * @see driver https://mongodb.github.com/node-mongodb-native/api-generated/collection.html#aggregate
46
+ * @see driver https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#aggregate
47
47
  * @param {Array} [pipeline] aggregation pipeline as an array of objects
48
48
  * @param {Model} [model] the model to use with this aggregate.
49
49
  * @api public
@@ -304,6 +304,30 @@ Aggregate.prototype.project = function(arg) {
304
304
  * @api public
305
305
  */
306
306
 
307
+
308
+ /**
309
+ * Appends a new $densify operator to this aggregate pipeline.
310
+ *
311
+ * #### Examples:
312
+ *
313
+ * aggregate.densify({
314
+ * field: 'timestamp',
315
+ * range: {
316
+ * step: 1,
317
+ * unit: 'hour',
318
+ * bounds: [new Date('2021-05-18T00:00:00.000Z'), new Date('2021-05-18T08:00:00.000Z')]
319
+ * }
320
+ * });
321
+ *
322
+ * @see $densify https://www.mongodb.com/docs/manual/reference/operator/aggregation/densify/
323
+ * @method densify
324
+ * @memberOf Aggregate
325
+ * @instance
326
+ * @param {Object} arg $densify operator contents
327
+ * @return {Aggregate}
328
+ * @api public
329
+ */
330
+
307
331
  /**
308
332
  * Appends a new $geoNear operator to this aggregate pipeline.
309
333
  *
@@ -342,7 +366,7 @@ Aggregate.prototype.near = function(arg) {
342
366
  * define methods
343
367
  */
344
368
 
345
- 'group match skip limit out'.split(' ').forEach(function($operator) {
369
+ 'group match skip limit out densify'.split(' ').forEach(function($operator) {
346
370
  Aggregate.prototype[$operator] = function(arg) {
347
371
  const op = {};
348
372
  op['$' + $operator] = arg;
@@ -623,7 +647,6 @@ Aggregate.prototype.unionWith = function(options) {
623
647
  * @return {Aggregate} this
624
648
  * @api public
625
649
  * @see mongodb https://docs.mongodb.org/manual/applications/replication/#read-preference
626
- * @see driver https://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
627
650
  */
628
651
 
629
652
  Aggregate.prototype.read = function(pref, tags) {
@@ -766,7 +789,7 @@ Aggregate.prototype.explain = function(verbosity, callback) {
766
789
  };
767
790
 
768
791
  /**
769
- * Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0)
792
+ * Sets the allowDiskUse option for the aggregation query
770
793
  *
771
794
  * #### Example:
772
795
  *
@@ -783,7 +806,7 @@ Aggregate.prototype.allowDiskUse = function(value) {
783
806
  };
784
807
 
785
808
  /**
786
- * Sets the hint option for the aggregation query (ignored for < 3.6.0)
809
+ * Sets the hint option for the aggregation query
787
810
  *
788
811
  * #### Example:
789
812
  *
@@ -863,7 +886,7 @@ Aggregate.prototype.option = function(value) {
863
886
  * @param {Boolean} [options.useMongooseAggCursor] use experimental mongoose-specific aggregation cursor (for `eachAsync()` and other query cursor semantics)
864
887
  * @return {AggregationCursor} cursor representing this aggregation
865
888
  * @api public
866
- * @see mongodb https://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html
889
+ * @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html
867
890
  */
868
891
 
869
892
  Aggregate.prototype.cursor = function(options) {
@@ -881,7 +904,7 @@ Aggregate.prototype.cursor = function(options) {
881
904
  * @param {Object} collation options
882
905
  * @return {Aggregate} this
883
906
  * @api public
884
- * @see mongodb https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#aggregate
907
+ * @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/interfaces/CollationOptions.html
885
908
  */
886
909
 
887
910
  Aggregate.prototype.collation = function(collation) {
@@ -1050,7 +1073,7 @@ Aggregate.prototype.catch = function(reject) {
1050
1073
  };
1051
1074
 
1052
1075
  /**
1053
- * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js
1076
+ * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js)
1054
1077
  * You do not need to call this function explicitly, the JavaScript runtime
1055
1078
  * will call it for you.
1056
1079
  *
@@ -1104,12 +1127,13 @@ function isOperator(obj) {
1104
1127
  return k.length === 1 && k[0][0] === '$';
1105
1128
  }
1106
1129
 
1107
- /*!
1130
+ /**
1108
1131
  * Adds the appropriate `$match` pipeline step to the top of an aggregate's
1109
1132
  * pipeline, should it's model is a non-root discriminator type. This is
1110
1133
  * analogous to the `prepareDiscriminatorCriteria` function in `lib/query.js`.
1111
1134
  *
1112
1135
  * @param {Aggregate} aggregate Aggregate to prepare
1136
+ * @api private
1113
1137
  */
1114
1138
 
1115
1139
  Aggregate._prepareDiscriminatorPipeline = prepareDiscriminatorPipeline;
@@ -2,7 +2,7 @@
2
2
 
3
3
  const CastError = require('../error/cast');
4
4
 
5
- /*!
5
+ /**
6
6
  * Given a value, cast it to a boolean, or throw a `CastError` if the value
7
7
  * cannot be casted. `null` and `undefined` are considered valid.
8
8
  *
@@ -2,7 +2,7 @@
2
2
 
3
3
  const assert = require('assert');
4
4
 
5
- /*!
5
+ /**
6
6
  * Given a value, cast it to a number, or throw an `Error` if the value
7
7
  * cannot be casted. `null` and `undefined` are considered valid.
8
8
  *
@@ -2,7 +2,7 @@
2
2
 
3
3
  const CastError = require('../error/cast');
4
4
 
5
- /*!
5
+ /**
6
6
  * Given a value, cast it to a string, or throw a `CastError` if the value
7
7
  * cannot be casted. `null` and `undefined` are considered valid.
8
8
  *
package/lib/cast.js CHANGED
@@ -25,6 +25,9 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
25
25
  * @param {Schema} schema
26
26
  * @param {Object} obj Object to cast
27
27
  * @param {Object} [options] the query options
28
+ * @param {Boolean|"throw"} [options.strict] Wheter to enable all strict options
29
+ * @param {Boolean|"throw"} [options.strictQuery] Enable strict Queries
30
+ * @param {Boolean} [options.upsert]
28
31
  * @param {Query} [context] passed to setters
29
32
  * @api private
30
33
  */
package/lib/connection.js CHANGED
@@ -26,7 +26,7 @@ const processConnectionOptions = require('./helpers/processConnectionOptions');
26
26
  const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
27
27
  const sessionNewDocuments = require('./helpers/symbols').sessionNewDocuments;
28
28
 
29
- /*!
29
+ /**
30
30
  * A list of authentication mechanisms that don't require a password for authentication.
31
31
  * This is used by the authMechanismDoesNotRequirePassword method.
32
32
  *
@@ -81,7 +81,7 @@ function Connection(base) {
81
81
  * Inherit from EventEmitter
82
82
  */
83
83
 
84
- Connection.prototype.__proto__ = EventEmitter.prototype;
84
+ Object.setPrototypeOf(Connection.prototype, EventEmitter.prototype);
85
85
 
86
86
  /**
87
87
  * Connection ready state
@@ -387,11 +387,11 @@ Connection.prototype.config;
387
387
  * with specified options. Used to create [capped collections](https://docs.mongodb.com/manual/core/capped-collections/)
388
388
  * and [views](https://docs.mongodb.com/manual/core/views/) from mongoose.
389
389
  *
390
- * Options are passed down without modification to the [MongoDB driver's `createCollection()` function](https://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
390
+ * Options are passed down without modification to the [MongoDB driver's `createCollection()` function](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#createCollection)
391
391
  *
392
392
  * @method createCollection
393
393
  * @param {string} collection The collection to create
394
- * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
394
+ * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#createCollection)
395
395
  * @param {Function} [callback]
396
396
  * @return {Promise} Returns a Promise if no `callback` is given.
397
397
  * @api public
@@ -423,7 +423,7 @@ Connection.prototype.createCollection = _wrapConnHelper(function createCollectio
423
423
  *
424
424
  *
425
425
  * @method startSession
426
- * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
426
+ * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#startSession)
427
427
  * @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
428
428
  * @param {Function} [callback]
429
429
  * @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
@@ -445,7 +445,7 @@ Connection.prototype.startSession = _wrapConnHelper(function startSession(option
445
445
  * async function executes successfully and attempt to retry if
446
446
  * there was a retriable error.
447
447
  *
448
- * Calls the MongoDB driver's [`session.withTransaction()`](https://mongodb.github.io/node-mongodb-native/3.5/api/ClientSession.html#withTransaction),
448
+ * Calls the MongoDB driver's [`session.withTransaction()`](https://mongodb.github.io/node-mongodb-native/4.9/classes/ClientSession.html#withTransaction),
449
449
  * but also handles resetting Mongoose document state as shown below.
450
450
  *
451
451
  * #### Example:
@@ -656,7 +656,7 @@ Connection.prototype.onOpen = function() {
656
656
  * Opens the connection with a URI using `MongoClient.connect()`.
657
657
  *
658
658
  * @param {String} uri The URI to connect with.
659
- * @param {Object} [options] Passed on to https://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
659
+ * @param {Object} [options] Passed on to [`MongoClient.connect`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#connect-1)
660
660
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
661
661
  * @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
662
662
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
@@ -667,7 +667,7 @@ Connection.prototype.onOpen = function() {
667
667
  * @param {Number} [options.serverSelectionTimeoutMS] If `useUnifiedTopology = true`, the MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds before erroring out. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
668
668
  * @param {Number} [options.heartbeatFrequencyMS] If `useUnifiedTopology = true`, the MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
669
669
  * @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
670
- * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
670
+ * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html#promiseLibrary).
671
671
  * @param {Number} [options.connectTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _during initial connection_. Defaults to 30000. This option is passed transparently to [Node.js' `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback).
672
672
  * @param {Number} [options.socketTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. A socket may be inactive because of either no activity or a long-running operation. This is set to `30000` by default, you should set this to 2-3x your longest running operation if you expect some of your database operations to run longer than 20 seconds. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
673
673
  * @param {Number} [options.family=0] Passed transparently to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. May be either `0, `4`, or `6`. `4` means use IPv4 only, `6` means use IPv6 only, `0` means try both.
@@ -1230,6 +1230,14 @@ Connection.prototype.model = function(name, schema, collection, options) {
1230
1230
  return sub;
1231
1231
  }
1232
1232
 
1233
+ if (arguments.length === 1) {
1234
+ model = this.models[name];
1235
+ if (!model) {
1236
+ throw new MongooseError.MissingSchemaError(name);
1237
+ }
1238
+ return model;
1239
+ }
1240
+
1233
1241
  if (!model) {
1234
1242
  throw new MongooseError.MissingSchemaError(name);
1235
1243
  }
@@ -1324,7 +1332,7 @@ Connection.prototype.deleteModel = function(name) {
1324
1332
  *
1325
1333
  * @api public
1326
1334
  * @param {Array} [pipeline]
1327
- * @param {Object} [options] passed without changes to [the MongoDB driver's `Db#watch()` function](https://mongodb.github.io/node-mongodb-native/3.4/api/Db.html#watch)
1335
+ * @param {Object} [options] passed without changes to [the MongoDB driver's `Db#watch()` function](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#watch)
1328
1336
  * @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
1329
1337
  */
1330
1338
 
@@ -1426,7 +1434,7 @@ Connection.prototype.optionsProvideAuthenticationData = function(options) {
1426
1434
  };
1427
1435
 
1428
1436
  /**
1429
- * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1437
+ * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
1430
1438
  * that this connection uses to talk to MongoDB.
1431
1439
  *
1432
1440
  * #### Example:
@@ -1445,7 +1453,7 @@ Connection.prototype.getClient = function getClient() {
1445
1453
  };
1446
1454
 
1447
1455
  /**
1448
- * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1456
+ * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
1449
1457
  * that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to
1450
1458
  * reuse it.
1451
1459
  *
@@ -71,8 +71,12 @@ function _init(model, c, agg) {
71
71
  }
72
72
  }
73
73
 
74
- /*!
74
+ /**
75
75
  * Necessary to satisfy the Readable API
76
+ * @method _read
77
+ * @memberOf AggregationCursor
78
+ * @instance
79
+ * @api private
76
80
  */
77
81
 
78
82
  AggregationCursor.prototype._read = function() {
@@ -147,8 +151,12 @@ Object.defineProperty(AggregationCursor.prototype, 'map', {
147
151
  writable: true
148
152
  });
149
153
 
150
- /*!
154
+ /**
151
155
  * Marks this cursor as errored
156
+ * @method _markError
157
+ * @instance
158
+ * @memberOf AggregationCursor
159
+ * @api private
152
160
  */
153
161
 
154
162
  AggregationCursor.prototype._markError = function(error) {
@@ -165,7 +173,7 @@ AggregationCursor.prototype._markError = function(error) {
165
173
  * @api public
166
174
  * @method close
167
175
  * @emits close
168
- * @see MongoDB driver cursor#close https://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
176
+ * @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
169
177
  */
170
178
 
171
179
  AggregationCursor.prototype.close = function(callback) {
@@ -223,7 +231,7 @@ AggregationCursor.prototype.eachAsync = function(fn, opts, callback) {
223
231
  };
224
232
 
225
233
  /**
226
- * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js
234
+ * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js)
227
235
  * You do not need to call this function explicitly, the JavaScript runtime
228
236
  * will call it for you.
229
237
  *
@@ -293,7 +301,7 @@ function _transformForAsyncIterator(doc) {
293
301
  }
294
302
 
295
303
  /**
296
- * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
304
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#addCursorFlag).
297
305
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
298
306
  *
299
307
  * @param {String} flag
@@ -324,9 +332,12 @@ function _waitForCursor(ctx, cb) {
324
332
  });
325
333
  }
326
334
 
327
- /*!
335
+ /**
328
336
  * Get the next doc from the underlying cursor and mongooseify it
329
337
  * (populate, etc.)
338
+ * @param {Any} ctx
339
+ * @param {Function} cb
340
+ * @api private
330
341
  */
331
342
 
332
343
  function _next(ctx, cb) {
@@ -85,8 +85,12 @@ function QueryCursor(query, options) {
85
85
 
86
86
  util.inherits(QueryCursor, Readable);
87
87
 
88
- /*!
88
+ /**
89
89
  * Necessary to satisfy the Readable API
90
+ * @method _read
91
+ * @memberOf QueryCursor
92
+ * @instance
93
+ * @api private
90
94
  */
91
95
 
92
96
  QueryCursor.prototype._read = function() {
@@ -152,8 +156,12 @@ Object.defineProperty(QueryCursor.prototype, 'map', {
152
156
  writable: true
153
157
  });
154
158
 
155
- /*!
159
+ /**
156
160
  * Marks this cursor as errored
161
+ * @method _markError
162
+ * @memberOf QueryCursor
163
+ * @instance
164
+ * @api private
157
165
  */
158
166
 
159
167
  QueryCursor.prototype._markError = function(error) {
@@ -170,7 +178,7 @@ QueryCursor.prototype._markError = function(error) {
170
178
  * @api public
171
179
  * @method close
172
180
  * @emits close
173
- * @see MongoDB driver cursor#close https://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
181
+ * @see AggregationCursor.close https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html#close
174
182
  */
175
183
 
176
184
  QueryCursor.prototype.close = function(callback) {
@@ -255,7 +263,7 @@ QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
255
263
  QueryCursor.prototype.options;
256
264
 
257
265
  /**
258
- * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
266
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html#addCursorFlag).
259
267
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
260
268
  *
261
269
  * @param {String} flag
@@ -340,9 +348,12 @@ function _transformForAsyncIterator(doc) {
340
348
  return doc == null ? { done: true } : { value: doc, done: false };
341
349
  }
342
350
 
343
- /*!
351
+ /**
344
352
  * Get the next doc from the underlying cursor and mongooseify it
345
353
  * (populate, etc.)
354
+ * @param {Any} ctx
355
+ * @param {Function} cb
356
+ * @api private
346
357
  */
347
358
 
348
359
  function _next(ctx, cb) {
package/lib/document.js CHANGED
@@ -696,12 +696,15 @@ Document.prototype.$__init = function(doc, opts) {
696
696
  return this;
697
697
  };
698
698
 
699
- /*!
699
+ /**
700
700
  * Init helper.
701
701
  *
702
702
  * @param {Object} self document instance
703
703
  * @param {Object} obj raw mongodb doc
704
704
  * @param {Object} doc object we are initializing
705
+ * @param {Object} [opts] Optional Options
706
+ * @param {Boolean} [opts.setters] Call `applySetters` instead of `cast`
707
+ * @param {String} [prefix] Prefix to add to each path
705
708
  * @api private
706
709
  */
707
710
 
@@ -3678,7 +3681,7 @@ Document.prototype.$toObject = function(options, json) {
3678
3681
  /**
3679
3682
  * Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)).
3680
3683
  *
3681
- * Buffers are converted to instances of [mongodb.Binary](https://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html) for proper storage.
3684
+ * Buffers are converted to instances of [mongodb.Binary](https://mongodb.github.io/node-mongodb-native/4.9/classes/Binary.html) for proper storage.
3682
3685
  *
3683
3686
  * #### Getters/Virtuals
3684
3687
  *
@@ -3805,7 +3808,7 @@ Document.prototype.$toObject = function(options, json) {
3805
3808
  * @param {Boolean} [options.flattenMaps=false] if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`.
3806
3809
  * @param {Boolean} [options.useProjection=false] - If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema.
3807
3810
  * @return {Object} js object (not a POJO)
3808
- * @see mongodb.Binary https://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html
3811
+ * @see mongodb.Binary https://mongodb.github.io/node-mongodb-native/4.9/classes/Binary.html
3809
3812
  * @api public
3810
3813
  * @memberOf Document
3811
3814
  * @instance
@@ -3815,11 +3818,12 @@ Document.prototype.toObject = function(options) {
3815
3818
  return this.$toObject(options);
3816
3819
  };
3817
3820
 
3818
- /*!
3821
+ /**
3819
3822
  * Minimizes an object, removing undefined values and empty objects
3820
3823
  *
3821
3824
  * @param {Object} object to minimize
3822
3825
  * @return {Object}
3826
+ * @api private
3823
3827
  */
3824
3828
 
3825
3829
  function minimize(obj) {
@@ -3925,12 +3929,14 @@ function applyVirtuals(self, json, options, toObjectOptions) {
3925
3929
  }
3926
3930
 
3927
3931
 
3928
- /*!
3932
+ /**
3929
3933
  * Applies virtuals properties to `json`.
3930
3934
  *
3931
3935
  * @param {Document} self
3932
3936
  * @param {Object} json
3937
+ * @param {Object} [options]
3933
3938
  * @return {Object} `json`
3939
+ * @api private
3934
3940
  */
3935
3941
 
3936
3942
  function applyGetters(self, json, options) {
@@ -3980,12 +3986,13 @@ function applyGetters(self, json, options) {
3980
3986
  return json;
3981
3987
  }
3982
3988
 
3983
- /*!
3989
+ /**
3984
3990
  * Applies schema type transforms to `json`.
3985
3991
  *
3986
3992
  * @param {Document} self
3987
3993
  * @param {Object} json
3988
3994
  * @return {Object} `json`
3995
+ * @api private
3989
3996
  */
3990
3997
 
3991
3998
  function applySchemaTypeTransforms(self, json) {
@@ -4158,10 +4165,7 @@ Document.prototype.inspect = function(options) {
4158
4165
  };
4159
4166
 
4160
4167
  if (inspect.custom) {
4161
- /*!
4162
- * Avoid Node deprecation warning DEP0079
4163
- */
4164
-
4168
+ // Avoid Node deprecation warning DEP0079
4165
4169
  Document.prototype[inspect.custom] = Document.prototype.inspect;
4166
4170
  }
4167
4171
 
@@ -4417,8 +4421,13 @@ Document.prototype.$populated = Document.prototype.populated;
4417
4421
  * doc.$assertPopulated('author'); // does not throw
4418
4422
  * doc.$assertPopulated('other path'); // throws an error
4419
4423
  *
4424
+ * // Manually populate and assert in one call. The following does
4425
+ * // `doc.$set({ likes })` before asserting.
4426
+ * doc.$assertPopulated('likes', { likes });
4420
4427
  *
4421
- * @param {String|String[]} paths
4428
+ *
4429
+ * @param {String|String[]} path path or array of paths to check. `$assertPopulated` throws if any of the given paths is not populated.
4430
+ * @param {Object} [values] optional values to `$set()`. Convenient if you want to manually populate a path and assert that the path was populated in 1 call.
4422
4431
  * @return {Document} this
4423
4432
  * @memberOf Document
4424
4433
  * @method $assertPopulated
@@ -4426,14 +4435,18 @@ Document.prototype.$populated = Document.prototype.populated;
4426
4435
  * @api public
4427
4436
  */
4428
4437
 
4429
- Document.prototype.$assertPopulated = function $assertPopulated(paths) {
4430
- if (Array.isArray(paths)) {
4431
- paths.forEach(path => this.$assertPopulated(path));
4438
+ Document.prototype.$assertPopulated = function $assertPopulated(path, values) {
4439
+ if (Array.isArray(path)) {
4440
+ path.forEach(p => this.$assertPopulated(p, values));
4432
4441
  return this;
4433
4442
  }
4434
4443
 
4435
- if (!this.$populated(paths)) {
4436
- throw new MongooseError(`Expected path "${paths}" to be populated`);
4444
+ if (arguments.length > 1) {
4445
+ this.$set(values);
4446
+ }
4447
+
4448
+ if (!this.$populated(path)) {
4449
+ throw new MongooseError(`Expected path "${path}" to be populated`);
4437
4450
  }
4438
4451
 
4439
4452
  return this;
@@ -9,8 +9,9 @@
9
9
 
10
10
  const ObjectId = require('bson').ObjectID;
11
11
 
12
- /*!
12
+ /**
13
13
  * Getter for convenience with populate, see gh-6115
14
+ * @api private
14
15
  */
15
16
 
16
17
  Object.defineProperty(ObjectId.prototype, '_id', {
@@ -7,7 +7,7 @@
7
7
  const mongodb = require('mongodb');
8
8
  const ReadPref = mongodb.ReadPreference;
9
9
 
10
- /*!
10
+ /**
11
11
  * Converts arguments to ReadPrefs the driver
12
12
  * can understand.
13
13
  *
@@ -34,7 +34,7 @@ function NativeCollection(name, conn, options) {
34
34
  * Inherit from abstract Collection.
35
35
  */
36
36
 
37
- NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
37
+ Object.setPrototypeOf(NativeCollection.prototype, MongooseCollection.prototype);
38
38
 
39
39
  /**
40
40
  * Called when the connection opens.
@@ -66,8 +66,10 @@ NativeCollection.prototype.onClose = function(force) {
66
66
 
67
67
  const syncCollectionMethods = { watch: true, find: true, aggregate: true };
68
68
 
69
- /*!
69
+ /**
70
70
  * Copy the collection methods and make them subject to queues
71
+ * @param {Number|String} I
72
+ * @api private
71
73
  */
72
74
 
73
75
  function iter(i) {
@@ -296,8 +298,10 @@ NativeCollection.prototype.$format = function(arg, color, shell) {
296
298
  return format(arg, false, color, shell);
297
299
  };
298
300
 
299
- /*!
301
+ /**
300
302
  * Debug print helper
303
+ * @param {Any} representation
304
+ * @api private
301
305
  */
302
306
 
303
307
  function inspectable(representation) {
@@ -32,7 +32,7 @@ NativeConnection.STATES = STATES;
32
32
  * Inherits from Connection.
33
33
  */
34
34
 
35
- NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
35
+ Object.setPrototypeOf(NativeConnection.prototype, MongooseConnection.prototype);
36
36
 
37
37
  /**
38
38
  * Switches to a different database using the same connection pool.
@@ -8,7 +8,7 @@ const MongooseError = require('./');
8
8
 
9
9
 
10
10
  class MissingSchemaError extends MongooseError {
11
- /*!
11
+ /**
12
12
  * MissingSchema Error constructor.
13
13
  */
14
14
  constructor() {