Docs HomeMongoDB Manual

Troubleshoot the Map Function地图功能故障排除

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. Aggregation 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和其他阶段)重写映射减少操作。
  • 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:有关映射减少的聚合管道替代方案的示例,请参阅:

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.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. The map, reduce, and finalize functions must be either BSON type String (BSON Type 2) or BSON Type JavaScript (BSON Type 13). 从MongoDB 4.4开始,mapReduce不再支持不推荐使用的BSON Type JavaScript代码,其函数具有作用域(BSON Type 15)。mapreducefinalize函数必须是BSON类型的String(BSON type 2)或BSON类型JavaScript(BSON Type13)。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函数发出的keyvalue对,请编写自己的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:定义map函数,将每个文档的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 helper方法db.collection.mapReduce()