mongodb: how to debug map/reduce on mongodb shell

2019-03-26 07:49发布

问题:

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

回答1:

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.



回答2:

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:

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)