Model.mapReduce()

Parameters
  • o «Object» an object specifying map-reduce options

  • [callback] «Function» optional callback

Returns:
  • «Promise»

Executes a mapReduce command.

o is an object specifying all mapReduce options as well as the map and reduce functions. All options are delegated to the driver implementation. See node-mongodb-native mapReduce() documentation for more detail about options.

This function does not trigger any middleware.

Example:

  1. const o = {};
  2. // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
  3. // these functions are converted to strings
  4. o.map = function () { emit(this.name, 1) };
  5. o.reduce = function (k, vals) { return vals.length };
  6. User.mapReduce(o, function (err, results) {
  7. console.log(results)
  8. })

Other options:

  • query {Object} query filter object.
  • sort {Object} sort input objects using this key
  • limit {Number} max number of documents
  • keeptemp {Boolean, default:false} keep temporary data
  • finalize {Function} finalize function
  • scope {Object} scope variables exposed to map/reduce/finalize during execution
  • jsMode {Boolean, default:false} it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X
  • verbose {Boolean, default:false} provide statistics on job execution time.
  • readPreference {String}
  • out* {Object, default: {inline:1}} sets the output target for the map reduce job.

* out options:

  • {inline:1} the results are returned in an array
  • {replace: 'collectionName'} add the results to collectionName: the results replace the collection
  • {reduce: 'collectionName'} add the results to collectionName: if dups are detected, uses the reducer / finalize functions
  • {merge: 'collectionName'} add the results to collectionName: if dups exist the new docs overwrite the old

If options.out is set to replace, merge, or reduce, a Model instance is returned that can be used for further querying. Queries run against this model are all executed with the lean option; meaning only the js object is returned and no Mongoose magic is applied (getters, setters, etc).

Example:

  1. const o = {};
  2. // You can also define `map()` and `reduce()` as strings if your
  3. // linter complains about `emit()` not being defined
  4. o.map = 'function () { emit(this.name, 1) }';
  5. o.reduce = 'function (k, vals) { return vals.length }';
  6. o.out = { replace: 'createdCollectionNameForResults' }
  7. o.verbose = true;
  8. User.mapReduce(o, function (err, model, stats) {
  9. console.log('map reduce took %d ms', stats.processtime)
  10. model.find().where('value').gt(10).exec(function (err, docs) {
  11. console.log(docs);
  12. });
  13. })
  14. // `mapReduce()` returns a promise. However, ES6 promises can only
  15. // resolve to exactly one value,
  16. o.resolveToObject = true;
  17. const promise = User.mapReduce(o);
  18. promise.then(function (res) {
  19. const model = res.model;
  20. const stats = res.stats;
  21. console.log('map reduce took %d ms', stats.processtime)
  22. return model.find().where('value').gt(10).exec();
  23. }).then(function (docs) {
  24. console.log(docs);
  25. }).then(null, handleError).end()