explain.js
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
const MongoError = require('./core/error').MongoError;
const ExplainVerbosity = {
queryPlanner: 'queryPlanner',
queryPlannerExtended: 'queryPlannerExtended',
executionStats: 'executionStats',
allPlansExecution: 'allPlansExecution'
};
/**
* @class
* @property {'queryPlanner'|'queryPlannerExtended'|'executionStats'|'allPlansExecution'} verbosity The verbosity mode for the explain output.
*/
class Explain {
/**
* Constructs an Explain from the explain verbosity.
*
* For backwards compatibility, true is interpreted as "allPlansExecution"
* and false as "queryPlanner". Prior to server version 3.6, aggregate()
* ignores the verbosity parameter and executes in "queryPlanner".
*
* @param {'queryPlanner'|'queryPlannerExtended'|'executionStats'|'allPlansExecution'|boolean} [verbosity] The verbosity mode for the explain output.
*/
constructor(verbosity) {
if (typeof verbosity === 'boolean') {
this.verbosity = verbosity ? 'allPlansExecution' : 'queryPlanner';
} else {
this.verbosity = verbosity;
}
}
/**
* Construct an Explain given an options object.
*
* @param {object} [options] The options object from which to extract the explain.
* @param {'queryPlanner'|'queryPlannerExtended'|'executionStats'|'allPlansExecution'|boolean} [options.explain] The verbosity mode for the explain output
* @return {Explain}
*/
static fromOptions(options) {
if (options == null || options.explain === undefined) {
return;
}
const explain = options.explain;
if (typeof explain === 'boolean' || explain in ExplainVerbosity) {
return new Explain(options.explain);
}
throw new MongoError(`explain must be one of ${Object.keys(ExplainVerbosity)} or a boolean`);
}
}
module.exports = { Explain };