[英]Map Reduce in MongoDB: undefined is not a function
我必须使用mapReduce
作为项目,我开始关注文档 。
我已经在Mongo中创建了一个名为test
的数据库,我从集合col_one
example中插入了对象:
{
_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 } ]
}
我的代码很简单(如示例所示):
// MongoDB part
// Create server
var mapFunction1 = function() {
emit(this.cust_id, this.price);
};
var reduceFunction1 = function(keyCustId, valuesPrices) {
return Array.sum(valuesPrices);
};
collection.mapReduce(
mapFunction1,
reduceFunction1,
{ out: "col_two" }
);
// Print items from col_two
这会引发此错误:
.../node_modules/mongodb/lib/mongodb/connection/server.js:524
throw err;
^ TypeError: undefined is not a function
如果我改为此,则此错误消失。
collection.mapReduce(
mapFunction1,
reduceFunction1,
{ out: "col_two" },
function() {
// Print items from col_two
}
);
为什么错误会消失?
您所遇到的是shell中使用的API与本机node.js驱动程序之间的主要区别之一:shell是同步的,而node.js驱动程序是异步的。
由于node.js驱动程序是异步的,因此必须为mapReduce
调用提供回调参数,如文档中所示,以便您可以接收结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.