Map-Reduce
On this page本页内容
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.您应该使用聚合管道,而不是map-reduce
。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.$group
、$merge
和其他阶段)重写映射减少操作。For map-reduce operations that require custom functionality, you can use the对于需要自定义功能的map reduce操作,可以使用$accumulator
and$function
aggregation operators, available starting in version 4.4.$accumulator
和$function
聚合运算符,这些运算符从4.4版开始提供。You can use those operators to define custom aggregation expressions in JavaScript.您可以使用这些运算符在JavaScript中定义自定义聚合表达式。
For examples of aggregation pipeline alternatives to map-reduce, see:有关映射减少的聚合管道替代方案的示例,请参阅:
Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. Map reduce是一种数据处理范式,用于将大量数据浓缩为有用的聚合结果。To perform map-reduce operations, MongoDB provides the 为了执行mapReduce
database command.map-reduce
操作,MongoDB提供了mapReduce
数据库命令。
Consider the following map-reduce operation:考虑以下映射缩减操作:
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). 在这个map-reduce操作中,MongoDB将map阶段应用于每个输入文档(即集合中与查询条件匹配的文档)。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. map函数会发出键值对。对于那些具有多个值的键,MongoDB应用reduce阶段,该阶段集合并浓缩聚合的数据。MongoDB then stores the results in a collection. 然后MongoDB将结果存储在一个集合中。Optionally, the output of the reduce function may pass through a finalize function to further condense or process the results of the aggregation.可选地,reduce函数的输出可以通过finalize函数以进一步浓缩或处理聚合的结果。
All map-reduce functions in MongoDB are JavaScript and run within the MongoDB中的所有map reduce函数都是JavaScript,并在mongod
process. mongod
进程中运行。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. Map-reduce操作将单个集合的文档作为输入,并且可以在开始Map阶段之前执行任意排序和限制。mapReduce可以将mapReduce
can return the results of a map-reduce operation as a document, or may write the results to collections.mapReduce
操作的结果作为文档返回,也可以将结果写入集合。
Map-Reduce JavaScript FunctionsMap Reduce JavaScript函数
In MongoDB, map-reduce operations use custom JavaScript functions to map, or associate, values to a key. 在MongoDB中,map reduce操作使用自定义JavaScript函数将值映射或关联到键。If a key has multiple values mapped to it, the operation reduces the values for the key to a single object.如果一个键映射了多个值,则该操作会将该键的值减少为单个对象。
The use of custom JavaScript functions provide flexibility to map-reduce operations. 自定义JavaScript函数的使用为映射reduce操作提供了灵活性。For instance, when processing a document, the map function can create more than one key and value mapping or no mapping. 例如,在处理文档时,map函数可以创建多个键和值映射,也可以不创建映射。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.Map reduce操作还可以使用自定义JavaScript函数对Map和reduce运算末尾的结果进行最终修改,例如执行额外的计算。
Starting in MongoDB 4.4, 从MongoDB 4.4开始,mapReduce
no longer supports the deprecated BSON type JavaScript code with scope (BSON Type 15) for its functions. mapReduce
不再支持不推荐使用的BSON类型的JavaScript代码,其函数具有作用域(BSON type 15)。The map
, reduce
, and finalize
functions must be either BSON type String (BSON Type 2) or BSON type JavaScript (BSON Type 13). map
、reduce
和finalize
函数必须是BSON类型String(BSON类型2)或BSON类型JavaScript(BSON type 13)。To pass constant values which will be accessible in the 要传递map
, reduce
, and finalize
functions, use the scope
parameter.map
、reduce
和finalize
函数中可访问的常数值,请使用scope
参数。
The use of JavaScript code with scope for the 自4.2.1版本以来,一直不赞成使用具有mapReduce
functions has been deprecated since version 4.2.1.mapReduce
函数作用域的JavaScript代码。
Map-Reduce Results后果
In MongoDB, the map-reduce operation can write results to a collection or return the results inline. 在MongoDB中,map reduce操作可以将结果写入集合或内联返回结果。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. 如果将映射减少输出写入集合,则可以对同一输入集合执行后续映射减少操作,以将新结果与以前的结果合并、替换、合并或减少。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 megabytes. 内联返回map-reduce操作的结果时,结果文档必须在BSON文档大小限制内,该限制当前为16兆字节。For additional information on limits and restrictions on map-reduce operations, see the mapReduce reference page.有关map reduce操作的限制和限制的其他信息,请参阅mapReduce参考页。
Sharded Collections分片集合
MongoDB supports map-reduce operations on sharded collections.MongoDB支持对分片集合的map-reduce操作。
However, starting in version 4.2, MongoDB deprecates the map-reduce option to create a new sharded collection and the use of the 然而,从4.2版本开始,MongoDB反对使用map-reduce选项来创建新的分片集合,并反对使用sharded
option for map-reduce. sharded
选项来减少map。To output to a sharded collection, create the sharded collection first. 若要输出到分片集合,请首先创建分片集合。MongoDB 4.2 also deprecates the replacement of an existing sharded collection.MongoDB 4.2也反对替换现有的分片集合。
Views视图
Views do not support map-reduce operations.视图不支持map-reduce操作。