Run a Map-Reduce job
Write map, reduce and finalize functions, preview each step on a sample, then execute the job inline or into a collection.
Map-Reduce runs a server-side JavaScript job on a collection: map() emits key and value pairs,
reduce() combines the values of each key, and an optional finalize() shapes the result. The editor
lets you preview every step on a sample before running the job on the whole collection.
MongoDB deprecates map-reduce since version 5.0, though it still runs it. For new work, the Aggregation Editor is usually the better choice.
Run a job
- Select a collection in the connection tree.
- Press Ctrl+M, or choose Collection > Open Map-Reduce. A new tab opens with default functions; output is inline.
- Write the map() function:, for example
function () { emit(this.status, this.total); }. - Write the reduce(key, values) function:, for example
function (key, values) { return Array.sum(values); }. - Optionally tick Use filter/sort/limit and fill Query:, Sort: and Limit:, tick Use finalize for a finalize(key, reducedValue) function:, or Use global scope for Global variables, as a document:.
- Preview the steps (below), then click Execute (Run the Map-Reduce job on the whole collection). The result opens in the Finished job tab.
Preview each step
Previews never write: they run on the first 100 documents of the input (fewer if Limit says so), always with inline output.
| Tab | Preview Input | Preview Output |
|---|---|---|
| Input data | The collection’s documents, unfiltered | The documents fed to map() (filter, sort and limit applied when on) |
| map() function | Every pair emitted, with the _id of the document that emitted it (map() sample output) | |
| reduce() function | The pairs reduce() receives (reduce() sample input) | The job on the sample, without finalize (reduce() sample output) |
| finalize() function | The job on the sample, with finalize (finalize() sample output) |
Output options
| Output | What happens |
|---|---|
| Inline | The result is returned to the Finished job tab; nothing is written |
| Replace, Merge, Reduce | The result is written to Output collection: (in Output database:) |
Writing into a collection asks first (Execute Map-Reduce: The job writes its result to … Run it?) and is refused on a read-only connection. Replace needs a collection name.
Options also offers jsMode and verbose, passed to the server as they are.
Files and details
| Item | What it does |
|---|---|
| Save script | Saves the job as a JavaScript file (Save the job as a JavaScript file), runnable in IntelliShell |
| Open Map-Reduce File | Reads such a file back, or a db.coll.mapReduce(map, reduce, { … }) script; Open recent lists the last 10 |
| Show details | Execution statistics and configuration: the server’s reply without the results, and a summary of the job |
| Result tab menu | Close, Close Other Tabs, Close All Tabs |
Result tabs are read-only, with the Table, Tree and JSON views and their copy options.
Troubleshooting
| Message | Meaning |
|---|---|
| This server does not run Map-Reduce jobs: … Use the Aggregation Editor instead. | Server-side JavaScript is turned off on the server, or the server (some MongoDB-compatible services) has no map-reduce. |
Query: … | The Query field does not read; the message names the field. |
| A JavaScript error from the server | Your function has a syntax error or throws; the tab keeps your text. |