banner ad

MongoDB Map-Reduce db.collection.mapReduce()

| March 19, 2013 | 0 Comments
0 Flares 0 Flares ×

Map-reduce operations can handle complex aggregation tasks. To perform map-reduce operations, MongoDB provides the mapReduce command and, in the mongo shell, the db.collection.mapReduce() wrapper method.

db.collection.mapReduce(mapreduce{<out><query><sort><limit><finalize><scope><jsMode><verbose>})

db.collection.mapReduce(
                         <map>,
                         <reduce>,
                         {
                           out: <collection>,
                           query: <document>,
                           sort: <document>,
                           limit: <number>,
                           finalize: <function>,
                           scope: <document>,
                           jsMode: <boolean>,
                           verbose: <boolean>
                         }
                       )

Parameters:
  • map –A JavaScript function that associates or “maps” a value with a key and emits the key and value pair.

    The map function processes every input document for the map-reduce operation. The map-reduce operation groups the emitted value objects by the key and passes these groupings to the reduce function.

  • reduce –A JavaScript function that “reduces” to a single object all the values associated with a particular key.

    The reduce function accepts two arguments: key and values. The values argument is an array whose elements are the value objects that are “mapped” to the key.

  • out –New in version 1.8.

    Specifies the location of the result of the map-reduce operation. You can output to a collection, output to a collection with an action, or output inline. You may output to a collection when performing map reduce operations on the primary members of the set; on secondary members you may only use the inline output.

  • query – Optional. Specifies the selection criteria using query operators for determining the documents input to the mapfunction.
  • sort – Optional. Sorts the input documents. This option is useful for optimization. For example, specify the sort key to be the same as the emit key so that there are fewer reduce operations.
  • limit – Optional. Specifies a maximum number of documents to return from the collection.
  • finalize –Optional. A JavaScript function that follows the reduce method and modifies the output.

    The finalize function receives two arguments: key and reducedValue. The reducedValue is the value returned from the reduce function for the key.

  • scope (document) – Optional. Specifies global variables that are accessible in the map , reduce and the finalizefunctions.
  • jsMode (Boolean) –New in version 2.0.

    Optional. Specifies whether to convert intermediate data into BSON format between the execution of the map and reducefunctions.

    If false:

    • Internally, MongoDB converts the JavaScript objects emitted by the map function to BSON objects. These BSON objects are then converted back to JavaScript objects when calling the reduce function.
    • The map-reduce operation places the intermediate BSON objects in temporary, on-disk storage. This allows the map-reduce operation to execute over arbitrarily large data sets.

    If true:

    • Internally, the JavaScript objects emitted during map function remain as JavaScript objects. There is no need to convert the objects for the reduce function, which can result in faster execution.
    • You can only use jsMode for result sets with fewer than 500,000 distinct key arguments to the mapper’s emit()function.

    The jsMode defaults to false.

  • verbose (Boolean) – Optional. Specifies whether to include the timing information in the result information. Theverbose defaults to true to include the timing information.
Download PDF
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×

Tags: ,

Category: Uncategorized

About the Author ()

My name is John Link.I am 26 years old. My major is Computer science and technology. I am a junior programmer with Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×