mongoose 6.4.4 → 6.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/aggregate.js CHANGED
@@ -19,7 +19,7 @@ const validRedactStringValues = new Set(['$$DESCEND', '$$PRUNE', '$$KEEP']);
19
19
 
20
20
  /**
21
21
  * Aggregate constructor used for building aggregation pipelines. Do not
22
- * instantiate this class directly, use [Model.aggregate()](/docs/api.html#model_Model.aggregate) instead.
22
+ * instantiate this class directly, use [Model.aggregate()](/docs/api.html#model_Model-aggregate) instead.
23
23
  *
24
24
  * #### Example:
25
25
  *
@@ -38,11 +38,9 @@ const validRedactStringValues = new Set(['$$DESCEND', '$$PRUNE', '$$KEEP']);
38
38
  * - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
39
39
  * - Mongoose does **not** cast pipeline stages. The below will **not** work unless `_id` is a string in the database
40
40
  *
41
- * ```javascript
42
- * new Aggregate([{ $match: { _id: '00000000000000000000000a' } }]);
43
- * // Do this instead to cast to an ObjectId
44
- * new Aggregate([{ $match: { _id: new mongoose.Types.ObjectId('00000000000000000000000a') } }]);
45
- * ```
41
+ * new Aggregate([{ $match: { _id: '00000000000000000000000a' } }]);
42
+ * // Do this instead to cast to an ObjectId
43
+ * new Aggregate([{ $match: { _id: new mongoose.Types.ObjectId('00000000000000000000000a') } }]);
46
44
  *
47
45
  * @see MongoDB https://docs.mongodb.org/manual/applications/aggregation/
48
46
  * @see driver https://mongodb.github.com/node-mongodb-native/api-generated/collection.html#aggregate
@@ -72,8 +70,8 @@ function Aggregate(pipeline, model) {
72
70
  * - [`cursor`](./api.html#aggregate_Aggregate-cursor)
73
71
  * - [`explain`](./api.html#aggregate_Aggregate-explain)
74
72
  * - `fieldsAsRaw`
75
- * - hint
76
- * - let
73
+ * - `hint`
74
+ * - `let`
77
75
  * - `maxTimeMS`
78
76
  * - `raw`
79
77
  * - `readConcern`
@@ -92,6 +90,7 @@ Aggregate.prototype.options;
92
90
  * Get/set the model that this aggregation will execute on.
93
91
  *
94
92
  * #### Example:
93
+ *
95
94
  * const aggregate = MyModel.aggregate([{ $match: { answer: 42 } }]);
96
95
  * aggregate.model() === MyModel; // true
97
96
  *
@@ -99,7 +98,7 @@ Aggregate.prototype.options;
99
98
  * aggregate.model(SomeOtherModel);
100
99
  * aggregate.model() === SomeOtherModel; // true
101
100
  *
102
- * @param {Model} [model] set the model associated with this aggregate.
101
+ * @param {Model} [model] Set the model associated with this aggregate. If not provided, returns the already stored model.
103
102
  * @return {Model}
104
103
  * @api public
105
104
  */
