mongoose 6.2.9 → 6.3.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.
- package/CHANGELOG.md +28 -0
- package/dist/browser.umd.js +2 -1693
- package/lib/aggregate.js +59 -67
- package/lib/browser.js +4 -4
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -2
- package/lib/cursor/ChangeStream.js +42 -2
- package/lib/cursor/QueryCursor.js +5 -3
- package/lib/document.js +39 -46
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +2 -2
- package/lib/helpers/cursor/eachAsync.js +44 -12
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/model/discriminator.js +1 -3
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/query/applyGlobalOption.js +29 -0
- package/lib/helpers/query/castUpdate.js +3 -1
- package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
- package/lib/helpers/update/applyTimestampsToUpdate.js +0 -1
- package/lib/index.js +33 -26
- package/lib/model.js +88 -90
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +2 -2
- package/lib/options/SchemaDocumentArrayOptions.js +3 -3
- package/lib/options/SchemaMapOptions.js +2 -2
- package/lib/options/SchemaNumberOptions.js +3 -3
- package/lib/options/SchemaObjectIdOptions.js +2 -2
- package/lib/options/SchemaStringOptions.js +1 -1
- package/lib/options/SchemaSubdocumentOptions.js +2 -2
- package/lib/options/SchemaTypeOptions.js +3 -3
- package/lib/query.js +273 -249
- package/lib/schema/SubdocumentPath.js +4 -3
- package/lib/schema/array.js +2 -2
- package/lib/schema/boolean.js +4 -4
- package/lib/schema/buffer.js +3 -3
- package/lib/schema/date.js +7 -7
- package/lib/schema/decimal128.js +2 -2
- package/lib/schema/documentarray.js +3 -3
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +6 -6
- package/lib/schema/objectid.js +4 -7
- package/lib/schema/string.js +38 -16
- package/lib/schema.js +144 -30
- package/lib/schematype.js +75 -68
- package/lib/types/ArraySubdocument.js +1 -1
- package/lib/types/DocumentArray/methods/index.js +2 -2
- package/lib/types/array/index.js +1 -1
- package/lib/types/array/methods/index.js +13 -13
- package/lib/types/buffer.js +1 -1
- package/lib/types/decimal128.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +31 -2
- package/lib/validoptions.js +1 -0
- package/lib/virtualtype.js +3 -3
- package/package.json +19 -13
- package/tools/repl.js +2 -1
- package/types/aggregate.d.ts +223 -0
- package/types/cursor.d.ts +10 -4
- package/types/index.d.ts +194 -209
- package/types/mongooseoptions.d.ts +10 -4
- package/lib/helpers/query/applyGlobalMaxTimeMS.js +0 -15
package/lib/aggregate.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
const AggregationCursor = require('./cursor/AggregationCursor');
|
|
8
8
|
const Query = require('./query');
|
|
9
|
-
const applyGlobalMaxTimeMS = require('./helpers/query/
|
|
9
|
+
const { applyGlobalMaxTimeMS, applyGlobalDiskUse } = require('./helpers/query/applyGlobalOption');
|
|
10
10
|
const getConstructorName = require('./helpers/getConstructorName');
|
|
11
11
|
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
|
|
12
12
|
const promiseOrCallback = require('./helpers/promiseOrCallback');
|
|
@@ -15,11 +15,13 @@ const utils = require('./utils');
|
|
|
15
15
|
const read = Query.prototype.read;
|
|
16
16
|
const readConcern = Query.prototype.readConcern;
|
|
17
17
|
|
|
18
|
+
const validRedactStringValues = new Set(['$$DESCEND', '$$PRUNE', '$$KEEP']);
|
|
19
|
+
|
|
18
20
|
/**
|
|
19
21
|
* Aggregate constructor used for building aggregation pipelines. Do not
|
|
20
22
|
* instantiate this class directly, use [Model.aggregate()](/docs/api.html#model_Model.aggregate) instead.
|
|
21
23
|
*
|
|
22
|
-
* ####Example:
|
|
24
|
+
* #### Example:
|
|
23
25
|
*
|
|
24
26
|
* const aggregate = Model.aggregate([
|
|
25
27
|
* { $project: { a: 1, b: 1 } },
|
|
@@ -31,7 +33,7 @@ const readConcern = Query.prototype.readConcern;
|
|
|
31
33
|
* unwind('tags').
|
|
32
34
|
* exec(callback);
|
|
33
35
|
*
|
|
34
|
-
* ####Note:
|
|
36
|
+
* #### Note:
|
|
35
37
|
*
|
|
36
38
|
* - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
|
|
37
39
|
* - Mongoose does **not** cast pipeline stages. The below will **not** work unless `_id` is a string in the database
|
|
@@ -63,19 +65,21 @@ function Aggregate(pipeline, model) {
|
|
|
63
65
|
* Contains options passed down to the [aggregate command](https://docs.mongodb.com/manual/reference/command/aggregate/).
|
|
64
66
|
* Supported options are:
|
|
65
67
|
*
|
|
66
|
-
* - `readPreference`
|
|
67
|
-
* - [`cursor`](./api.html#aggregate_Aggregate-cursor)
|
|
68
|
-
* - [`explain`](./api.html#aggregate_Aggregate-explain)
|
|
69
68
|
* - [`allowDiskUse`](./api.html#aggregate_Aggregate-allowDiskUse)
|
|
70
|
-
* - `maxTimeMS`
|
|
71
69
|
* - `bypassDocumentValidation`
|
|
72
|
-
* - `raw`
|
|
73
|
-
* - `promoteLongs`
|
|
74
|
-
* - `promoteValues`
|
|
75
|
-
* - `promoteBuffers`
|
|
76
70
|
* - [`collation`](./api.html#aggregate_Aggregate-collation)
|
|
77
71
|
* - `comment`
|
|
72
|
+
* - [`cursor`](./api.html#aggregate_Aggregate-cursor)
|
|
73
|
+
* - [`explain`](./api.html#aggregate_Aggregate-explain)
|
|
74
|
+
* - `fieldsAsRaw`
|
|
75
|
+
* - hint
|
|
76
|
+
* - let
|
|
77
|
+
* - `maxTimeMS`
|
|
78
|
+
* - `raw`
|
|
79
|
+
* - `readConcern`
|
|
80
|
+
* - `readPreference`
|
|
78
81
|
* - [`session`](./api.html#aggregate_Aggregate-session)
|
|
82
|
+
* - `writeConcern`
|
|
79
83
|
*
|
|
80
84
|
* @property options
|
|
81
85
|
* @memberOf Aggregate
|
|
@@ -87,7 +91,7 @@ Aggregate.prototype.options;
|
|
|
87
91
|
/**
|
|
88
92
|
* Get/set the model that this aggregation will execute on.
|
|
89
93
|
*
|
|
90
|
-
* ####Example:
|
|
94
|
+
* #### Example:
|
|
91
95
|
* const aggregate = MyModel.aggregate([{ $match: { answer: 42 } }]);
|
|
92
96
|
* aggregate.model() === MyModel; // true
|
|
93
97
|
*
|
|
@@ -123,7 +127,7 @@ Aggregate.prototype.model = function(model) {
|
|
|
123
127
|
/**
|
|
124
128
|
* Appends new operators to this aggregate pipeline
|
|
125
129
|
*
|
|
126
|
-
* ####Examples:
|
|
130
|
+
* #### Examples:
|
|
127
131
|
*
|
|
128
132
|
* aggregate.append({ $project: { field: 1 }}, { $limit: 2 });
|
|
129
133
|
*
|
|
@@ -154,7 +158,7 @@ Aggregate.prototype.append = function() {
|
|
|
154
158
|
* Appends a new $addFields operator to this aggregate pipeline.
|
|
155
159
|
* Requires MongoDB v3.4+ to work
|
|
156
160
|
*
|
|
157
|
-
* ####Examples:
|
|
161
|
+
* #### Examples:
|
|
158
162
|
*
|
|
159
163
|
* // adding new fields based on existing fields
|
|
160
164
|
* aggregate.addFields({
|
|
@@ -185,7 +189,7 @@ Aggregate.prototype.addFields = function(arg) {
|
|
|
185
189
|
*
|
|
186
190
|
* Mongoose query [selection syntax](#query_Query-select) is also supported.
|
|
187
191
|
*
|
|
188
|
-
* ####Examples:
|
|
192
|
+
* #### Examples:
|
|
189
193
|
*
|
|
190
194
|
* // include a, include b, exclude _id
|
|
191
195
|
* aggregate.project("a b -_id");
|
|
@@ -240,7 +244,7 @@ Aggregate.prototype.project = function(arg) {
|
|
|
240
244
|
/**
|
|
241
245
|
* Appends a new custom $group operator to this aggregate pipeline.
|
|
242
246
|
*
|
|
243
|
-
* ####Examples:
|
|
247
|
+
* #### Examples:
|
|
244
248
|
*
|
|
245
249
|
* aggregate.group({ _id: "$department" });
|
|
246
250
|
*
|
|
@@ -256,7 +260,7 @@ Aggregate.prototype.project = function(arg) {
|
|
|
256
260
|
/**
|
|
257
261
|
* Appends a new custom $match operator to this aggregate pipeline.
|
|
258
262
|
*
|
|
259
|
-
* ####Examples:
|
|
263
|
+
* #### Examples:
|
|
260
264
|
*
|
|
261
265
|
* aggregate.match({ department: { $in: [ "sales", "engineering" ] } });
|
|
262
266
|
*
|
|
@@ -272,7 +276,7 @@ Aggregate.prototype.project = function(arg) {
|
|
|
272
276
|
/**
|
|
273
277
|
* Appends a new $skip operator to this aggregate pipeline.
|
|
274
278
|
*
|
|
275
|
-
* ####Examples:
|
|
279
|
+
* #### Examples:
|
|
276
280
|
*
|
|
277
281
|
* aggregate.skip(10);
|
|
278
282
|
*
|
|
@@ -288,7 +292,7 @@ Aggregate.prototype.project = function(arg) {
|
|
|
288
292
|
/**
|
|
289
293
|
* Appends a new $limit operator to this aggregate pipeline.
|
|
290
294
|
*
|
|
291
|
-
* ####Examples:
|
|
295
|
+
* #### Examples:
|
|
292
296
|
*
|
|
293
297
|
* aggregate.limit(10);
|
|
294
298
|
*
|
|
@@ -304,11 +308,11 @@ Aggregate.prototype.project = function(arg) {
|
|
|
304
308
|
/**
|
|
305
309
|
* Appends a new $geoNear operator to this aggregate pipeline.
|
|
306
310
|
*
|
|
307
|
-
* ####
|
|
311
|
+
* #### Note:
|
|
308
312
|
*
|
|
309
313
|
* **MUST** be used as the first operator in the pipeline.
|
|
310
314
|
*
|
|
311
|
-
* ####Examples:
|
|
315
|
+
* #### Examples:
|
|
312
316
|
*
|
|
313
317
|
* aggregate.near({
|
|
314
318
|
* near: [40.724, -73.997],
|
|
@@ -353,7 +357,7 @@ Aggregate.prototype.near = function(arg) {
|
|
|
353
357
|
* Note that the `$unwind` operator requires the path name to start with '$'.
|
|
354
358
|
* Mongoose will prepend '$' if the specified field doesn't start '$'.
|
|
355
359
|
*
|
|
356
|
-
* ####Examples:
|
|
360
|
+
* #### Examples:
|
|
357
361
|
*
|
|
358
362
|
* aggregate.unwind("tags");
|
|
359
363
|
* aggregate.unwind("a", "b", "c");
|
|
@@ -374,7 +378,7 @@ Aggregate.prototype.unwind = function() {
|
|
|
374
378
|
res.push({ $unwind: arg });
|
|
375
379
|
} else if (typeof arg === 'string') {
|
|
376
380
|
res.push({
|
|
377
|
-
$unwind: (arg
|
|
381
|
+
$unwind: (arg[0] === '$') ? arg : '$' + arg
|
|
378
382
|
});
|
|
379
383
|
} else {
|
|
380
384
|
throw new Error('Invalid arg "' + arg + '" to unwind(), ' +
|
|
@@ -392,14 +396,14 @@ Aggregate.prototype.unwind = function() {
|
|
|
392
396
|
* If you are passing in a string Mongoose will prepend '$' if the specified field doesn't start '$'.
|
|
393
397
|
* If you are passing in an object the strings in your expression will not be altered.
|
|
394
398
|
*
|
|
395
|
-
* ####Examples:
|
|
399
|
+
* #### Examples:
|
|
396
400
|
*
|
|
397
401
|
* aggregate.replaceRoot("user");
|
|
398
402
|
*
|
|
399
403
|
* aggregate.replaceRoot({ x: { $concat: ['$this', '$that'] } });
|
|
400
404
|
*
|
|
401
405
|
* @see $replaceRoot https://docs.mongodb.org/manual/reference/operator/aggregation/replaceRoot
|
|
402
|
-
* @param {String|Object} the field or document which will become the new root document
|
|
406
|
+
* @param {String|Object} newRoot the field or document which will become the new root document
|
|
403
407
|
* @return {Aggregate}
|
|
404
408
|
* @api public
|
|
405
409
|
*/
|
|
@@ -423,18 +427,18 @@ Aggregate.prototype.replaceRoot = function(newRoot) {
|
|
|
423
427
|
/**
|
|
424
428
|
* Appends a new $count operator to this aggregate pipeline.
|
|
425
429
|
*
|
|
426
|
-
* ####Examples:
|
|
430
|
+
* #### Examples:
|
|
427
431
|
*
|
|
428
432
|
* aggregate.count("userCount");
|
|
429
433
|
*
|
|
430
434
|
* @see $count https://docs.mongodb.org/manual/reference/operator/aggregation/count
|
|
431
|
-
* @param {String}
|
|
435
|
+
* @param {String} fieldName The name of the output field which has the count as its value. It must be a non-empty string, must not start with $ and must not contain the . character.
|
|
432
436
|
* @return {Aggregate}
|
|
433
437
|
* @api public
|
|
434
438
|
*/
|
|
435
439
|
|
|
436
|
-
Aggregate.prototype.count = function(
|
|
437
|
-
return this.append({ $count:
|
|
440
|
+
Aggregate.prototype.count = function(fieldName) {
|
|
441
|
+
return this.append({ $count: fieldName });
|
|
438
442
|
};
|
|
439
443
|
|
|
440
444
|
/**
|
|
@@ -444,7 +448,7 @@ Aggregate.prototype.count = function(countName) {
|
|
|
444
448
|
* Note that the `$sortByCount` operator requires the new root to start with '$'.
|
|
445
449
|
* Mongoose will prepend '$' if the specified field name doesn't start with '$'.
|
|
446
450
|
*
|
|
447
|
-
* ####Examples:
|
|
451
|
+
* #### Examples:
|
|
448
452
|
*
|
|
449
453
|
* aggregate.sortByCount('users');
|
|
450
454
|
* aggregate.sortByCount({ $mergeObjects: [ "$employee", "$business" ] })
|
|
@@ -460,7 +464,7 @@ Aggregate.prototype.sortByCount = function(arg) {
|
|
|
460
464
|
return this.append({ $sortByCount: arg });
|
|
461
465
|
} else if (typeof arg === 'string') {
|
|
462
466
|
return this.append({
|
|
463
|
-
$sortByCount: (arg
|
|
467
|
+
$sortByCount: (arg[0] === '$') ? arg : '$' + arg
|
|
464
468
|
});
|
|
465
469
|
} else {
|
|
466
470
|
throw new TypeError('Invalid arg "' + arg + '" to sortByCount(), ' +
|
|
@@ -471,7 +475,7 @@ Aggregate.prototype.sortByCount = function(arg) {
|
|
|
471
475
|
/**
|
|
472
476
|
* Appends new custom $lookup operator to this aggregate pipeline.
|
|
473
477
|
*
|
|
474
|
-
* ####Examples:
|
|
478
|
+
* #### Examples:
|
|
475
479
|
*
|
|
476
480
|
* aggregate.lookup({ from: 'users', localField: 'userId', foreignField: '_id', as: 'users' });
|
|
477
481
|
*
|
|
@@ -522,7 +526,7 @@ Aggregate.prototype.graphLookup = function(options) {
|
|
|
522
526
|
/**
|
|
523
527
|
* Appends new custom $sample operator to this aggregate pipeline.
|
|
524
528
|
*
|
|
525
|
-
* ####Examples:
|
|
529
|
+
* #### Examples:
|
|
526
530
|
*
|
|
527
531
|
* aggregate.sample(3); // Add a pipeline that picks 3 random documents
|
|
528
532
|
*
|
|
@@ -543,7 +547,7 @@ Aggregate.prototype.sample = function(size) {
|
|
|
543
547
|
*
|
|
544
548
|
* If a string is passed, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with `-` which will be treated as descending.
|
|
545
549
|
*
|
|
546
|
-
* ####Examples:
|
|
550
|
+
* #### Examples:
|
|
547
551
|
*
|
|
548
552
|
* // these are equivalent
|
|
549
553
|
* aggregate.sort({ field: 'asc', test: -1 });
|
|
@@ -591,7 +595,7 @@ Aggregate.prototype.sort = function(arg) {
|
|
|
591
595
|
/**
|
|
592
596
|
* Appends new $unionWith operator to this aggregate pipeline.
|
|
593
597
|
*
|
|
594
|
-
* ####Examples:
|
|
598
|
+
* #### Examples:
|
|
595
599
|
*
|
|
596
600
|
* aggregate.unionWith({ coll: 'users', pipeline: [ { $match: { _id: 1 } } ] });
|
|
597
601
|
*
|
|
@@ -608,7 +612,7 @@ Aggregate.prototype.unionWith = function(options) {
|
|
|
608
612
|
/**
|
|
609
613
|
* Sets the readPreference option for the aggregation query.
|
|
610
614
|
*
|
|
611
|
-
* ####Example:
|
|
615
|
+
* #### Example:
|
|
612
616
|
*
|
|
613
617
|
* await Model.aggregate(pipeline).read('primaryPreferred');
|
|
614
618
|
*
|
|
@@ -621,9 +625,6 @@ Aggregate.prototype.unionWith = function(options) {
|
|
|
621
625
|
*/
|
|
622
626
|
|
|
623
627
|
Aggregate.prototype.read = function(pref, tags) {
|
|
624
|
-
if (!this.options) {
|
|
625
|
-
this.options = {};
|
|
626
|
-
}
|
|
627
628
|
read.call(this, pref, tags);
|
|
628
629
|
return this;
|
|
629
630
|
};
|
|
@@ -631,7 +632,7 @@ Aggregate.prototype.read = function(pref, tags) {
|
|
|
631
632
|
/**
|
|
632
633
|
* Sets the readConcern level for the aggregation query.
|
|
633
634
|
*
|
|
634
|
-
* ####Example:
|
|
635
|
+
* #### Example:
|
|
635
636
|
*
|
|
636
637
|
* await Model.aggregate(pipeline).readConcern('majority');
|
|
637
638
|
*
|
|
@@ -642,9 +643,6 @@ Aggregate.prototype.read = function(pref, tags) {
|
|
|
642
643
|
*/
|
|
643
644
|
|
|
644
645
|
Aggregate.prototype.readConcern = function(level) {
|
|
645
|
-
if (!this.options) {
|
|
646
|
-
this.options = {};
|
|
647
|
-
}
|
|
648
646
|
readConcern.call(this, level);
|
|
649
647
|
return this;
|
|
650
648
|
};
|
|
@@ -655,7 +653,7 @@ Aggregate.prototype.readConcern = function(level) {
|
|
|
655
653
|
* If 3 arguments are supplied, Mongoose will wrap them with if-then-else of $cond operator respectively
|
|
656
654
|
* If `thenExpr` or `elseExpr` is string, make sure it starts with $$, like `$$DESCEND`, `$$PRUNE` or `$$KEEP`.
|
|
657
655
|
*
|
|
658
|
-
* ####Example:
|
|
656
|
+
* #### Example:
|
|
659
657
|
*
|
|
660
658
|
* await Model.aggregate(pipeline).redact({
|
|
661
659
|
* $cond: {
|
|
@@ -678,9 +676,9 @@ Aggregate.prototype.readConcern = function(level) {
|
|
|
678
676
|
|
|
679
677
|
Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
|
|
680
678
|
if (arguments.length === 3) {
|
|
681
|
-
if ((typeof thenExpr === 'string' && !
|
|
682
|
-
(typeof elseExpr === 'string' && !
|
|
683
|
-
throw new Error('If thenExpr or elseExpr is string, it must
|
|
679
|
+
if ((typeof thenExpr === 'string' && !validRedactStringValues.has(thenExpr)) ||
|
|
680
|
+
(typeof elseExpr === 'string' && !validRedactStringValues.has(elseExpr))) {
|
|
681
|
+
throw new Error('If thenExpr or elseExpr is string, it must be either $$DESCEND, $$PRUNE or $$KEEP');
|
|
684
682
|
}
|
|
685
683
|
|
|
686
684
|
expression = {
|
|
@@ -700,7 +698,7 @@ Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
|
|
|
700
698
|
/**
|
|
701
699
|
* Execute the aggregation with explain
|
|
702
700
|
*
|
|
703
|
-
* ####Example:
|
|
701
|
+
* #### Example:
|
|
704
702
|
*
|
|
705
703
|
* Model.aggregate(..).explain(callback)
|
|
706
704
|
*
|
|
@@ -768,12 +766,11 @@ Aggregate.prototype.explain = function(verbosity, callback) {
|
|
|
768
766
|
/**
|
|
769
767
|
* Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0)
|
|
770
768
|
*
|
|
771
|
-
* ####Example:
|
|
769
|
+
* #### Example:
|
|
772
770
|
*
|
|
773
771
|
* await Model.aggregate([{ $match: { foo: 'bar' } }]).allowDiskUse(true);
|
|
774
772
|
*
|
|
775
773
|
* @param {Boolean} value Should tell server it can use hard drive to store data during aggregation.
|
|
776
|
-
* @param {Array} [tags] optional tags for this query
|
|
777
774
|
* @see mongodb https://docs.mongodb.org/manual/reference/command/aggregate/
|
|
778
775
|
*/
|
|
779
776
|
|
|
@@ -785,7 +782,7 @@ Aggregate.prototype.allowDiskUse = function(value) {
|
|
|
785
782
|
/**
|
|
786
783
|
* Sets the hint option for the aggregation query (ignored for < 3.6.0)
|
|
787
784
|
*
|
|
788
|
-
* ####Example:
|
|
785
|
+
* #### Example:
|
|
789
786
|
*
|
|
790
787
|
* Model.aggregate(..).hint({ qty: 1, category: 1 }).exec(callback)
|
|
791
788
|
*
|
|
@@ -801,7 +798,7 @@ Aggregate.prototype.hint = function(value) {
|
|
|
801
798
|
/**
|
|
802
799
|
* Sets the session for this aggregation. Useful for [transactions](/docs/transactions.html).
|
|
803
800
|
*
|
|
804
|
-
* ####Example:
|
|
801
|
+
* #### Example:
|
|
805
802
|
*
|
|
806
803
|
* const session = await Model.startSession();
|
|
807
804
|
* await Model.aggregate(..).session(session);
|
|
@@ -822,7 +819,7 @@ Aggregate.prototype.session = function(session) {
|
|
|
822
819
|
/**
|
|
823
820
|
* Lets you set arbitrary options, for middleware or plugins.
|
|
824
821
|
*
|
|
825
|
-
* ####Example:
|
|
822
|
+
* #### Example:
|
|
826
823
|
*
|
|
827
824
|
* const agg = Model.aggregate(..).option({ allowDiskUse: true }); // Set the `allowDiskUse` option
|
|
828
825
|
* agg.options; // `{ allowDiskUse: true }`
|
|
@@ -849,7 +846,7 @@ Aggregate.prototype.option = function(value) {
|
|
|
849
846
|
* Cursors are useful if you want to process the results of the aggregation one-at-a-time
|
|
850
847
|
* because the aggregation result is too big to fit into memory.
|
|
851
848
|
*
|
|
852
|
-
* ####Example:
|
|
849
|
+
* #### Example:
|
|
853
850
|
*
|
|
854
851
|
* const cursor = Model.aggregate(..).cursor({ batchSize: 1000 });
|
|
855
852
|
* cursor.eachAsync(function(doc, i) {
|
|
@@ -865,9 +862,6 @@ Aggregate.prototype.option = function(value) {
|
|
|
865
862
|
*/
|
|
866
863
|
|
|
867
864
|
Aggregate.prototype.cursor = function(options) {
|
|
868
|
-
if (!this.options) {
|
|
869
|
-
this.options = {};
|
|
870
|
-
}
|
|
871
865
|
this.options.cursor = options || {};
|
|
872
866
|
return new AggregationCursor(this); // return this;
|
|
873
867
|
};
|
|
@@ -875,7 +869,7 @@ Aggregate.prototype.cursor = function(options) {
|
|
|
875
869
|
/**
|
|
876
870
|
* Adds a collation
|
|
877
871
|
*
|
|
878
|
-
* ####Example:
|
|
872
|
+
* #### Example:
|
|
879
873
|
*
|
|
880
874
|
* const res = await Model.aggregate(pipeline).collation({ locale: 'en_US', strength: 1 });
|
|
881
875
|
*
|
|
@@ -886,9 +880,6 @@ Aggregate.prototype.cursor = function(options) {
|
|
|
886
880
|
*/
|
|
887
881
|
|
|
888
882
|
Aggregate.prototype.collation = function(collation) {
|
|
889
|
-
if (!this.options) {
|
|
890
|
-
this.options = {};
|
|
891
|
-
}
|
|
892
883
|
this.options.collation = collation;
|
|
893
884
|
return this;
|
|
894
885
|
};
|
|
@@ -896,7 +887,7 @@ Aggregate.prototype.collation = function(collation) {
|
|
|
896
887
|
/**
|
|
897
888
|
* Combines multiple aggregation pipelines.
|
|
898
889
|
*
|
|
899
|
-
* ####Example:
|
|
890
|
+
* #### Example:
|
|
900
891
|
*
|
|
901
892
|
* const res = await Model.aggregate().facet({
|
|
902
893
|
* books: [{ groupBy: '$author' }],
|
|
@@ -919,7 +910,7 @@ Aggregate.prototype.facet = function(options) {
|
|
|
919
910
|
* Helper for [Atlas Text Search](https://docs.atlas.mongodb.com/reference/atlas-search/tutorial/)'s
|
|
920
911
|
* `$search` stage.
|
|
921
912
|
*
|
|
922
|
-
* ####Example:
|
|
913
|
+
* #### Example:
|
|
923
914
|
*
|
|
924
915
|
* const res = await Model.aggregate().
|
|
925
916
|
* search({
|
|
@@ -944,7 +935,7 @@ Aggregate.prototype.search = function(options) {
|
|
|
944
935
|
/**
|
|
945
936
|
* Returns the current pipeline
|
|
946
937
|
*
|
|
947
|
-
* ####Example:
|
|
938
|
+
* #### Example:
|
|
948
939
|
*
|
|
949
940
|
* MyModel.aggregate().match({ test: 1 }).pipeline(); // [{ $match: { test: 1 } }]
|
|
950
941
|
*
|
|
@@ -960,7 +951,7 @@ Aggregate.prototype.pipeline = function() {
|
|
|
960
951
|
/**
|
|
961
952
|
* Executes the aggregate pipeline on the currently bound Model.
|
|
962
953
|
*
|
|
963
|
-
* ####Example:
|
|
954
|
+
* #### Example:
|
|
964
955
|
*
|
|
965
956
|
* aggregate.exec(callback);
|
|
966
957
|
*
|
|
@@ -982,6 +973,7 @@ Aggregate.prototype.exec = function(callback) {
|
|
|
982
973
|
const collection = this._model.collection;
|
|
983
974
|
|
|
984
975
|
applyGlobalMaxTimeMS(this.options, model);
|
|
976
|
+
applyGlobalDiskUse(this.options, model);
|
|
985
977
|
|
|
986
978
|
if (this.options && this.options.cursor) {
|
|
987
979
|
return new AggregationCursor(this);
|
|
@@ -1027,7 +1019,7 @@ Aggregate.prototype.exec = function(callback) {
|
|
|
1027
1019
|
/**
|
|
1028
1020
|
* Provides promise for aggregate.
|
|
1029
1021
|
*
|
|
1030
|
-
* ####Example:
|
|
1022
|
+
* #### Example:
|
|
1031
1023
|
*
|
|
1032
1024
|
* Model.aggregate(..).then(successCallback, errorCallback);
|
|
1033
1025
|
*
|
|
@@ -1059,7 +1051,7 @@ Aggregate.prototype.catch = function(reject) {
|
|
|
1059
1051
|
* You do not need to call this function explicitly, the JavaScript runtime
|
|
1060
1052
|
* will call it for you.
|
|
1061
1053
|
*
|
|
1062
|
-
* ####Example
|
|
1054
|
+
* #### Example
|
|
1063
1055
|
*
|
|
1064
1056
|
* const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]);
|
|
1065
1057
|
* for await (const doc of agg) {
|
package/lib/browser.js
CHANGED
|
@@ -46,7 +46,7 @@ exports.Error = require('./error/index');
|
|
|
46
46
|
/**
|
|
47
47
|
* The Mongoose [Schema](#schema_Schema) constructor
|
|
48
48
|
*
|
|
49
|
-
* ####Example:
|
|
49
|
+
* #### Example:
|
|
50
50
|
*
|
|
51
51
|
* const mongoose = require('mongoose');
|
|
52
52
|
* const Schema = mongoose.Schema;
|
|
@@ -61,12 +61,12 @@ exports.Schema = require('./schema');
|
|
|
61
61
|
/**
|
|
62
62
|
* The various Mongoose Types.
|
|
63
63
|
*
|
|
64
|
-
* ####Example:
|
|
64
|
+
* #### Example:
|
|
65
65
|
*
|
|
66
66
|
* const mongoose = require('mongoose');
|
|
67
67
|
* const array = mongoose.Types.Array;
|
|
68
68
|
*
|
|
69
|
-
* ####Types:
|
|
69
|
+
* #### Types:
|
|
70
70
|
*
|
|
71
71
|
* - [Array](/docs/schematypes.html#arrays)
|
|
72
72
|
* - [Buffer](/docs/schematypes.html#buffers)
|
|
@@ -98,7 +98,7 @@ exports.VirtualType = require('./virtualtype');
|
|
|
98
98
|
/**
|
|
99
99
|
* The various Mongoose SchemaTypes.
|
|
100
100
|
*
|
|
101
|
-
* ####Note:
|
|
101
|
+
* #### Note:
|
|
102
102
|
*
|
|
103
103
|
* _Alias of mongoose.Schema.Types for backwards compatibility._
|
|
104
104
|
*
|
package/lib/connection.js
CHANGED
|
@@ -92,7 +92,7 @@ Connection.prototype.__proto__ = EventEmitter.prototype;
|
|
|
92
92
|
*
|
|
93
93
|
* Each state change emits its associated event name.
|
|
94
94
|
*
|
|
95
|
-
* ####Example
|
|
95
|
+
* #### Example
|
|
96
96
|
*
|
|
97
97
|
* conn.on('connected', callback);
|
|
98
98
|
* conn.on('disconnected', callback);
|
|
@@ -131,7 +131,7 @@ Object.defineProperty(Connection.prototype, 'readyState', {
|
|
|
131
131
|
/**
|
|
132
132
|
* Gets the value of the option `key`. Equivalent to `conn.options[key]`
|
|
133
133
|
*
|
|
134
|
-
* ####Example:
|
|
134
|
+
* #### Example:
|
|
135
135
|
*
|
|
136
136
|
* conn.get('test'); // returns the 'test' value
|
|
137
137
|
*
|
|
@@ -155,7 +155,7 @@ Connection.prototype.get = function(key) {
|
|
|
155
155
|
*
|
|
156
156
|
* - `maxTimeMS`: Set [`maxTimeMS`](/docs/api.html#query_Query-maxTimeMS) for all queries on this connection.
|
|
157
157
|
*
|
|
158
|
-
* ####Example:
|
|
158
|
+
* #### Example:
|
|
159
159
|
*
|
|
160
160
|
* conn.set('test', 'foo');
|
|
161
161
|
* conn.get('test'); // 'foo'
|
|
@@ -192,7 +192,7 @@ Connection.prototype.collections;
|
|
|
192
192
|
/**
|
|
193
193
|
* The name of the database this connection points to.
|
|
194
194
|
*
|
|
195
|
-
* ####Example
|
|
195
|
+
* #### Example
|
|
196
196
|
*
|
|
197
197
|
* mongoose.createConnection('mongodb://localhost:27017/mydb').name; // "mydb"
|
|
198
198
|
*
|
|
@@ -209,7 +209,7 @@ Connection.prototype.name;
|
|
|
209
209
|
* a map from model names to models. Contains all models that have been
|
|
210
210
|
* added to this connection using [`Connection#model()`](/docs/api/connection.html#connection_Connection-model).
|
|
211
211
|
*
|
|
212
|
-
* ####Example
|
|
212
|
+
* #### Example
|
|
213
213
|
*
|
|
214
214
|
* const conn = mongoose.createConnection();
|
|
215
215
|
* const Test = conn.model('Test', mongoose.Schema({ name: String }));
|
|
@@ -229,7 +229,7 @@ Connection.prototype.models;
|
|
|
229
229
|
* A number identifier for this connection. Used for debugging when
|
|
230
230
|
* you have [multiple connections](/docs/connections.html#multiple_connections).
|
|
231
231
|
*
|
|
232
|
-
* ####Example
|
|
232
|
+
* #### Example
|
|
233
233
|
*
|
|
234
234
|
* // The default connection has `id = 0`
|
|
235
235
|
* mongoose.connection.id; // 0
|
|
@@ -249,7 +249,7 @@ Connection.prototype.id;
|
|
|
249
249
|
/**
|
|
250
250
|
* The plugins that will be applied to all models created on this connection.
|
|
251
251
|
*
|
|
252
|
-
* ####Example:
|
|
252
|
+
* #### Example:
|
|
253
253
|
*
|
|
254
254
|
* const db = mongoose.createConnection('mongodb://localhost:27017/mydb');
|
|
255
255
|
* db.plugin(() => console.log('Applied'));
|
|
@@ -273,7 +273,7 @@ Object.defineProperty(Connection.prototype, 'plugins', {
|
|
|
273
273
|
* The host name portion of the URI. If multiple hosts, such as a replica set,
|
|
274
274
|
* this will contain the first host name in the URI
|
|
275
275
|
*
|
|
276
|
-
* ####Example
|
|
276
|
+
* #### Example
|
|
277
277
|
*
|
|
278
278
|
* mongoose.createConnection('mongodb://localhost:27017/mydb').host; // "localhost"
|
|
279
279
|
*
|
|
@@ -293,7 +293,7 @@ Object.defineProperty(Connection.prototype, 'host', {
|
|
|
293
293
|
* The port portion of the URI. If multiple hosts, such as a replica set,
|
|
294
294
|
* this will contain the port from the first host name in the URI.
|
|
295
295
|
*
|
|
296
|
-
* ####Example
|
|
296
|
+
* #### Example
|
|
297
297
|
*
|
|
298
298
|
* mongoose.createConnection('mongodb://localhost:27017/mydb').port; // 27017
|
|
299
299
|
*
|
|
@@ -312,7 +312,7 @@ Object.defineProperty(Connection.prototype, 'port', {
|
|
|
312
312
|
/**
|
|
313
313
|
* The username specified in the URI
|
|
314
314
|
*
|
|
315
|
-
* ####Example
|
|
315
|
+
* #### Example
|
|
316
316
|
*
|
|
317
317
|
* mongoose.createConnection('mongodb://val:psw@localhost:27017/mydb').user; // "val"
|
|
318
318
|
*
|
|
@@ -331,7 +331,7 @@ Object.defineProperty(Connection.prototype, 'user', {
|
|
|
331
331
|
/**
|
|
332
332
|
* The password specified in the URI
|
|
333
333
|
*
|
|
334
|
-
* ####Example
|
|
334
|
+
* #### Example
|
|
335
335
|
*
|
|
336
336
|
* mongoose.createConnection('mongodb://val:psw@localhost:27017/mydb').pass; // "psw"
|
|
337
337
|
*
|
|
@@ -409,7 +409,7 @@ Connection.prototype.createCollection = _wrapConnHelper(function createCollectio
|
|
|
409
409
|
* for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
|
|
410
410
|
* and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
411
411
|
*
|
|
412
|
-
* ####Example:
|
|
412
|
+
* #### Example:
|
|
413
413
|
*
|
|
414
414
|
* const session = await conn.startSession();
|
|
415
415
|
* let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
|
|
@@ -447,7 +447,7 @@ Connection.prototype.startSession = _wrapConnHelper(function startSession(option
|
|
|
447
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
|
-
* ####Example:
|
|
450
|
+
* #### Example:
|
|
451
451
|
*
|
|
452
452
|
* const doc = new Person({ name: 'Will Riker' });
|
|
453
453
|
* await db.transaction(async function setRank(session) {
|
|
@@ -532,7 +532,7 @@ Connection.prototype.dropCollection = _wrapConnHelper(function dropCollection(co
|
|
|
532
532
|
* Helper for `dropDatabase()`. Deletes the given database, including all
|
|
533
533
|
* collections, documents, and indexes.
|
|
534
534
|
*
|
|
535
|
-
* ####Example:
|
|
535
|
+
* #### Example:
|
|
536
536
|
*
|
|
537
537
|
* const conn = mongoose.createConnection('mongodb://localhost:27017/mydb');
|
|
538
538
|
* // Deletes the entire 'mydb' database
|
|
@@ -1037,7 +1037,7 @@ Connection.prototype.collection = function(name, options) {
|
|
|
1037
1037
|
*
|
|
1038
1038
|
* Equivalent to calling `.plugin(fn)` on each schema you create.
|
|
1039
1039
|
*
|
|
1040
|
-
* ####Example:
|
|
1040
|
+
* #### Example:
|
|
1041
1041
|
* const db = mongoose.createConnection('mongodb://localhost:27017/mydb');
|
|
1042
1042
|
* db.plugin(() => console.log('Applied'));
|
|
1043
1043
|
* db.plugins.length; // 1
|
|
@@ -1067,7 +1067,7 @@ Connection.prototype.plugin = function(fn, opts) {
|
|
|
1067
1067
|
*
|
|
1068
1068
|
* _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._
|
|
1069
1069
|
*
|
|
1070
|
-
* ####Example:
|
|
1070
|
+
* #### Example:
|
|
1071
1071
|
*
|
|
1072
1072
|
* const schema = new Schema({ name: String }, { collection: 'actor' });
|
|
1073
1073
|
*
|
|
@@ -1185,7 +1185,7 @@ Connection.prototype.model = function(name, schema, collection, options) {
|
|
|
1185
1185
|
* use this function to clean up any models you created in your tests to
|
|
1186
1186
|
* prevent OverwriteModelErrors.
|
|
1187
1187
|
*
|
|
1188
|
-
* ####Example:
|
|
1188
|
+
* #### Example:
|
|
1189
1189
|
*
|
|
1190
1190
|
* conn.model('User', new Schema({ name: String }));
|
|
1191
1191
|
* console.log(conn.model('User')); // Model object
|
|
@@ -1243,7 +1243,7 @@ Connection.prototype.deleteModel = function(name) {
|
|
|
1243
1243
|
* - 'end': Emitted if the underlying stream is closed
|
|
1244
1244
|
* - 'close': Emitted if the underlying stream is closed
|
|
1245
1245
|
*
|
|
1246
|
-
* ####Example:
|
|
1246
|
+
* #### Example:
|
|
1247
1247
|
*
|
|
1248
1248
|
* const User = conn.model('User', new Schema({ name: String }));
|
|
1249
1249
|
*
|
|
@@ -1287,7 +1287,7 @@ Connection.prototype.watch = function(pipeline, options) {
|
|
|
1287
1287
|
* successfully connects to MongoDB, or rejects if this connection failed
|
|
1288
1288
|
* to connect.
|
|
1289
1289
|
*
|
|
1290
|
-
* ####Example:
|
|
1290
|
+
* #### Example:
|
|
1291
1291
|
* const conn = await mongoose.createConnection('mongodb://localhost:27017/test').
|
|
1292
1292
|
* asPromise();
|
|
1293
1293
|
* conn.readyState; // 1, means Mongoose is connected
|
|
@@ -1356,7 +1356,7 @@ Connection.prototype.optionsProvideAuthenticationData = function(options) {
|
|
|
1356
1356
|
* Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html) instance
|
|
1357
1357
|
* that this connection uses to talk to MongoDB.
|
|
1358
1358
|
*
|
|
1359
|
-
* ####Example:
|
|
1359
|
+
* #### Example:
|
|
1360
1360
|
* const conn = await mongoose.createConnection('mongodb://localhost:27017/test');
|
|
1361
1361
|
*
|
|
1362
1362
|
* conn.getClient(); // MongoClient { ... }
|
|
@@ -1374,7 +1374,7 @@ Connection.prototype.getClient = function getClient() {
|
|
|
1374
1374
|
* that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to
|
|
1375
1375
|
* reuse it.
|
|
1376
1376
|
*
|
|
1377
|
-
* ####Example:
|
|
1377
|
+
* #### Example:
|
|
1378
1378
|
* const client = await mongodb.MongoClient.connect('mongodb://localhost:27017/test');
|
|
1379
1379
|
*
|
|
1380
1380
|
* const conn = mongoose.createConnection().setClient(client);
|
|
@@ -107,7 +107,7 @@ if (Symbol.asyncIterator != null) {
|
|
|
107
107
|
* Registers a transform function which subsequently maps documents retrieved
|
|
108
108
|
* via the streams interface or `.next()`
|
|
109
109
|
*
|
|
110
|
-
* ####Example
|
|
110
|
+
* #### Example
|
|
111
111
|
*
|
|
112
112
|
* // Map documents returned by `data` events
|
|
113
113
|
* Thing.
|
|
@@ -226,7 +226,7 @@ AggregationCursor.prototype.eachAsync = function(fn, opts, callback) {
|
|
|
226
226
|
* You do not need to call this function explicitly, the JavaScript runtime
|
|
227
227
|
* will call it for you.
|
|
228
228
|
*
|
|
229
|
-
* ####Example
|
|
229
|
+
* #### Example
|
|
230
230
|
*
|
|
231
231
|
* // Async iterator without explicitly calling `cursor()`. Mongoose still
|
|
232
232
|
* // creates an AggregationCursor instance internally.
|