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

  • SEARCH
  • Home
  • 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 8286935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:54:28+00:00 2026-06-08T11:54:28+00:00

I would like to make some javascript functions that perform parametric map/reduce jobs in

  • 0

I would like to make some javascript functions that perform parametric map/reduce jobs in mongo, but I’m getting confused about the scoping of JavaScript. For example, the following code gives me counts of the "gender" variable; i.e. it will tell me how many "male" and "female" records I have:

// count categories
db.responses.mapReduce(
    function(){
        emit(this["gender"], {count: 1})
    }, function(state, values){
        var result = {count: 0};
        values.forEach(function(value) {
            result.count += value.count;
        });
        return result;
    }, {out: { inline : 1}}
);

This works perfectly fine. In the next step I would like to create a function that does this for an arbitrary property

function countCategories(item) { 
    function mapper(it){
        fn = function(){
            if(this[it]){
                emit(this[it], {count: 1});
            }
        };
        return fn;
    }(item); 
    var reducer = function(state, values){
        var result = {count: 0};
        values.forEach(function(value) {
            result.count += value.count;
        });
        return result;
    };
    var out = {out: { inline : 1}};
    var results = db.responses.mapReduce(
        mapper, 
        reducer, 
        out
    );
    return results;
}   

countCategories("gender")

However when I try:

countCategories("gender")
{
    "results" : [ ],
    "timeMillis" : 48,
    "counts" : {
        "input" : 2462,
        "emit" : 0,
        "reduce" : 0,
        "output" : 0
    },
    "ok" : 1,
}

The emit function has never been called. What has gone wrong here? My guess is something with the scoping of the emit function that mongo supplies, but I’m not quite sure why it is not being called, nor throwing an error.

  • 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-08T11:54:29+00:00Added an answer on June 8, 2026 at 11:54 am

    From what I read in the docs, the scope of functions to be used in database command is not their default javascript scope, but can (and must, if needed) be set manually. So, it think this should work:

    var mapper = function(){
        if(item in this){
            emit(this[item], {count: 1});
        }
    };
    ...
    db.responses.mapReduce(
        mapper, 
        reducer, 
        {
           out: {"inline": 1},
           scope: {"item": item}
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make some input fields that I would like to work like
I have this function that works, but I would like to make this one
I've got some javascript functions that take perform asynchronous requests over the network and
I would like to make some of my code more monadic and to use
I'm developing a free application and would like to make some money by embedding
I would like to make my application ask the user some informations via something
I would like to make a form in HTML where a user enters some
I have a folder with some dotfiles I would like to make symlinks for.
I have some data in .cvs. I would like to make a simple barplot
Would like to make anapplication in Java that will not automatically parse parameters used

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.