codious
codious

Reputation: 3511

Integer as key for Map Reduce in Mongodb

I have a collection in Mongodb like the following:

{_id: {"thread": "abc"}, "value": 1}
{_id: {"thread": "cdf"}, "value": 1}
{_id: {"thread": "edf"}, "value": 1}

I tried to map reduce trying to key on value, hoping for an output as follows:

{id_: {"value": 1}, value: 3}

MAP:

function() {
var key = {value : this.value};
emit(key, 1);
};

REDUCE:

function(key, values) {
var sum = 0;
values.forEach(function(value) {
sum += value;});
return sum;
};

EDIT Orignial question: Map Reduce didn't work is not valid anymore. It was a connection problem and not Map Reduce. Thanks to everyone for the extra bit of information.

unfortunately, that didn't work and gave an some output as follows:

{u'_id': {u'value': None}, u'value': 1160856.0}

Isn't it possible to key on an integer value?? I read the map reduce documentation on Mongodb but couldn't find an info related on the data type of key.

Upvotes: 2

Views: 2042

Answers (1)

Remon van Vliet
Remon van Vliet

Reputation: 18595

If you are running your map/reduce from the MongoDB shell you may be running into a feature/issue (opinions vary on that one ;) ) where the shell converts all integer types to double and stores them as such. There is currently no real workaround other than invoking your m/r from something other than the shell. That said, if I run your m/r I get this :

{
        "_id" : {
                "value" : 1
        },
        "value" : 3
}

Which seems to be what you're trying to do. I'm not sure if it is or not. Make sure you're running the latest db version and such.

Upvotes: 3

Related Questions