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

  1. Select a collection in the connection tree.
  2. Press Ctrl+M, or choose Collection > Open Map-Reduce. A new tab opens with default functions; output is inline.
  3. Write the map() function:, for example function () { emit(this.status, this.total); }.
  4. Write the reduce(key, values) function:, for example function (key, values) { return Array.sum(values); }.
  5. 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:.
  6. 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.

TabPreview InputPreview Output
Input dataThe collection’s documents, unfilteredThe documents fed to map() (filter, sort and limit applied when on)
map() functionEvery pair emitted, with the _id of the document that emitted it (map() sample output)
reduce() functionThe pairs reduce() receives (reduce() sample input)The job on the sample, without finalize (reduce() sample output)
finalize() functionThe job on the sample, with finalize (finalize() sample output)

Output options

OutputWhat happens
InlineThe result is returned to the Finished job tab; nothing is written
Replace, Merge, ReduceThe 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

ItemWhat it does
Save scriptSaves the job as a JavaScript file (Save the job as a JavaScript file), runnable in IntelliShell
Open Map-Reduce FileReads such a file back, or a db.coll.mapReduce(map, reduce, { … }) script; Open recent lists the last 10
Show detailsExecution statistics and configuration: the server’s reply without the results, and a summary of the job
Result tab menuClose, Close Other Tabs, Close All Tabs

Result tabs are read-only, with the Table, Tree and JSON views and their copy options.

Troubleshooting

MessageMeaning
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 serverYour function has a syntax error or throws; the tab keeps your text.