MapReduce aggregation

MapReduce aggregation

Mongo Streams supports mongodbs mapreduce functionality. Similarly with Aggregation Pipeline syntax mapReduce is based on initial query. It also honors thesort` syntax from query. Following are available syntax for mapReduce operation:

  • simple map and reduce operation
 query() mapReduce (mapJavaScript reduce reduceJavaScript)
  • with finalization function
 query() mapReduce (mapJavaScript reduce reduceJavaScript finalize finalizeJavaScript)
  • with sorting of documents first query() sort ("key" Ascending) mapReduce (mapJavaScript reduce reduceJavaScript )
  • with configuring javascript scope query() mapReduce (mapJavaScript reduce reduceJavaScript scope scopeJavascript)
  • with configuring in-memory javascript object passing query() mapReduce (mapJavaScript reduce reduceJavaScript jsModeInMemory)
  • with configuring increased verbosity query() mapReduce (mapJavaScript reduce reduceJavaScript verbose)
  • with specifying maximum number of results returned query() mapReduce (mapJavaScript reduce reduceJavaScript limit (100))

Specifying output of MapReduce command

Each mapReduce command may eventually specify if results will be stored persistently into database, where these results will be stored and
how the results will be stored. By default results override any results found in target collection. This is controlled with persists syntax:

  • in current database under given collection :
 query() mapReduce (mapJavaScript reduce reduceJavaScript persist ("results"))
  • in supplied collection and database :
 query() mapReduce (mapJavaScript reduce reduceJavaScript persist ("resultdb.results"))
  • in supplied collection : query() mapReduce (mapJavaScript reduce reduceJavaScript persist (dbCollection))
  • to merge with current records in result collection : query() mapReduce (mapJavaScript reduce reduceJavaScript persist (dbCollection) mergeCurrent)
  • to reduce with current records in result collection : query() mapReduce (mapJavaScript reduce reduceJavaScript persist (dbCollection) reduceCurrent)
  • to store results in shard collection : query() mapReduce (mapJavaScript reduce reduceJavaScript persist (dbCollection) asShard)
  • to make the store operation nonatomic :
query() mapReduce (mapJavaScript reduce reduceJavaScript persist (dbCollection) nonAtomic)

Total for specification MapReduceSpec
Finished in2 seconds, 320 ms
Results4 examples, 0 failure, 0 error