Docs Menu
Docs Home
/
MongoDB Manual
/ /

Troubleshoot the Map Function

On this page

  • Verify Key and Value Pairs

Note

Aggregation Pipeline as Alternative to Map-Reduce

An aggregation pipeline provides better performance and usability than a map-reduce operation.

Map-reduce operations can be rewritten using aggregation pipeline operators, such as $group, $merge, and others.

For map-reduce operations that require custom functionality, MongoDB provides the $accumulator and $function aggregation operators starting in version 4.4. Use these operators to define custom aggregation expressions in JavaScript.

For examples of aggregation pipeline alternatives to map-reduce operations, see Map-Reduce to Aggregation Pipeline and Map-Reduce Examples.

An aggregation pipeline is also easier to troubleshoot than a map-reduce operation.

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:

{
_id: ObjectId("50a8240b927d5d8b5891743c"),
cust_id: "abc123",
ord_date: new Date("Oct 04, 2012"),
status: 'A',
price: 250,
items: [ { sku: "mmm", qty: 5, price: 2.5 },
{ sku: "nnn", qty: 5, price: 2.5 } ]
}
  1. Define the map function that maps the price to the cust_id for each document and emits the cust_id and price pair:

    var map = function() {
    emit(this.cust_id, this.price);
    };
  2. Define the emit function to print the key and value:

    var emit = function(key, value) {
    print("emit");
    print("key: " + key + " value: " + tojson(value));
    }
  3. Invoke the map function with a single document from the orders collection:

    var myDoc = db.orders.findOne( { _id: ObjectId("50a8240b927d5d8b5891743c") } );
    map.apply(myDoc);
  4. Verify the key and value pair is as you expected.

    emit
    key: abc123 value:250
  5. Invoke the map function with multiple documents from the orders collection:

    var myCursor = db.orders.find( { cust_id: "abc123" } );
    while (myCursor.hasNext()) {
    var doc = myCursor.next();
    print ("document _id= " + tojson(doc._id));
    map.apply(doc);
    print();
    }
  6. Verify the key and value pairs are as you expected.

Tip

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().

Back

Perform Incremental Map-Reduce