Docs HomeMongoDB Manual

Map-Reduce Concurrency并发

Note

Aggregation Pipeline as an Alternative to Map-Reduce聚合管道作为Map Reduce的替代方案

Starting in MongoDB 5.0, map-reduce is deprecated:从MongoDB 5.0开始,不赞成使用map-reduce:

  • Instead of map-reduce, you should use an aggregation pipeline. Aggregation pipelines provide better performance and usability than map-reduce.
  • You can rewrite map-reduce operations using aggregation pipeline stages, such as $group, $merge, and others.
  • For map-reduce operations that require custom functionality, you can use the $accumulator and $function aggregation operators, available starting in version 4.4. You can use those operators to define custom aggregation expressions in JavaScript.

For examples of aggregation pipeline alternatives to map-reduce, see:有关映射减少的聚合管道替代方案的示例,请参阅:

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.map reduce操作由许多任务组成,包括从输入集合读取、执行map函数、执行reduce函数、在处理期间写入临时集合以及写入输出集合。

During the operation, map-reduce takes the following locks:在操作过程中,map reduce将执行以下锁定:

  • The read phase takes a read lock. It yields every 100 documents.读取阶段采用读取锁定。它每100个文档就产生一个。
  • 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. 如果输出集合存在,那么输出操作(如mergereplacereduce)将执行写锁定。This write lock is global, and blocks all operations on the mongod instance.这个写锁是全局的,并阻止mongod实例上的所有操作。
Note

The final write lock during post-processing makes the results appear atomically. 后处理过程中的最终写入锁定使结果以原子形式显示。However, output actions merge and reduce may take minutes to process. 但是,mergereduce输出操作可能需要几分钟的处理时间。For the merge and reduce, the nonAtomic flag is available, which releases the lock between writing each output document. 对于mergereducenonAtomic标志是可用的,它释放了写入每个输出文档之间的锁定。Starting in MongoDB 4.2, explicitly setting nonAtomic: false is deprecated. 从MongoDB 4.2开始,不赞成显式设置nonAtomic: falseSee the db.collection.mapReduce() reference for more information.有关详细信息,请参阅db.collection.mapReduce()参考。