@@ -135,7 +134,7 @@ Aggregate.prototype.model = function(model) {
135
134
  * const pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
136
135
  * aggregate.append(pipeline);
137
136
  *
138
- * @param {Object} ops operator(s) to append
137
+ * @param {...Object|Object[]} ops operator(s) to append. Can either be a spread of objects or a single parameter of a object array.
139
138
  * @return {Aggregate}
140
139
  * @api public
141
140
  */
@@ -364,7 +363,7 @@ Aggregate.prototype.near = function(arg) {
364
363
  * aggregate.unwind({ path: '$tags', preserveNullAndEmptyArrays: true });
365
364
  *
366
365
  * @see $unwind https://docs.mongodb.org/manual/reference/aggregation/unwind/
367
- * @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 '$'.
366
+ * @param {String|Object|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 '$'.
368
367
  * @return {Aggregate}
369
368
  * @api public
370
369
  */
@@ -495,6 +494,7 @@ Aggregate.prototype.lookup = function(options) {
495
494
  * Note that graphLookup can only consume at most 100MB of memory, and does not allow disk use even if `{ allowDiskUse: true }` is specified.
496
495
  *
497
496
  * #### Examples:
497
+ *
498
498
  * // Suppose we have a collection of courses, where a document might look like `{ _id: 0, name: 'Calculus', prerequisite: 'Trigonometry'}` and `{ _id: 0, name: 'Trigonometry', prerequisite: 'Algebra' }`
499
499
  * aggregate.graphLookup({ from: 'courses', startWith: '$prerequisite', connectFromField: 'prerequisite', connectToField: 'name', as: 'prerequisites', maxDepth: 3 }) // this will recursively search the 'courses' collection up to 3 prerequisites
500
500
  *
@@ -602,7 +602,8 @@ Aggregate.prototype.sort = function(arg) {
602
602
  *
603
603
  * @see $unionWith https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith
604
604
  * @param {Object} options to $unionWith query as described in the above link
605
- * @return {Aggregate}* @api public
605
+ * @return {Aggregate}
606
+ * @api public
606
607
  */
607
608
 
608
609
  Aggregate.prototype.unionWith = function(options) {
@@ -617,8 +618,8 @@ Aggregate.prototype.unionWith = function(options) {
617
618
  *
618
619
  * await Model.aggregate(pipeline).read('primaryPreferred');
619
620
  *
620
- * @param {String} pref one of the listed preference options or their aliases
621
- * @param {Array} [tags] optional tags for this query
621
+ * @param {String|ReadPreference} pref one of the listed preference options or their aliases
622
+ * @param {Array} [tags] optional tags for this query. DEPRECATED
622
623
  * @return {Aggregate} this
623
624
  * @api public
624
625
  * @see mongodb https://docs.mongodb.org/manual/applications/replication/#read-preference
@@ -703,9 +704,9 @@ Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
703
704
  *
704
705
  * Model.aggregate(..).explain(callback)
705
706
  *
706
- * @param {String} verbosity
707
- * @param {Function} callback
708
- * @return {Promise}
707
+ * @param {String} [verbosity]
708
+ * @param {Function} [callback] The callback function to call, if not specified, will return a Promise instead.
709
+ * @return {Promise} Returns a promise if no "callback" is given
709
710
  */
710
711
 
711
712
  Aggregate.prototype.explain = function(verbosity, callback) {
@@ -772,6 +773,7 @@ Aggregate.prototype.explain = function(verbosity, callback) {
772
773
  * await Model.aggregate([{ $match: { foo: 'bar' } }]).allowDiskUse(true);
773
774
  *
774
775
  * @param {Boolean} value Should tell server it can use hard drive to store data during aggregation.
776
+ * @return {Aggregate} this
775
777
  * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
776
778
  */
777
779
 
@@ -788,6 +790,7 @@ Aggregate.prototype.allowDiskUse = function(value) {
788
790
  * Model.aggregate(..).hint({ qty: 1, category: 1 }).exec(callback)
789
791
  *
790
792
  * @param {Object|String} value a hint object or the index name
793
+ * @return {Aggregate} this
791
794
  * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
792
795
  */
793
796
 
@@ -805,6 +808,7 @@ Aggregate.prototype.hint = function(value) {
805
808
  * await Model.aggregate(..).session(session);
806
809
  *
807
810
  * @param {ClientSession} session
811
+ * @return {Aggregate} this
808
812
  * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
809
813
  */
810
814
 
@@ -826,10 +830,10 @@ Aggregate.prototype.session = function(session) {
826
830
  * agg.options; // `{ allowDiskUse: true }`
827
831
  *
828
832
  * @param {Object} options keys to merge into current options
829
- * @param [options.maxTimeMS] number limits the time this aggregation will run, see [MongoDB docs on `maxTimeMS`](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/)
830
- * @param [options.allowDiskUse] boolean if true, the MongoDB server will use the hard drive to store data during this aggregation
831
- * @param [options.collation] object see [`Aggregate.prototype.collation()`](./docs/api.html#aggregate_Aggregate-collation)
832
- * @param [options.session] ClientSession see [`Aggregate.prototype.session()`](./docs/api.html#aggregate_Aggregate-session)
833
+ * @param {Number} [options.maxTimeMS] number limits the time this aggregation will run, see [MongoDB docs on `maxTimeMS`](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/)
834
+ * @param {Boolean} [options.allowDiskUse] boolean if true, the MongoDB server will use the hard drive to store data during this aggregation
835
+ * @param {Object} [options.collation] object see [`Aggregate.prototype.collation()`](./docs/api.html#aggregate_Aggregate-collation)
836
+ * @param {ClientSession} [options.session] ClientSession see [`Aggregate.prototype.session()`](./docs/api.html#aggregate_Aggregate-session)
833
837
  * @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
834
838
  * @return {Aggregate} this
835
839
  * @api public
@@ -855,7 +859,7 @@ Aggregate.prototype.option = function(value) {
855
859
  * });
856
860
  *
857
861
  * @param {Object} options
858
- * @param {Number} options.batchSize set the cursor batch size
862
+ * @param {Number} [options.batchSize] set the cursor batch size
859
863
  * @param {Boolean} [options.useMongooseAggCursor] use experimental mongoose-specific aggregation cursor (for `eachAsync()` and other query cursor semantics)
860
864
  * @return {AggregationCursor} cursor representing this aggregation
861
865
  * @api public
@@ -940,11 +944,10 @@ Aggregate.prototype.search = function(options) {
940
944
  *
941
945
  * MyModel.aggregate().match({ test: 1 }).pipeline(); // [{ $match: { test: 1 } }]
942
946
  *
943
- * @return {Array}
947
+ * @return {Array} The current pipeline similar to the operation that will be executed
944
948
  * @api public
945
949
  */
946
950
 
947
-
948
951
  Aggregate.prototype.pipeline = function() {
949
952
  return this._pipeline;
950
953
  };
@@ -957,12 +960,10 @@ Aggregate.prototype.pipeline = function() {
957
960
  * aggregate.exec(callback);
958
961
  *
959
962
  * // Because a promise is returned, the `callback` is optional.
960
- * const promise = aggregate.exec();
961
- * promise.then(..);
963
+ * const result = await aggregate.exec();
962
964
  *
963
- * @see Promise #promise_Promise
964
965
  * @param {Function} [callback]
965
- * @return {Promise}
966
+ * @return {Promise} Returns a Promise if no "callback" is given.
966
967
  * @api public
967
968
  */
968
969
 
@@ -1018,13 +1019,13 @@ Aggregate.prototype.exec = function(callback) {
1018
1019
  };
1019
1020
 
1020
1021
  /**
1021
- * Provides promise for aggregate.
1022
+ * Provides a Promise-like `then` function, which will call `.exec` without a callback
1023
+ * Compatible with `await`.
1022
1024
  *
1023
1025
  * #### Example:
1024
1026
  *
1025
1027
  * Model.aggregate(..).then(successCallback, errorCallback);
1026
1028
  *
1027
- * @see Promise #promise_Promise
1028
1029
  * @param {Function} [resolve] successCallback
1029
1030
  * @param {Function} [reject] errorCallback
1030
1031
  * @return {Promise}
@@ -1037,6 +1038,7 @@ Aggregate.prototype.then = function(resolve, reject) {
1037
1038
  * Executes the query returning a `Promise` which will be
1038
1039
  * resolved with either the doc(s) or rejected with the error.
1039
1040
  * Like [`.then()`](#query_Query-then), but only takes a rejection handler.
1041
+ * Compatible with `await`.
1040
1042
  *
1041
1043
  * @param {Function} [reject]
1042
1044
  * @return {Promise}
package/lib/browser.js CHANGED
@@ -103,7 +103,7 @@ exports.VirtualType = require('./virtualtype');
103
103
  * _Alias of mongoose.Schema.Types for backwards compatibility._
104
104
  *
105
105
  * @property SchemaTypes
106
- * @see Schema.SchemaTypes #schema_Schema.Types
106
+ * @see Schema.SchemaTypes #schema_Schema-Types
107
107
  * @api public
108
108
  */
109
109
 
package/lib/cast.js CHANGED
@@ -24,8 +24,8 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
24
24
  *
25
25
  * @param {Schema} schema
26
26
  * @param {Object} obj Object to cast
27
- * @param {Object} options the query options
28
- * @param {Query} context passed to setters
27
+ * @param {Object} [options] the query options
28
+ * @param {Query} [context] passed to setters
29
29
  * @api private
30
30
  */
31
31
  module.exports = function cast(schema, obj, options, context) {
package/lib/collection.js CHANGED
@@ -15,7 +15,7 @@ const immediate = require('./helpers/immediate');
15
15
  *
16
16
  * @param {String} name name of the collection
17
17
  * @param {Connection} conn A MongooseConnection instance
18
- * @param {Object} opts optional collection options
18
+ * @param {Object} [opts] optional collection options
19
19
  * @api public
20
20
  */
21
21
 
package/lib/connection.js CHANGED
@@ -393,7 +393,7 @@ Connection.prototype.config;
393
393
  * @param {string} collection The collection to create
394
394
  * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection)
395
395
  * @param {Function} [callback]
396
- * @return {Promise}
396
+ * @return {Promise} Returns a Promise if no `callback` is given.
397
397
  * @api public
398
398
  */
399
399
 
@@ -521,7 +521,7 @@ Connection.prototype.transaction = function transaction(fn, options) {
521
521
  * @method dropCollection
522
522
  * @param {string} collection The collection to delete
523
523
  * @param {Function} [callback]
524
- * @return {Promise}
524
+ * @return {Promise} Returns a Promise if no `callback` is given.
525
525
  * @api public
526
526
  */
527
527
 
@@ -541,7 +541,7 @@ Connection.prototype.dropCollection = _wrapConnHelper(function dropCollection(co
541
541
  *
542
542
  * @method dropDatabase
543
543
  * @param {Function} [callback]
544
- * @return {Promise}
544
+ * @return {Promise} Returns a Promise if no `callback` is given.
545
545
  * @api public
546
546
  */
547
547
 
@@ -608,6 +608,8 @@ Connection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
608
608
  *
609
609
  * @param {Error} err
610
610
  * @param {Function} callback optional
611
+ * @emits "error" Emits the `error` event with the given `err`, unless a callback is specified
612
+ * @returns {Promise|null} Returns a rejected Promise if no `callback` is given.
611
613
  * @api private
612
614
  */
613
615
 
@@ -668,7 +670,7 @@ Connection.prototype.onOpen = function() {
668
670
  * @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.
669
671
  * @param {Boolean} [options.autoCreate=false] Set to `true` to make Mongoose automatically call `createCollection()` on every model created on this connection.
670
672
  * @param {Function} [callback]
671
- * @returns {Connection} this
673
+ * @returns {Promise<Connection>} Returns a Promise if no `callback` is given.
672
674
  * @api public
673
675
  */
674
676
 
@@ -913,6 +915,14 @@ function _setClient(conn, client, options, dbName) {
913
915
  }
914
916
  }
915
917
 
918
+ /**
919
+ * Destory the connection (not just a alias of [`.close`](#connection_Connection-close))
920
+ *
921
+ * @param {Boolean} [force]
922
+ * @param {Function} [callback]
923
+ * @returns {Promise} Returns a Promise if no `callback` is given.
924
+ */
925
+
916
926
  Connection.prototype.destroy = function(force, callback) {
917
927
  if (typeof force === 'function') {
918
928
  callback = force;
@@ -935,7 +945,7 @@ Connection.prototype.destroy = function(force, callback) {
935
945
  *
936
946
  * @param {Boolean} [force] optional
937
947
  * @param {Function} [callback] optional
938
- * @return {Promise}
948
+ * @return {Promise} Returns a Promise if no `callback` is given.
939
949
  * @api public
940
950
  */
941
951
 
@@ -961,7 +971,8 @@ Connection.prototype.close = function(force, callback) {
961
971
  *
962
972
  * @param {Boolean} force
963
973
  * @param {Boolean} destroy
964
- * @param {Function} callback
974
+ * @param {Function} [callback]
975
+ * @returns {Connection} this
965
976
  * @api private
966
977
  */
967
978
  Connection.prototype._close = function(force, destroy, callback) {
@@ -1081,6 +1092,7 @@ Connection.prototype.collection = function(name, options) {
1081
1092
  * Equivalent to calling `.plugin(fn)` on each schema you create.
1082
1093
  *
1083
1094
  * #### Example:
1095
+ *
1084
1096
  * const db = mongoose.createConnection('mongodb://localhost:27017/mydb');
1085
1097
  * db.plugin(() => console.log('Applied'));
1086
1098
  * db.plugins.length; // 1
@@ -1108,7 +1120,7 @@ Connection.prototype.plugin = function(fn, opts) {
1108
1120
  * const Ticket = db.model('Ticket', new Schema(..));
1109
1121
  * const Venue = db.model('Venue');
1110
1122
  *
1111
- * _When no `collection` argument is passed, Mongoose produces a collection name by passing the model `name` to the [utils.toCollectionName](#utils_exports.toCollectionName) method. This method pluralizes the name. If you don't like this behavior, either pass a collection name or set your schemas collection name option._
1123
+ * _When no `collection` argument is passed, Mongoose produces a collection name by passing the model `name` to the [utils.toCollectionName](#utils_exports-toCollectionName) method. This method pluralizes the name. If you don't like this behavior, either pass a collection name or set your schemas collection name option._
1112
1124
  *
1113
1125
  * #### Example:
1114
1126
  *
@@ -1274,7 +1286,7 @@ Connection.prototype.deleteModel = function(name) {
1274
1286
 
1275
1287
  /**
1276
1288
  * Watches the entire underlying database for changes. Similar to
1277
- * [`Model.watch()`](/docs/api/model.html#model_Model.watch).
1289
+ * [`Model.watch()`](/docs/api/model.html#model_Model-watch).
1278
1290
  *
1279
1291
  * This function does **not** trigger any middleware. In particular, it
1280
1292
  * does **not** trigger aggregate middleware.
@@ -1330,6 +1342,7 @@ Connection.prototype.watch = function(pipeline, options) {
1330
1342
  * to connect.
1331
1343
  *
1332
1344
  * #### Example:
1345
+ *
1333
1346
  * const conn = await mongoose.createConnection('mongodb://localhost:27017/test').
1334
1347
  * asPromise();
1335
1348
  * conn.readyState; // 1, means Mongoose is connected
@@ -1345,7 +1358,7 @@ Connection.prototype.asPromise = function asPromise() {
1345
1358
  /**
1346
1359
  * Returns an array of model names created on this connection.
1347
1360
  * @api public
1348
- * @return {Array}
1361
+ * @return {String[]}
1349
1362
  */
1350
1363
 
1351
1364
  Connection.prototype.modelNames = function() {
@@ -1353,9 +1366,10 @@ Connection.prototype.modelNames = function() {
1353
1366
  };
1354
1367
 
1355
1368
  /**
1356
- * @brief Returns if the connection requires authentication after it is opened. Generally if a
1369
+ * Returns if the connection requires authentication after it is opened. Generally if a
1357
1370
  * username and password are both provided than authentication is needed, but in some cases a
1358
1371
  * password is not required.
1372
+ *
1359
1373
  * @api private
1360
1374
  * @return {Boolean} true if the connection should be authenticated after it is opened, otherwise false.
1361
1375
  */
@@ -1365,8 +1379,9 @@ Connection.prototype.shouldAuthenticate = function() {
1365
1379
  };
1366
1380
 
1367
1381
  /**
1368
- * @brief Returns a boolean value that specifies if the current authentication mechanism needs a
1382
+ * Returns a boolean value that specifies if the current authentication mechanism needs a
1369
1383
  * password to authenticate according to the auth objects passed into the openUri methods.
1384
+ *
1370
1385
  * @api private
1371
1386
  * @return {Boolean} true if the authentication mechanism specified in the options object requires
1372
1387
  * a password, otherwise false.
@@ -1379,10 +1394,11 @@ Connection.prototype.authMechanismDoesNotRequirePassword = function() {
1379
1394
  };
1380
1395
 
1381
1396
  /**
1382
- * @brief Returns a boolean value that specifies if the provided objects object provides enough
1397
+ * Returns a boolean value that specifies if the provided objects object provides enough
1383
1398
  * data to authenticate with. Generally this is true if the username and password are both specified
1384
1399
  * but in some authentication methods, a password is not required for authentication so only a username
1385
1400
  * is required.
1401
+ *
1386
1402
  * @param {Object} [options] the options object passed into the openUri methods.
1387
1403
  * @api private
1388
1404
  * @return {Boolean} true if the provided options object provides enough data to authenticate with,
@@ -1399,6 +1415,7 @@ Connection.prototype.optionsProvideAuthenticationData = function(options) {
1399
1415
  * that this connection uses to talk to MongoDB.
1400
1416
  *
1401
1417
  * #### Example:
1418
+ *
1402
1419
  * const conn = await mongoose.createConnection('mongodb://localhost:27017/test').
1403
1420
  * asPromise();
1404
1421
  *
@@ -1418,6 +1435,7 @@ Connection.prototype.getClient = function getClient() {
1418
1435
  * reuse it.
1419
1436
  *
1420
1437
  * #### Example:
1438
+ *
1421
1439
  * const client = await mongodb.MongoClient.connect('mongodb://localhost:27017/test');
1422
1440
  *
1423
1441
  * const conn = mongoose.createConnection().setClient(client);
@@ -1426,6 +1444,7 @@ Connection.prototype.getClient = function getClient() {
1426
1444
  * conn.readyState; // 1, means 'CONNECTED'
1427
1445
  *
1428
1446
  * @api public
1447
+ * @param {MongClient} client The Client to set to be used.
1429
1448
  * @return {Connection} this
1430
1449
  */
1431
1450
 
@@ -1447,12 +1466,11 @@ Connection.prototype.setClient = function setClient(client) {
1447
1466
  };
1448
1467
 
1449
1468
  /**
1450
- *
1451
1469
  * Syncs all the indexes for the models registered with this connection.
1452
1470
  *
1453
- * @param {Object} options
1454
- * @param {Boolean} options.continueOnError `false` by default. If set to `true`, mongoose will not throw an error if one model syncing failed, and will return an object where the keys are the names of the models, and the values are the results/errors for each model.
1455
- * @return {Promise} Returns a Promise, when the Promise resolves the value is a list of the dropped indexes.
1471
+ * @param {Object} [options]
1472
+ * @param {Boolean} [options.continueOnError] `false` by default. If set to `true`, mongoose will not throw an error if one model syncing failed, and will return an object where the keys are the names of the models, and the values are the results/errors for each model.
1473
+ * @return {Promise<Object>} Returns a Promise, when the Promise resolves the value is a list of the dropped indexes.
1456
1474
  */
1457
1475
  Connection.prototype.syncIndexes = async function syncIndexes(options = {}) {
1458
1476
  const result = {};