Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb: how to debug map/reduce on mongodb shell

I am new to MongoDB, I am using map/reduce. Can somebody tell me how to debug while using map/reduce? I used "print()" function but on MongoDB shell, nothing is printed. Following is my reduce function:

    var reduce = function(key, values){
        var result = {count: 0, host: ""};

        for(var i in values){
        result.count++;
        result.host = values[i].host;
        print(key+" : "+values[i]);
        }
        return result;
    }

when I write the above function on shell and the press Enter after completing, nothing gets printed on the shell. Is there anything else I should do to debug?

Thanks

like image 952
Bhupi Avatar asked Sep 23 '11 09:09

Bhupi


3 Answers

It seems that print() statements in reduce functions are written to the log file, rather than the shell. So check your log file for your debug output.

You can specify the log file by using a --logpath D:\path\to\log.txt parameter when starting the mongod process.

like image 79
Niels van der Rest Avatar answered Nov 19 '22 11:11

Niels van der Rest


Take a look at this simple online MongoDB MapReduce debugger which allows to get aggregation results on sample data as well as perform step-by-step debugging of Map / Reduce / Finalize functions right in your browser dev environment.

I hope it will be useful.

http://targetprocess.github.io/mongo-mapreduce-debug-online/

like image 33
user3493312 Avatar answered Nov 19 '22 10:11

user3493312


There is a dedicated page on the mongodb website which is your answer : http://www.mongodb.org/display/DOCS/Troubleshooting+MapReduce

and obviously your reduce is wrong : the line result.count++ will end up containing the number of elements contained in the array values which (in map reduce paradigm) does not mean anything. Your reduce function is just returning a "random" hostname (because mapreduce algo is not predicable on the reduce content at any step) and a random number.

Can you explain what you want to do ? (my guess would be that you want to count the number of "something" per host)

like image 3
kamaradclimber Avatar answered Nov 19 '22 09:11

kamaradclimber