map_reduce.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MapReduceOperation = void 0;
  4. const bson_1 = require("../bson");
  5. const error_1 = require("../error");
  6. const read_preference_1 = require("../read_preference");
  7. const utils_1 = require("../utils");
  8. const command_1 = require("./command");
  9. const operation_1 = require("./operation");
  10. const exclusionList = [
  11. 'explain',
  12. 'readPreference',
  13. 'readConcern',
  14. 'session',
  15. 'bypassDocumentValidation',
  16. 'writeConcern',
  17. 'raw',
  18. 'fieldsAsRaw',
  19. 'promoteLongs',
  20. 'promoteValues',
  21. 'promoteBuffers',
  22. 'bsonRegExp',
  23. 'serializeFunctions',
  24. 'ignoreUndefined',
  25. 'enableUtf8Validation',
  26. 'scope' // this option is reformatted thus exclude the original
  27. ];
  28. /**
  29. * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
  30. * @internal
  31. */
  32. class MapReduceOperation extends command_1.CommandOperation {
  33. /**
  34. * Constructs a MapReduce operation.
  35. *
  36. * @param collection - Collection instance.
  37. * @param map - The mapping function.
  38. * @param reduce - The reduce function.
  39. * @param options - Optional settings. See Collection.prototype.mapReduce for a list of options.
  40. */
  41. constructor(collection, map, reduce, options) {
  42. super(collection, options);
  43. this.options = options !== null && options !== void 0 ? options : {};
  44. this.collection = collection;
  45. this.map = map;
  46. this.reduce = reduce;
  47. }
  48. execute(server, session, callback) {
  49. const coll = this.collection;
  50. const map = this.map;
  51. const reduce = this.reduce;
  52. let options = this.options;
  53. const mapCommandHash = {
  54. mapReduce: coll.collectionName,
  55. map: map,
  56. reduce: reduce
  57. };
  58. if (options.scope) {
  59. mapCommandHash.scope = processScope(options.scope);
  60. }
  61. // Add any other options passed in
  62. for (const n in options) {
  63. // Only include if not in exclusion list
  64. if (exclusionList.indexOf(n) === -1) {
  65. mapCommandHash[n] = options[n];
  66. }
  67. }
  68. options = Object.assign({}, options);
  69. // If we have a read preference and inline is not set as output fail hard
  70. if (this.readPreference.mode === read_preference_1.ReadPreferenceMode.primary &&
  71. options.out &&
  72. options.out.inline !== 1 &&
  73. options.out !== 'inline') {
  74. // Force readPreference to primary
  75. options.readPreference = read_preference_1.ReadPreference.primary;
  76. // Decorate command with writeConcern if supported
  77. (0, utils_1.applyWriteConcern)(mapCommandHash, { db: coll.s.db, collection: coll }, options);
  78. }
  79. else {
  80. (0, utils_1.decorateWithReadConcern)(mapCommandHash, coll, options);
  81. }
  82. // Is bypassDocumentValidation specified
  83. if (options.bypassDocumentValidation === true) {
  84. mapCommandHash.bypassDocumentValidation = options.bypassDocumentValidation;
  85. }
  86. // Have we specified collation
  87. try {
  88. (0, utils_1.decorateWithCollation)(mapCommandHash, coll, options);
  89. }
  90. catch (err) {
  91. return callback(err);
  92. }
  93. if (this.explain && (0, utils_1.maxWireVersion)(server) < 9) {
  94. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on mapReduce`));
  95. return;
  96. }
  97. // Execute command
  98. super.executeCommand(server, session, mapCommandHash, (err, result) => {
  99. if (err)
  100. return callback(err);
  101. // Check if we have an error
  102. if (1 !== result.ok || result.err || result.errmsg) {
  103. return callback(new error_1.MongoServerError(result));
  104. }
  105. // If an explain option was executed, don't process the server results
  106. if (this.explain)
  107. return callback(undefined, result);
  108. // Create statistics value
  109. const stats = {};
  110. if (result.timeMillis)
  111. stats['processtime'] = result.timeMillis;
  112. if (result.counts)
  113. stats['counts'] = result.counts;
  114. if (result.timing)
  115. stats['timing'] = result.timing;
  116. // invoked with inline?
  117. if (result.results) {
  118. // If we wish for no verbosity
  119. if (options['verbose'] == null || !options['verbose']) {
  120. return callback(undefined, result.results);
  121. }
  122. return callback(undefined, { results: result.results, stats: stats });
  123. }
  124. // The returned collection
  125. let collection = null;
  126. // If we have an object it's a different db
  127. if (result.result != null && typeof result.result === 'object') {
  128. const doc = result.result;
  129. // Return a collection from another db
  130. collection = coll.s.db.s.client.db(doc.db, coll.s.db.s.options).collection(doc.collection);
  131. }
  132. else {
  133. // Create a collection object that wraps the result collection
  134. collection = coll.s.db.collection(result.result);
  135. }
  136. // If we wish for no verbosity
  137. if (options['verbose'] == null || !options['verbose']) {
  138. return callback(err, collection);
  139. }
  140. // Return stats as third set of values
  141. callback(err, { collection, stats });
  142. });
  143. }
  144. }
  145. exports.MapReduceOperation = MapReduceOperation;
  146. /** Functions that are passed as scope args must be converted to Code instances. */
  147. function processScope(scope) {
  148. if (!(0, utils_1.isObject)(scope) || scope._bsontype === 'ObjectID') {
  149. return scope;
  150. }
  151. const newScope = {};
  152. for (const key of Object.keys(scope)) {
  153. if ('function' === typeof scope[key]) {
  154. newScope[key] = new bson_1.Code(String(scope[key]));
  155. }
  156. else if (scope[key]._bsontype === 'Code') {
  157. newScope[key] = scope[key];
  158. }
  159. else {
  160. newScope[key] = processScope(scope[key]);
  161. }
  162. }
  163. return newScope;
  164. }
  165. (0, operation_1.defineAspects)(MapReduceOperation, [operation_1.Aspect.EXPLAINABLE]);
  166. //# sourceMappingURL=map_reduce.js.map