MongoDB map reduce count giving more results than a query

Posted by giorgiosironi on Stack Overflow See other posts from Stack Overflow or by giorgiosironi
Published on 2012-07-09T08:02:19Z Indexed on 2012/07/09 15:16 UTC
Read the original article Hit count: 153

Filed under:

I have a collection users in Mongo and I execute this map reduce which I believe is the equivalent of a COUNT(*) GROUP BY origin:

> m = function() { for (i in this.membership) {
... emit( this.membership[i].platform_profile.origin, 1 );
... }  }
function () {
    for (i in this.membership) {
        emit(this.membership[i].platform_profile.origin, 1);
    }
}
> r = function( id, values ) { var result = 0; 
... for ( var i = 0; i < values.length; i ++ ) { result += values[i];  }
... return result; }
function (id, values) {
    var result = 0;
    for (var i = 0; i < values.length; i++) {
        result += values[i];
    }
    return result;
}
> db.users.mapReduce(m, r, {out : { inline: 1}});
{
    "results" : [
        {
            "_id" : 0,
            "value" : 15
        },
        {
            "_id" : 1,
            "value" : 449
        },
    ...
}

But if I try to count how many documents have this field set to a specific value like 1, I get fewer results:

db.users.count({"membership.platform_profile.origin": 1});

424

What am I missing?

© Stack Overflow or respective owner

Related posts about mongodb