Skip to content

Commit fd1abea

Browse files
committed
fix(aggregate): allow passing verbosity to Aggregate.prototype.explain()
Fix #11144
1 parent 2d6bbf9 commit fd1abea

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,7 @@ declare module 'mongoose' {
28172817

28182818
/** Execute the aggregation with explain */
28192819
explain(callback?: Callback): Promise<any>;
2820+
explain(verbosity?: string, callback?: Callback): Promise<any>;
28202821

28212822
/** Combines multiple aggregation pipelines. */
28222823
facet(options: PipelineStage.Facet['$facet']): this;

lib/aggregate.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,17 @@ Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
710710
*
711711
* Model.aggregate(..).explain(callback)
712712
*
713+
* @param {String} verbosity
713714
* @param {Function} callback
714715
* @return {Promise}
715716
*/
716717

717-
Aggregate.prototype.explain = function(callback) {
718+
Aggregate.prototype.explain = function(verbosity, callback) {
718719
const model = this._model;
720+
if (typeof verbosity === 'function') {
721+
callback = verbosity;
722+
verbosity = null;
723+
}
719724

720725
return promiseOrCallback(callback, cb => {
721726
if (!this._pipeline.length) {
@@ -733,24 +738,34 @@ Aggregate.prototype.explain = function(callback) {
733738
});
734739
}
735740

736-
this.options.explain = true;
737-
738741
model.collection.aggregate(this._pipeline, this.options, (error, cursor) => {
739742
if (error != null) {
740743
const _opts = { error: error };
741744
return model.hooks.execPost('aggregate', this, [null], _opts, error => {
742745
cb(error);
743746
});
744747
}
745-
cursor.explain((error, result) => {
746-
const _opts = { error: error };
747-
return model.hooks.execPost('aggregate', this, [result], _opts, error => {
748-
if (error) {
749-
return cb(error);
750-
}
751-
return cb(null, result);
748+
if (verbosity != null) {
749+
cursor.explain(verbosity, (error, result) => {
750+
const _opts = { error: error };
751+
return model.hooks.execPost('aggregate', this, [result], _opts, error => {
752+
if (error) {
753+
return cb(error);
754+
}
755+
return cb(null, result);
756+
});
752757
});
753-
});
758+
} else {
759+
cursor.explain((error, result) => {
760+
const _opts = { error: error };
761+
return model.hooks.execPost('aggregate', this, [result], _opts, error => {
762+
if (error) {
763+
return cb(error);
764+
}
765+
return cb(null, result);
766+
});
767+
});
768+
}
754769
});
755770
});
756771
}, model.events);

0 commit comments

Comments
 (0)