explain.js 1.8 KB

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