On this page本页内容
Starting in MongoDB 5.0, map-reduce is deprecated:从MongoDB 5.0开始,不推荐使用map-reduce:
$group
, $merge
, and others.$group
、$merge
等)重写map-reduce操作。$accumulator
and $function
aggregation 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:有关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操作期间发出键和值对。
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. 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). map
、reduce
和finalize
函数必须是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.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代码。
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 } ] }
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_id
和price
对:
var map = function() { emit(this.cust_id, this.price); };
Define the 定义emit
function to print the key and value:emit
函数以打印键和值:
var emit = function(key, value) { print("emit"); print("key: " + key + " value: " + tojson(value)); }
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);
Verify the key and value pair is as you expected.验证密钥和值对是否符合预期。
emit key: abc123 value:250
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(); }
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
助手方法db.collection.mapReduce()
。