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 8929519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:41:13+00:00 2026-06-15T08:41:13+00:00

i have a question about passing an arugment in MongoDB javascript to a map

  • 0

i have a question about passing an arugment in MongoDB javascript to a map function.
Currently, what i had in mind is something like this:

var map = function(n) {
    if(this.x == n){
        emit(this.x);
    }
}
var reduce = function(key, values) {
    values.forEach(function(x) {
        //do something
    });
    return {nd:values};
}
db.smsdb.mapReduce(map(2), reduce, "collection")

But as i have tried to do this, the shell returns an error “not code”…so i’m guessing i’m not doing this the right way.
Does anyone have the right solution for this kind of problem, i would be more than glad to get it right.

Thanks

  • 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-15T08:41:15+00:00Added an answer on June 15, 2026 at 8:41 am

    This line:

    db.smsdb.mapReduce(map(2), reduce, "collection")
    

    is calling map(2) with the result (undefined) being passed as the map function for mapReduce.

    Instead do something like this:

    db.smsdb.mapReduce(function(){ map.call(this, 2); }, reduce, "collection")
    

    UPDATE

    The above doesn’t work because the map function isn’t available in the scope the mapReduce map function is run. So you have to wrap it up into a single function that can generate the map function you need:

    var mapper = function(n) {
        function map() {
            if(this.x == n){
                emit(this.x);
            }
        }
        return map;
    };
    db.smsdb.mapReduce(mapper(2), reduce, "collection");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had look this question about Passing command line arguments in C# . But
I have a question about the map function in Python. From what I understand,
I have question about parsing in Html helper : I have sth like: @foreach
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
Possible Duplicate: Is Java pass by reference? I have a question about passing by
I have a simple question about passing custom objects via a webservice. I created
I have a question about preg_replace() function. I'm using it with 'e' modifier. Here
This is a follow up to a previous question that I had before about
I have a question about events in .NET (C#). I have had to write
I have a thorny question about transforming Linq Expressions. I had a good search

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.