10

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

3 Answers 3

20

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.

4
  • 5
    The log file in Linux is located in /var/log/mongodb/mongodb.log
    – Dennis Y.
    Commented Mar 30, 2012 at 13:50
  • Mac OS: /usr/local/var/log/mongodb/mongo.log, I'm still unable to see anything in the log file. Commented Oct 24, 2013 at 2:32
  • 2
    tail -f /usr/local/var/log/mongodb/mongo.log worked for me just fine. print("some debug text") called from within reduce function outputs to this file. Make sure your mongo is writing logs to this file. Maybe you overridden it. Check your mongo conf (/usr/local/etc/mongod.conf). Commented Jan 4, 2014 at 12:19
  • the above location for Mac OS didn't work for me. You are better off adding the --logpath flag with your own file. I can see the output when using print() that way
    – dmo
    Commented Aug 11, 2016 at 15:34
4

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/

3
3

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.