Docs Menu

Docs HomeMongoDB Manual

Map-Reduce Concurrency


Aggregation Pipeline as an Alternative to Map-Reduce

An aggregation pipeline provides better performance and usability than a map-reduce operation.

Map-reduce operations can be rewritten using aggregation pipeline operators, such as $group, $merge, and others.

For map-reduce operations that require custom functionality, MongoDB provides the $accumulator and $function aggregation operators starting in version 4.4. Use these operators to define custom aggregation expressions in JavaScript.

For examples of aggregation pipeline alternatives to map-reduce operations, see Map-Reduce to Aggregation Pipeline and Map-Reduce Examples.

The map-reduce operation is composed of many tasks, including reads from the input collection, executions of the map function, executions of the reduce function, writes to a temporary collection during processing, and writes to the output collection.

During the operation, map-reduce takes the following locks:

  • The read phase takes a read lock. It yields every 100 documents.

  • The insert into the temporary collection takes a write lock for a single write.

  • If the output collection does not exist, the creation of the output collection takes a write lock.

  • If the output collection exists, then the output actions (i.e. merge, replace, reduce) take a write lock. This write lock is global, and blocks all operations on the mongod instance.


The final write lock during post-processing makes the results appear atomically. However, output actions merge and reduce may take minutes to process. For the merge and reduce, the nonAtomic flag is available, which releases the lock between writing each output document. Starting in MongoDB 4.2, explicitly setting nonAtomic: false is deprecated. See the db.collection.mapReduce() reference for more information.

←  Map-Reduce and Sharded CollectionsMap-Reduce Examples →
Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.