mongodb: how to debug map/reduce on mongodb shell

2019-03-26 08:29发布

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条回答
三岁会撩人
2楼-- · 2019-03-26 08:34

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楼-- · 2019-03-26 08:40

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楼-- · 2019-03-26 08:45

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)

查看更多
登录 后发表回答