Note
Aggregation Pipeline as Alternative聚合管道作为替代方案
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.您应该使用聚合管道而不是map-reduce。聚合管道提供了比map-reduce更好的性能和可用性。You can rewrite map-reduce operations using aggregation pipeline stages, such as您可以使用聚合管道阶段(如$group,$merge, and others.$group、$merge等)重写map-reduce操作。For map-reduce operations that require custom functionality, you can use the对于需要自定义功能的map-reduce操作,可以使用$accumulatorand$functionaggregation operators. You can use those operators to define custom aggregation expressions in JavaScript.$accumulator和$function聚合运算符。您可以使用这些运算符在JavaScript中定义自定义聚合表达式。
For examples of aggregation pipeline alternatives to map-reduce, see:有关map-reduce的聚合管道替代方案的示例,请参阅:
You can run aggregation pipelines in the UI for deployments hosted in MongoDB Atlas.您可以在MongoDB Atlas中托管的部署的UI中运行聚合管道。
Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. To perform map-reduce operations, MongoDB provides the Map-reduce是一种数据处理范式,用于将大量数据压缩为有用的聚合结果。为了执行map-reduce操作,MongoDB提供了mapReduce database command.mapReduce数据库命令。
Consider the following map-reduce operation:考虑以下map reduce操作:
In this map-reduce operation, MongoDB applies the map phase to each input document (i.e. the documents in the collection that match the query condition). The map function emits key-value pairs. For those keys that have multiple values, MongoDB applies the reduce phase, which collects and condenses the aggregated data. MongoDB then stores the results in a collection. Optionally, the output of the reduce function may pass through a finalize function to further condense or process the results of the aggregation.在这个map reduce操作中,MongoDB将map阶段应用于每个输入文档(即集合中与查询条件匹配的文档)。map函数发出键值对。对于那些具有多个值的键,MongoDB应用reduce阶段,该阶段集合并压缩聚合数据。然后,MongoDB将结果存储在一个集合中。可选地,reduce函数的输出可以通过finalize函数来进一步压缩或处理聚合的结果。
All map-reduce functions in MongoDB are JavaScript and run within the MongoDB中的所有map reduce函数都是JavaScript,并在mongod process. Map-reduce operations take the documents of a single collection as the input and can perform any arbitrary sorting and limiting before beginning the map stage. mapReduce can return the results of a map-reduce operation as a document, or may write the results to collections.mongod进程中运行。Map-reduce操作将单个集合的文档作为输入,可以在开始映射阶段之前执行任何任意排序和限制。mapReduce可以将map-reduce操作的结果作为文档返回,也可以将结果写入集合。
Note
Map-reduce is unsupported for MongoDB Atlas Free and Flex clusters.Map-reduce不支持MongoDB Atlas Free和Flex集群。
Map-Reduce JavaScript FunctionsMap Reduce JavaScript函数
In MongoDB, map-reduce operations use custom JavaScript functions to map, or associate, values to a key. If a key has multiple values mapped to it, the operation reduces the values for the key to a single object.在MongoDB中,map-reduce操作使用自定义JavaScript函数将值映射或关联到键。如果一个键映射了多个值,则该操作会将键的值减少到单个对象。
The use of custom JavaScript functions provide flexibility to map-reduce operations. For instance, when processing a document, the map function can create more than one key and value mapping or no mapping. Map-reduce operations can also use a custom JavaScript function to make final modifications to the results at the end of the map and reduce operation, such as perform additional calculations.自定义JavaScript函数的使用为映射减少操作提供了灵活性。例如,在处理文档时,map函数可以创建多个键和值映射,也可以不创建映射。Map-reduce操作还可以使用自定义JavaScript函数对映射和reduce操作末尾的结果进行最终修改,例如执行其他计算。
Map-Reduce Results结果
In MongoDB, the map-reduce operation can write results to a collection or return the results inline. If you write map-reduce output to a collection, you can perform subsequent map-reduce operations on the same input collection that merge replace, merge, or reduce new results with previous results. 在MongoDB中,map reduce操作可以将结果写入集合或内联返回结果。如果将map-reduce输出写入集合,则可以对同一输入集合执行后续的map-reducer操作,将新结果与以前的结果合并、替换、合并或缩减。See 有关详细信息和示例,请参阅mapReduce and Perform Incremental Map-Reduce for details and examples.mapReduce和执行增量Map-Reduce。
When returning the results of a map-reduce operation inline, the result documents must be within the BSON Document Size limit, which is currently 16 mebibytes. For additional information on limits and restrictions on map-reduce operations, see the mapReduce (database command) reference page.内联返回map-reduce操作的结果时,结果文档必须在BSON文档大小限制范围内,目前为16兆字节。有关map-reduce操作的限制和约束的更多信息,请参阅mapReduce(数据库命令)参考页面。
Sharded Collections分片化集合
MongoDB supports map-reduce operations on sharded collections.MongoDB支持对分片集合进行map reduce操作。
Views视图
Views do not support map-reduce operations.视图不支持map-reduce操作。