Reputation: 9459
I'm trying to use Node.js to write a MongoDB map/reduce job. I've got the node.js code that calls the map reduce working correctly, but I need to do a Node.js call in the mapper method. It looks like it's interpreting the mapper (and reducer) as Javascript, rather than as Node.js.
Is there a way to execute the mapper and reducer as Node.js functions? Is there some trick or workaround I can do to call my Node.js from the Javascript?
My code:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var fbFeed = require('./getFeeds');
var server = new Server('localhost', 27017, {auto_reconnect: true});
var db = new Db('test', server);
var map = function () {
fbFeed.getFbFeed(this.dbId, function(record) {
emit(record.fbId, record);
});
}
var reduce = function (key, values) {
return values;
}
db.open(function (err, db) {
if (!err) {
db.collection('facebookToScan', function (err, collection) {
if (!err) {
collection.mapReduce(map, reduce, {'out': "fbClicks"}, function(err, collection) {
});
}
});
}
});
(where './getFeeds' contains getFbFeed
)
And my error is:
mr failed, removing collection :: caused by :: 9014 map invoke failed: JS Error: ReferenceError: fbFeed is not defined nofile_b:1
Upvotes: 1
Views: 326
Reputation: 36794
I am not aware of this being possible. The M/R in MongoDB runs in its own JavaScript environment would links to the JavaScript environment in which node.js runs.
Upvotes: 1