Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:02:21+00:00 2026-06-09T01:02:21+00:00

I am yet to ask another Map/Reduce question. I have a collection example which

  • 0

I am yet to ask another Map/Reduce question.

I have a collection “example” which looks like this:

{
"userid" : "somehash",
"channel" : "Channel 1"
}

My Map/Reduce functions look like this:

var map = function () {
    emit(this.channel, {user:this.userid, count: 1});
}

var reduce = function (key, values) {
    var result = {total:0, unique:0};
    var temp = [];
    values.forEach(function (value) {
        result.total += value.count;

        if (temp.indexOf(value.user) == -1) {
            temp.push(value.user);
        }
    });

    result.unique += temp.length;

    return result;
}

Unfortunately, it gives me some really strange results:

{ "_id" : "Channel 1", "value" : { "total" : NaN, "unique" : 47 } }
{ "_id" : "Channel 2", "value" : { "total" : NaN, "unique" : 12 } }
{ "_id" : "Channel 3", "value" : { "total" : 6, "unique" : 6 } }

And it seems like value.count resolves to null, it also seems like “Unique” isn’t the correct value as well. What I want to do is to count all the values for each channel and also calculate it in such a way that I can see an unique value for each user. Which means, a document in this collection, example, may occur several times. I want to know all times AND unique times.

I followed this guide: http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction and I don’t know why I get null thrown in my face? Very strange, any good ideas on the subject?

Thanks for the advice and better wisdom.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:02:23+00:00Added an answer on June 9, 2026 at 1:02 am

    The reason this is happening is because map/reduce sometimes fires over itself, i.e. reduce is fired over the result of reduce. But result of reduce does not have count field. You must always make sure that map emit and reduce result have the same format. Read more about this in documentation.

    EDIT Here’s a simple demonstration how you can fix this:

    var map = function () {
        emit(this.channel, { user: [this.userid], count: 1 });
    }
    
    var reduce = function (key, values) {
        var result = { user: [], count: 0 };
        values.forEach(function (value) {
            result.count += value.count;
    
            value.user.forEach(function(usr) {
                if (result.user.indexOf( usr ) == -1) {
                    result.user.push( usr );
                }
            });
        });
    
        return result;
    }
    

    Now result.user.length should give you unique users. Didn’t test it, but it should work.

    EDIT 2 It should be slow though, .indexOf is a quite expensive function. You can make it faster by making two map/reduce jobs. First you map/reduce over the collection like this:

    var map = function() {
        // make a key unique per channel and userid
        emit( this.channel + '_' + this.userid,
            { count: 1, channel: this.channel }
        );
    }
    
    var reduce = function(key, values) {
        var result = { count: 0, channel: null };
        values.forEach(function( value ) {
            result.count += value.count;
            // Don't worry about these substitutions,
            // these values can't change anyway per key.
            result.channel = value.channel;
        });
        return result;
    }
    

    Now count over this collection will give you number of unique entries. To get the total number you do second map/reduce over results like this:

    var map = function() {
        // Note the key!!!
        emit( this.value.channel, { count: this.value.count } );
    }
    
    var reduce = function(key, values) {
        var result = { count: 0 };
        values.forEach(function( value ) {
            result.count += value.count;
        });
        return result;
    }
    

    This should be a lot faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that I am going to ask a stupid question, yet
This is yet another MySQL literature question. I asked for some MySQL / DBA
I am going to ask yet another question! So, CSS Rotate works in ie9
First: I'm shocked that I have to ask this question. Nowhere in the docs
Intro I am not an R expert yet so please excuse another question which
Yet another data structure doubt. I will get straight to it. This is what
Yet another PHP question. I've got two arrays: one string-based and the other numeric.
Another slightly non-technical question, but I couldn't decide whether to ask here or on
So, I've come back to ask, once more, a patterns-related question. This may be
I'm really sorry. This must seem like an incredibly stupid question, but unless I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.