mongodb map reduce 原型

Map-reduce is a data processing paradigm for condensing large volumesof data into useful aggregated results. For map-reduce operations,MongoDB provides the mapReduce database command.

Consider the following map-reduce operation:

Diagram of the annotated map-reduce operation.

Diagram of the annotated map-reduce operation.

In this map-reduce operation, MongoDB applies the map phase to eachinput document (i.e. the documents in the collection that match thequery condition). The map function emits key-value pairs. For thosekeys that have multiple values, MongoDB applies the reduce phase, whichcollects and condenses the aggregated data. MongoDB then stores the resultsin a collection. Optionally, the output of the reduce function maypass through a finalize function to further condense or process theresults of the aggregation.

All map-reduce functions in MongoDB are JavaScript and runwithin the mongod process. Map-reduce operations take thedocuments of a single collection as the input and can performany arbitrary sorting and limiting before beginning the map stage.mapReduce can return the results of a map-reduce operationas a document, or may write the results to collections. The input andthe output collections may be sharded.

Note

For most aggregation operations, theAggregation Pipeline provides better performance andmore coherent interface. However, map-reduce operations providesome flexibility that is not presently available in the aggregationpipeline.

Map-Reduce JavaScript Functions

In MongoDB, map-reduce operations use custom JavaScript functions tomap, or associate, values to a key. If a key has multiple valuesmapped to it, the operation reduces the values for the key to asingle object.

The use of custom JavaScript functions provide flexibility tomap-reduce operations. For instance, when processing a document, themap function can create more than one key and value mapping or nomapping. Map-reduce operations can also use a custom JavaScriptfunction to make final modifications to the results at the end of themap and reduce operation, such as perform additional calculations.

Map-Reduce Behavior

In MongoDB, the map-reduce operation can write results to a collectionor return the results inline. If you write map-reduce output to acollection, you can perform subsequent map-reduce operations on thesame input collection that merge replace, merge, or reduce new resultswith previous results. See mapReduce andPerform Incremental Map-Reduce for details andexamples.

When returning the results of a map reduce operation inline, theresult documents must be within the BSON Document Size limit,which is currently 16 megabytes. For additional information on limitsand restrictions on map-reduce operations, see themapReduce reference page.

MongoDB supports map-reduce operations on sharded collections. Map-reduce operations can also outputthe results to a sharded collection. SeeMap-Reduce and Sharded Collections.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值