Node.js 使用mongoose在mongodb上执行mapreduce时,无法在reduce函数中使用下划线.js
我有一个电影收藏,每部电影都有几位明星,例如:Node.js 使用mongoose在mongodb上执行mapreduce时,无法在reduce函数中使用下划线.js,node.js,mongodb,mapreduce,underscore.js,Node.js,Mongodb,Mapreduce,Underscore.js,我有一个电影收藏,每部电影都有几位明星,例如: { movie: "Pride & Prejudice", starList: ["Keira Knightley", "Matthew Macfadyen"] } { movie: "Begin Again", starList: ["Keira Knightley", "Mark Ruffalo"] } 我想得到一个倒排索引,每个明星都有几部电影 { 明星:“凯拉·奈特利”, 电影演员:[《傲慢与
{
movie: "Pride & Prejudice",
starList: ["Keira Knightley", "Matthew Macfadyen"]
}
{
movie: "Begin Again",
starList: ["Keira Knightley", "Mark Ruffalo"]
}
我想得到一个倒排索引,每个明星都有几部电影
{
明星:“凯拉·奈特利”,
电影演员:[《傲慢与偏见》,《重新开始》]
}
这可以通过在mongodb上使用mapreduce来实现。我使用nodejs和mongoose驱动程序。代码如下:
var _ = require("underscore");
var o = {};
o.scope = { _: _ };
o.map = function()
{
var movie = this.movie;
this.starList.forEach(function(star)
{
emit(star, { movieList: [movie]});
});
};
o.reduce = function(k, vals)
{
var movieList = _.flatten(_.pluck(vals, "movieList"));
return { movieList: movieList};
};
Movie.mapReduce(o).then(function(results)
{
console.log(JSON.stringify(results, null, 4));
});
我收到一个错误,显示我无法在reduce函数中使用下划线.js
MongoError: TypeError: _.pluck is not a function :
_funcs2@:8:34
at Function.MongoError.create (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
at commandCallback (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1187:66)
at Callbacks.emit (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3)
at null.messageHandler (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:358:23)
at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:292:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
at TCP.onread (net.js:536:20)
MongoError:TypeError:\ pull不是一个函数:
_funcs2@:8:34
在Function.MongoError.create(/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb core/lib/error.js:31:11)
在commandCallback(/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb core/lib/topologies/server.js:1187:66)
在Callbacks.emit(/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb core/lib/topologies/server.js:119:3)
位于null.messageHandler(/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb core/lib/topologies/server.js:358:23)
在插座上。(/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb core/lib/connection/connection.js:292:22)
在emitOne(events.js:77:13)
位于Socket.emit(events.js:169:7)
在readableAddChunk(_stream_readable.js:153:18)
在Socket.Readable.push(_stream_Readable.js:111:10)
在TCP.onread(net.js:536:20)
我可能错了。但我认为,当您尝试访问reduce方法中的下划线方法时,您的u变量并没有所有的方法,或者您必须使用不同的访问方法。我建议这样试试
_['pluck'](....)
还要检查u变量,看看是否所有方法都在正确的范围内
希望这能有所帮助。使用聚合框架的一种非常简单的方法如下:
Movie.aggregate([
{ "$unwind": "$starsList" },
{
"$group": {
"_id": "$starsList",
"movieList": {"$push": "$movie"}
}
},
{
"$project": {
"_id": 0,
"star": "$_id",
"movieList": 1
}
}
]).exec(callback);
或者使用fluent生成器API:
Movie.aggregate()
.unwind("starsList")
.group({
"_id": "$starsList",
"movieList": {"$push": "$movie"}
})
.project({
"_id": 0,
"star": "$_id",
"movieList": 1
})
.exec(callback);
另外,“StartList”应该是“starList”谢谢,它可以工作,但我不能接受你的答案,因为问题是关于“在mapreduce中使用下划线”如何检查reduce函数中的u变量?似乎我不能在reduce函数中使用console.log()