Troubleshoot the Map Function对Map函数进行故障排除

On this page本页内容

Note注意
Aggregation Pipeline as Alternative to Map-Reduce聚合管道作为Map-Reduce的替代方案

Starting in MongoDB 5.0, map-reduce is deprecated:从MongoDB 5.0开始,不推荐使用map-reduce

  • Instead of map-reduce, you should use an aggregation pipeline. 应该使用聚合管道,而不是map-reduceAggregation pipelines provide better performance and usability than map-reduce.聚合管道提供了比map-reduce更好的性能和可用性。
  • You can rewrite map-reduce operations using aggregation pipeline stages, such as $group, $merge, and others.您可以使用聚合管道阶段(例如$group$merge等)重写map-reduce操作。
  • For map-reduce operations that require custom functionality, you can use the $accumulator and $function aggregation operators, available starting in version 4.4. You can use those operators to define custom aggregation expressions in JavaScript.对于需要自定义功能的map-reduce操作,可以使用$accumulator$function聚合运算符,从版本4.4开始提供。可以使用这些运算符在JavaScript中定义自定义聚合表达式。

For examples of aggregation pipeline alternatives to map-reduce, see:有关map reduce的聚合管道替代方案的示例,请参阅:

An aggregation pipeline is also easier to troubleshoot than a map-reduce operation.聚合管道也比map-reduce操作更容易进行故障排除。

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.map函数是一个JavaScript函数,它将值与键关联或“映射”,并在map-reduce操作期间发出键和值对。

Note注意

Starting in MongoDB 4.4, mapReduce no longer supports the deprecated BSON type JavaScript code with scope (BSON type 15) for its functions. 从MongoDB 4.4开始,mapReduce不再支持不推荐的BSON类型JavaScript代码,其功能的作用域为(BSON类型15)。The map, reduce, and finalize functions must be either BSON type String (BSON type 2) or BSON type JavaScript (BSON type 13). mapreducefinalize函数必须是BSON类型字符串(BSON类型2)或BSON类型JavaScript(BSON类型13)。To pass constant values which will be accessible in the map, reduce, and finalize functions, use the scope parameter.要传递可在mapreducefinalize确定函数中访问的常量值,请使用scope参数。

The use of JavaScript code with scope for the mapReduce functions has been deprecated since version 4.2.1.自版本4.2.1以来,不推荐使用具有mapReduce函数作用域的JavaScript代码。

Verify Key and Value Pairs验证键和值对

To verify the key and value pairs emitted by the map function, write your own emit function.要验证map函数发出的键和值对,请编写自己的emit函数。

Consider a collection orders that contains documents of the following prototype:考虑包含以下原型文档的集合orders

{
     _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:定义映射函数,将每个文档的price映射到cust_id,并发出cust_idprice对:

    var map = function() {
        emit(this.cust_id, this.price);
    };
  2. Define the emit function to print the key and value:定义emit函数以打印键和值:

    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:使用orders集合中的单个文档调用map函数:

    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:使用orders集合中的多个文档调用map函数:

    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. map函数必须满足各种要求。For a list of all the requirements for the map function, see mapReduce, or mongosh helper method db.collection.mapReduce().有关map函数的所有要求的列表,请参阅mapReducemongosh 助手方法db.collection.mapReduce()

←  Perform Incremental Map-ReduceTroubleshoot the Reduce Function →