Troubleshoot the Map Function地图功能故障排除
On this page本页内容
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对于需要自定义功能的map reduce操作,可以使用$accumulatorand$functionaggregation operators, available starting in version 4.4. You can use those operators to define custom aggregation expressions in JavaScript.$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操作期间发出键和值对。
Starting in MongoDB 4.4, 从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). mapReduce不再支持不推荐使用的BSON Type JavaScript代码,其函数具有作用域(BSON Type 15)。map、reduce和finalize函数必须是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.map、reduce和finalize函数中可访问的常数值,请使用scope参数。
The use of JavaScript code with scope for the 自4.2.1版本以来,一直不赞成使用具有mapReduce functions has been deprecated since version 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函数发出的key和value对,请编写自己的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 } ]
}
Define the定义mapfunction that maps thepriceto thecust_idfor each document and emits thecust_idandpricepair:map函数,将每个文档的price映射到cust_id,并发出cust_id和price对:var map = function() {
emit(this.cust_id, this.price);
};Define the定义emitfunction to print the key and value:emit函数以打印键和值:var emit = function(key, value) {
print("emit");
print("key: " + key + " value: " + tojson(value));
}Invoke the使用mapfunction with a single document from theorderscollection:orders集合中的单个文档调用map函数:var myDoc = db.orders.findOne( { _id: ObjectId("50a8240b927d5d8b5891743c") } );
map.apply(myDoc);Verify the key and value pair is as you expected.验证键和值对是否符合预期。emit
key: abc123 value:250Invoke the使用mapfunction with multiple documents from theorderscollection: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();
}Verify the key and value pairs are as you expected.验证键和值对是否符合预期。
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函数的所有要求的列表,请参阅mapReduce或mongosh helper方法db.collection.mapReduce()。