Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Is it possible to use node.js in the map function of a mongoDB map reduce?

Is it possible to use node.js in the map function of a mongoDB map reduce?

Problem

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
Problem courtesy of: Ron Romero

Solution

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.

Solution courtesy of: Derick

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Is it possible to use node.js in the map function of a mongoDB map reduce?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×