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 MongoDB 4.4, mapReduce
no longer supports the deprecated BSON type JavaScript code with scope (BSON type 15) for its functions. The map
, reduce
, and finalize
functions must be either 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.
The use of JavaScript code with scope for the mapReduce
functions has been deprecated since version 4.2.1.
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:
map
function that maps the price
to the cust_id
for each document and emits the cust_id
and price
pair:
emit
function to print the key and value:
map
function with a single document from the orders
collection:
map
function with multiple documents from the orders
collection:
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()
.