- Aggregation >
- Aggregation Examples >
- Troubleshoot the Map Function
Troubleshoot the Map Function¶
The map function is a JavaScript function that associates or “maps”
a value with a key and emits the key and value pair during a
map-reduce operation.
To verify the key and value pairs emitted by the map
function, write your own emit function.
Consider a collection orders that contains documents of the
following prototype:
Define the
mapfunction that maps thepriceto thecust_idfor each document and emits thecust_idandpricepair:Define the
emitfunction to print the key and value:Invoke the
mapfunction with a single document from theorderscollection:Verify the key and value pair is as you expected.
Invoke the
mapfunction with multiple documents from theorderscollection:Verify the key and value pairs are as you expected.
See also
The map function must meet various requirements. For a list of all
the requirements for the map function, see mapReduce,
or the mongo shell helper method
db.collection.mapReduce().