- Aggregation >
- Map-Reduce >
- Troubleshoot the Map Function
Troubleshoot the Map Function¶
On this page
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.
Note
Starting in version 4.2.1, MongoDB deprecates the use of BSON type
JavaScript code with scope (BSON type 15) for the map
, reduce
, and
finalize
functions. Using JavaScript code with scope with these
functions will print a warning to the log. Instead, use BSON type
String (BSON type 2) or BSON type
JavaScript (BSON type 13). To pass
constant values which will be accessible in the map
, reduce
,
and finalize
functions, use the scope
parameter.
Verify Key and Value Pairs¶
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
map
function that maps theprice
to thecust_id
for each document and emits thecust_id
andprice
pair:Define the
emit
function to print the key and value:Invoke the
map
function with a single document from theorders
collection:Verify the key and value pair is as you expected.
Invoke the
map
function with multiple documents from theorders
collection: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()
.