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
package/index.js CHANGED
@@ -6,4 +6,8 @@
6
6
 
7
7
  'use strict';
8
8
 
9
- module.exports = require('./lib/');
9
+ const mongoose = require('./lib/');
10
+
11
+ module.exports = mongoose;
12
+ module.exports.default = mongoose;
13
+ module.exports.mongoose = mongoose;
package/lib/aggregate.js CHANGED
@@ -42,8 +42,8 @@ const readConcern = Query.prototype.readConcern;
42
42
  * new Aggregate([{ $match: { _id: new mongoose.Types.ObjectId('00000000000000000000000a') } }]);
43
43
  * ```
44
44
  *
45
- * @see MongoDB http://docs.mongodb.org/manual/applications/aggregation/
46
- * @see driver http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#aggregate
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
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
@@ -174,15 +174,10 @@ Aggregate.prototype.append = function() {
174
174
  * @api public
175
175
  */
176
176
  Aggregate.prototype.addFields = function(arg) {
177
- const fields = {};
178
- if (typeof arg === 'object' && !Array.isArray(arg)) {
179
- Object.keys(arg).forEach(function(field) {
180
- fields[field] = arg[field];
181
- });
182
- } else {
177
+ if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
183
178
  throw new Error('Invalid addFields() argument. Must be an object');
184
179
  }
185
- return this.append({ $addFields: fields });
180
+ return this.append({ $addFields: Object.assign({}, arg) });
186
181
  };
187
182
 
188
183
  /**
@@ -212,7 +207,7 @@ Aggregate.prototype.addFields = function(arg) {
212
207
  * aggregate.project({ salary_k: { $divide: [ "$salary", 1000 ] } });
213
208
  *
214
209
  * @param {Object|String} arg field specification
215
- * @see projection http://docs.mongodb.org/manual/reference/aggregation/project/
210
+ * @see projection https://docs.mongodb.org/manual/reference/aggregation/project/
216
211
  * @return {Aggregate}
217
212
  * @api public
218
213
  */
@@ -249,7 +244,7 @@ Aggregate.prototype.project = function(arg) {
249
244
  *
250
245
  * aggregate.group({ _id: "$department" });
251
246
  *
252
- * @see $group http://docs.mongodb.org/manual/reference/aggregation/group/
247
+ * @see $group https://docs.mongodb.org/manual/reference/aggregation/group/
253
248
  * @method group
254
249
  * @memberOf Aggregate
255
250
  * @instance
@@ -265,7 +260,7 @@ Aggregate.prototype.project = function(arg) {
265
260
  *
266
261
  * aggregate.match({ department: { $in: [ "sales", "engineering" ] } });
267
262
  *
268
- * @see $match http://docs.mongodb.org/manual/reference/aggregation/match/
263
+ * @see $match https://docs.mongodb.org/manual/reference/aggregation/match/
269
264
  * @method match
270
265
  * @memberOf Aggregate
271
266
  * @instance
@@ -281,7 +276,7 @@ Aggregate.prototype.project = function(arg) {
281
276
  *
282
277
  * aggregate.skip(10);
283
278
  *
284
- * @see $skip http://docs.mongodb.org/manual/reference/aggregation/skip/
279
+ * @see $skip https://docs.mongodb.org/manual/reference/aggregation/skip/
285
280
  * @method skip
286
281
  * @memberOf Aggregate
287
282
  * @instance
@@ -297,7 +292,7 @@ Aggregate.prototype.project = function(arg) {
297
292
  *
298
293
  * aggregate.limit(10);
299
294
  *
300
- * @see $limit http://docs.mongodb.org/manual/reference/aggregation/limit/
295
+ * @see $limit https://docs.mongodb.org/manual/reference/aggregation/limit/
301
296
  * @method limit
302
297
  * @memberOf Aggregate
303
298
  * @instance
@@ -325,7 +320,7 @@ Aggregate.prototype.project = function(arg) {
325
320
  * num: 5
326
321
  * });
327
322
  *
328
- * @see $geoNear http://docs.mongodb.org/manual/reference/aggregation/geoNear/
323
+ * @see $geoNear https://docs.mongodb.org/manual/reference/aggregation/geoNear/
329
324
  * @method near
330
325
  * @memberOf Aggregate
331
326
  * @instance
@@ -364,7 +359,7 @@ Aggregate.prototype.near = function(arg) {
364
359
  * aggregate.unwind("a", "b", "c");
365
360
  * aggregate.unwind({ path: '$tags', preserveNullAndEmptyArrays: true });
366
361
  *
367
- * @see $unwind http://docs.mongodb.org/manual/reference/aggregation/unwind/
362
+ * @see $unwind https://docs.mongodb.org/manual/reference/aggregation/unwind/
368
363
  * @param {String|Object} fields the field(s) to unwind, either as field names or as [objects with options](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/#document-operand-with-options). If passing a string, prefixing the field name with '$' is optional. If passing an object, `path` must start with '$'.
369
364
  * @return {Aggregate}
370
365
  * @api public
@@ -554,7 +549,7 @@ Aggregate.prototype.sample = function(size) {
554
549
  * aggregate.sort({ field: 'asc', test: -1 });
555
550
  * aggregate.sort('field -test');
556
551
  *
557
- * @see $sort http://docs.mongodb.org/manual/reference/aggregation/sort/
552
+ * @see $sort https://docs.mongodb.org/manual/reference/aggregation/sort/
558
553
  * @param {Object|String} arg
559
554
  * @return {Aggregate} this
560
555
  * @api public
@@ -621,8 +616,8 @@ Aggregate.prototype.unionWith = function(options) {
621
616
  * @param {Array} [tags] optional tags for this query
622
617
  * @return {Aggregate} this
623
618
  * @api public
624
- * @see mongodb http://docs.mongodb.org/manual/applications/replication/#read-preference
625
- * @see driver http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
619
+ * @see mongodb https://docs.mongodb.org/manual/applications/replication/#read-preference
620
+ * @see driver https://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
626
621
  */
627
622
 
628
623
  Aggregate.prototype.read = function(pref, tags) {
@@ -779,7 +774,7 @@ Aggregate.prototype.explain = function(verbosity, callback) {
779
774
  *
780
775
  * @param {Boolean} value Should tell server it can use hard drive to store data during aggregation.
781
776
  * @param {Array} [tags] optional tags for this query
782
- * @see mongodb http://docs.mongodb.org/manual/reference/command/aggregate/
777
+ * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
783
778
  */
784
779
 
785
780
  Aggregate.prototype.allowDiskUse = function(value) {
@@ -795,7 +790,7 @@ Aggregate.prototype.allowDiskUse = function(value) {
795
790
  * Model.aggregate(..).hint({ qty: 1, category: 1 }).exec(callback)
796
791
  *
797
792
  * @param {Object|String} value a hint object or the index name
798
- * @see mongodb http://docs.mongodb.org/manual/reference/command/aggregate/
793
+ * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
799
794
  */
800
795
 
801
796
  Aggregate.prototype.hint = function(value) {
@@ -812,7 +807,7 @@ Aggregate.prototype.hint = function(value) {
812
807
  * await Model.aggregate(..).session(session);
813
808
  *
814
809
  * @param {ClientSession} session
815
- * @see mongodb http://docs.mongodb.org/manual/reference/command/aggregate/
810
+ * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
816
811
  */
817
812
 
818
813
  Aggregate.prototype.session = function(session) {
@@ -837,7 +832,7 @@ Aggregate.prototype.session = function(session) {
837
832
  * @param [options.allowDiskUse] boolean if true, the MongoDB server will use the hard drive to store data during this aggregation
838
833
  * @param [options.collation] object see [`Aggregate.prototype.collation()`](./docs/api.html#aggregate_Aggregate-collation)
839
834
  * @param [options.session] ClientSession see [`Aggregate.prototype.session()`](./docs/api.html#aggregate_Aggregate-session)
840
- * @see mongodb http://docs.mongodb.org/manual/reference/command/aggregate/
835
+ * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
841
836
  * @return {Aggregate} this
842
837
  * @api public
843
838
  */
@@ -866,7 +861,7 @@ Aggregate.prototype.option = function(value) {
866
861
  * @param {Boolean} [options.useMongooseAggCursor] use experimental mongoose-specific aggregation cursor (for `eachAsync()` and other query cursor semantics)
867
862
  * @return {AggregationCursor} cursor representing this aggregation
868
863
  * @api public
869
- * @see mongodb http://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html
864
+ * @see mongodb https://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html
870
865
  */
871
866
 
872
867
  Aggregate.prototype.cursor = function(options) {
@@ -887,7 +882,7 @@ Aggregate.prototype.cursor = function(options) {
887
882
  * @param {Object} collation options
888
883
  * @return {Aggregate} this
889
884
  * @api public
890
- * @see mongodb http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#aggregate
885
+ * @see mongodb https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#aggregate
891
886
  */
892
887
 
893
888
  Aggregate.prototype.collation = function(collation) {
@@ -1105,13 +1100,13 @@ if (Symbol.asyncIterator != null) {
1105
1100
  */
1106
1101
 
1107
1102
  function isOperator(obj) {
1108
- if (typeof obj !== 'object') {
1103
+ if (typeof obj !== 'object' || obj === null) {
1109
1104
  return false;
1110
1105
  }
1111
1106
 
1112
1107
  const k = Object.keys(obj);
1113
1108
 
1114
- return k.length === 1 && k.some(key => { return key[0] === '$'; });
1109
+ return k.length === 1 && k[0][0] === '$';
1115
1110
  }
1116
1111
 
1117
1112
  /*!
@@ -19,7 +19,7 @@ const isObject = require('./helpers/isObject');
19
19
  * @param {Object} obj the values to set
20
20
  * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
21
21
  * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
22
- * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
22
+ * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
23
23
  * @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose.
24
24
  * @event `save`: Emitted when the document is successfully saved
25
25
  * @api private
@@ -3,12 +3,11 @@
3
3
  const assert = require('assert');
4
4
 
5
5
  /*!
6
- * Given a value, cast it to a number, or throw a `CastError` if the value
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
  *
9
9
  * @param {Any} value
10
- * @param {String} [path] optional the path to set on the CastError
11
- * @return {Boolean|null|undefined}
10
+ * @return {Number}
12
11
  * @throws {Error} if `value` is not one of the allowed values
13
12
  * @api private
14
13
  */
package/lib/cast.js CHANGED
@@ -102,15 +102,18 @@ module.exports = function cast(schema, obj, options, context) {
102
102
  const pathFirstHalf = split.slice(0, j).join('.');
103
103
  const pathLastHalf = split.slice(j).join('.');
104
104
  const _schematype = schema.path(pathFirstHalf);
105
- const discriminatorKey = get(_schematype, 'schema.options.discriminatorKey');
105
+ const discriminatorKey = _schematype &&
106
+ _schematype.schema &&
107
+ _schematype.schema.options &&
108
+ _schematype.schema.options.discriminatorKey;
106
109
 
107
110
  // gh-6027: if we haven't found the schematype but this path is
108
111
  // underneath an embedded discriminator and the embedded discriminator
109
112
  // key is in the query, use the embedded discriminator schema
110
113
  if (_schematype != null &&
111
- get(_schematype, 'schema.discriminators') != null &&
112
- discriminatorKey != null &&
113
- pathLastHalf !== discriminatorKey) {
114
+ (_schematype.schema && _schematype.schema.discriminators) != null &&
115
+ discriminatorKey != null &&
116
+ pathLastHalf !== discriminatorKey) {
114
117
  const discriminatorVal = get(obj, pathFirstHalf + '.' + discriminatorKey);
115
118
  if (discriminatorVal != null) {
116
119
  schematype = _schematype.schema.discriminators[discriminatorVal].
package/lib/connection.js CHANGED
@@ -41,7 +41,7 @@ const noPasswordAuthMechanisms = [
41
41
  * For practical reasons, a Connection equals a Db.
42
42
  *
43
43
  * @param {Mongoose} base a mongoose instance
44
- * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
44
+ * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
45
45
  * @event `connecting`: Emitted when `connection.openUri()` is executed on this connection.
46
46
  * @event `connected`: Emitted when this connection successfully connects to the db. May be emitted _multiple_ times in `reconnected` scenarios.
47
47
  * @event `open`: Emitted after we `connected` and `onOpen` is executed on all of this connection's models.
@@ -386,11 +386,11 @@ Connection.prototype.config;
386
386
  * with specified options. Used to create [capped collections](https://docs.mongodb.com/manual/core/capped-collections/)
387
387
  * and [views](https://docs.mongodb.com/manual/core/views/) from mongoose.
388
388
  *
389
- * Options are passed down without modification to the [MongoDB driver's `createCollection()` function](http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
389
+ * 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
390
  *
391
391
  * @method createCollection
392
392
  * @param {string} collection The collection to create
393
- * @param {Object} [options] see [MongoDB driver docs](http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
393
+ * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
394
394
  * @param {Function} [callback]
395
395
  * @return {Promise}
396
396
  * @api public
@@ -407,7 +407,7 @@ Connection.prototype.createCollection = _wrapConnHelper(function createCollectio
407
407
  /**
408
408
  * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
409
409
  * for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
410
- * and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
410
+ * and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
411
411
  *
412
412
  * ####Example:
413
413
  *
@@ -422,7 +422,7 @@ Connection.prototype.createCollection = _wrapConnHelper(function createCollectio
422
422
  *
423
423
  *
424
424
  * @method startSession
425
- * @param {Object} [options] see the [mongodb driver options](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
425
+ * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
426
426
  * @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
427
427
  * @param {Function} [callback]
428
428
  * @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
@@ -444,7 +444,7 @@ Connection.prototype.startSession = _wrapConnHelper(function startSession(option
444
444
  * async function executes successfully and attempt to retry if
445
445
  * there was a retriable error.
446
446
  *
447
- * Calls the MongoDB driver's [`session.withTransaction()`](http://mongodb.github.io/node-mongodb-native/3.5/api/ClientSession.html#withTransaction),
447
+ * Calls the MongoDB driver's [`session.withTransaction()`](https://mongodb.github.io/node-mongodb-native/3.5/api/ClientSession.html#withTransaction),
448
448
  * but also handles resetting Mongoose document state as shown below.
449
449
  *
450
450
  * ####Example:
@@ -651,18 +651,18 @@ Connection.prototype.onOpen = function() {
651
651
  * Opens the connection with a URI using `MongoClient.connect()`.
652
652
  *
653
653
  * @param {String} uri The URI to connect with.
654
- * @param {Object} [options] Passed on to http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
655
- * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
654
+ * @param {Object} [options] Passed on to https://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
655
+ * @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.
656
656
  * @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.
657
657
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
658
658
  * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
659
659
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
660
- * @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](http://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
661
- * @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](http://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
660
+ * @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
661
+ * @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
662
662
  * @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).
663
663
  * @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.
664
664
  * @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
665
- * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
665
+ * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
666
666
  * @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).
667
667
  * @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.
668
668
  * @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.
@@ -682,7 +682,7 @@ Connection.prototype.openUri = function(uri, options, callback) {
682
682
  throw new MongooseError('Mongoose 5.x no longer supports ' +
683
683
  '`mongoose.connect(host, dbname, port)` or ' +
684
684
  '`mongoose.createConnection(host, dbname, port)`. See ' +
685
- 'http://mongoosejs.com/docs/connections.html for supported connection syntax');
685
+ 'https://mongoosejs.com/docs/connections.html for supported connection syntax');
686
686
  }
687
687
 
688
688
  if (typeof uri !== 'string') {
@@ -787,6 +787,11 @@ Connection.prototype.openUri = function(uri, options, callback) {
787
787
  return reject(error);
788
788
  }
789
789
  _this.client = client;
790
+
791
+ for (const db of this.otherDbs) {
792
+ _setClient(db, client, {}, db.name);
793
+ }
794
+
790
795
  client.setMaxListeners(0);
791
796
  client.connect((error) => {
792
797
  if (error) {
@@ -832,9 +837,19 @@ function _setClient(conn, client, options, dbName) {
832
837
  const db = dbName != null ? client.db(dbName) : client.db();
833
838
  conn.db = db;
834
839
  conn.client = client;
835
- conn.host = get(client, 's.options.hosts.0.host', void 0);
836
- conn.port = get(client, 's.options.hosts.0.port', void 0);
837
- conn.name = dbName != null ? dbName : get(client, 's.options.dbName', void 0);
840
+ conn.host = client &&
841
+ client.s &&
842
+ client.s.options &&
843
+ client.s.options.hosts &&
844
+ client.s.options.hosts[0] &&
845
+ client.s.options.hosts[0].host || void 0;
846
+ conn.port = client &&
847
+ client.s &&
848
+ client.s.options &&
849
+ client.s.options.hosts &&
850
+ client.s.options.hosts[0] &&
851
+ client.s.options.hosts[0].port || void 0;
852
+ conn.name = dbName != null ? dbName : client && client.s && client.s.options && client.s.options.dbName || void 0;
838
853
  conn._closeCalled = client._closeCalled;
839
854
 
840
855
  const _handleReconnect = () => {
@@ -850,7 +865,10 @@ function _setClient(conn, client, options, dbName) {
850
865
  }
851
866
  };
852
867
 
853
- const type = get(client, 'topology.description.type', '');
868
+ const type = client &&
869
+ client.topology &&
870
+ client.topology.description &&
871
+ client.topology.description.type || '';
854
872
 
855
873
  if (type === 'Single') {
856
874
  client.on('serverDescriptionChanged', ev => {
@@ -898,7 +916,11 @@ Connection.prototype.close = function(force, callback) {
898
916
  force = false;
899
917
  }
900
918
 
901
- this.$wasForceClosed = !!force;
919
+ if (force != null && typeof force === 'object') {
920
+ this.$wasForceClosed = !!force.force;
921
+ } else {
922
+ this.$wasForceClosed = !!force;
923
+ }
902
924
 
903
925
  return promiseOrCallback(callback, cb => {
904
926
  this._close(force, cb);
@@ -982,7 +1004,7 @@ Connection.prototype.onClose = function(force) {
982
1004
  this.emit('close', force);
983
1005
 
984
1006
  for (const db of this.otherDbs) {
985
- db.close(force);
1007
+ db.close({ force: force, skipCloseClient: true });
986
1008
  }
987
1009
  };
988
1010
 
@@ -1331,7 +1353,7 @@ Connection.prototype.optionsProvideAuthenticationData = function(options) {
1331
1353
  };
1332
1354
 
1333
1355
  /**
1334
- * Returns the [MongoDB driver `MongoClient`](http://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1356
+ * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1335
1357
  * that this connection uses to talk to MongoDB.
1336
1358
  *
1337
1359
  * ####Example:
@@ -1348,7 +1370,7 @@ Connection.prototype.getClient = function getClient() {
1348
1370
  };
1349
1371
 
1350
1372
  /**
1351
- * Set the [MongoDB driver `MongoClient`](http://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1373
+ * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
1352
1374
  * that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to
1353
1375
  * reuse it.
1354
1376
  *
@@ -1376,7 +1398,7 @@ Connection.prototype.setClient = function setClient(client) {
1376
1398
  }
1377
1399
 
1378
1400
  this._connectionString = client.s.url;
1379
- _setClient(this, client, { useUnifiedTopology: client.s.options.useUnifiedTopology }, client.s.options.dbName);
1401
+ _setClient(this, client, {}, client.s.options.dbName);
1380
1402
 
1381
1403
  return this;
1382
1404
  };
@@ -97,7 +97,7 @@ AggregationCursor.prototype._read = function() {
97
97
 
98
98
  if (Symbol.asyncIterator != null) {
99
99
  const msg = 'Mongoose does not support using async iterators with an ' +
100
- 'existing aggregation cursor. See http://bit.ly/mongoose-async-iterate-aggregation';
100
+ 'existing aggregation cursor. See https://bit.ly/mongoose-async-iterate-aggregation';
101
101
 
102
102
  AggregationCursor.prototype[Symbol.asyncIterator] = function() {
103
103
  throw new MongooseError(msg);
@@ -137,10 +137,15 @@ if (Symbol.asyncIterator != null) {
137
137
  * @method map
138
138
  */
139
139
 
140
- AggregationCursor.prototype.map = function(fn) {
141
- this._transforms.push(fn);
142
- return this;
143
- };
140
+ Object.defineProperty(AggregationCursor.prototype, 'map', {
141
+ value: function(fn) {
142
+ this._transforms.push(fn);
143
+ return this;
144
+ },
145
+ enumerable: true,
146
+ configurable: true,
147
+ writable: true
148
+ });
144
149
 
145
150
  /*!
146
151
  * Marks this cursor as errored
@@ -160,7 +165,7 @@ AggregationCursor.prototype._markError = function(error) {
160
165
  * @api public
161
166
  * @method close
162
167
  * @emits close
163
- * @see MongoDB driver cursor#close http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
168
+ * @see MongoDB driver cursor#close https://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
164
169
  */
165
170
 
166
171
  AggregationCursor.prototype.close = function(callback) {
@@ -288,7 +293,7 @@ function _transformForAsyncIterator(doc) {
288
293
  }
289
294
 
290
295
  /**
291
- * Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
296
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
292
297
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
293
298
  *
294
299
  * @param {String} flag
@@ -141,10 +141,15 @@ QueryCursor.prototype._read = function() {
141
141
  * @method map
142
142
  */
143
143
 
144
- QueryCursor.prototype.map = function(fn) {
145
- this._transforms.push(fn);
146
- return this;
147
- };
144
+ Object.defineProperty(QueryCursor.prototype, 'map', {
145
+ value: function(fn) {
146
+ this._transforms.push(fn);
147
+ return this;
148
+ },
149
+ enumerable: true,
150
+ configurable: true,
151
+ writable: true
152
+ });
148
153
 
149
154
  /*!
150
155
  * Marks this cursor as errored
@@ -164,7 +169,7 @@ QueryCursor.prototype._markError = function(error) {
164
169
  * @api public
165
170
  * @method close
166
171
  * @emits close
167
- * @see MongoDB driver cursor#close http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
172
+ * @see MongoDB driver cursor#close https://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#close
168
173
  */
169
174
 
170
175
  QueryCursor.prototype.close = function(callback) {
@@ -247,7 +252,7 @@ QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
247
252
  QueryCursor.prototype.options;
248
253
 
249
254
  /**
250
- * Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
255
+ * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
251
256
  * Useful for setting the `noCursorTimeout` and `tailable` flags.
252
257
  *
253
258
  * @param {String} flag