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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:53:29+00:00 2026-06-08T23:53:29+00:00

I have some different Map/Reduces functions that I use in my project. But one

  • 0

I have some different Map/Reduces functions that I use in my project. But one is a lot different than the others since it requires a loop in the map functionality. And for each count in the loop, I send an emit.

What I have is this scenario (in the user collection):

 "channels" : [
        "Channel 1",
        "Channel 2",
    ],

What I want to do is to count how many users each channel has. So for that I could use db.users.find({channels: "Channel 1"}).count() but unfortunately channels are dynamic which means I don’t know all the possible channel names and it may well change in the future.

So I thought that a Map/Reduce job would sit just perfect. But the problem is that the first Reduce job I wrote calculated wrong. And the other where I used a query for each emit, would come to take forever (more than 3 hours before the ssh session shut down).

So now I’m stuck and I need help, preferably I would want to have a Map/Reduce job since it’s more nice than a bunch of queries which is kind of slow to run in real time.

This is the latest Map and Reduce functions I wrote:

var map = function() {
    if(this.channels) {
            for(var i = 0, imax = this.channels.length; i<imax; i++) {
            emit(this.channels[i], 1);
        }   
    }
}

var reduce = function (key, values) {
    var result = 0;

    values.forEach(function (value) {
        // had this before: result += 1;
        result = db.users.find({'channels' : key}).count();
    });

    return result;
}

I knew that the reduce function was horrific but I just tried the best I could think of. I think my logic may seem wrong but I can’t find a good solution. Now I’m thinking of just doing a bunch of queries on every page load, but it will be slow as hell.

Please help! 🙂

  • 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-08T23:53:30+00:00Added an answer on June 8, 2026 at 11:53 pm

    In your scenario the reduce function should look like this:

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

    Let me know if it is still not working and if it does please give an example of input and (incorrect) output.

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

Sidebar

Related Questions

I have some code in a couple of different functions that looks something like
I have map with multiple different colors markers with some info window. Everything works
I have some folder with different files. I want to use something like this:
I have some XML files that contain different function information. I am trying to
I have a map that I need to map to a different type, and
I have come across with this syntax while reading some others code Map<String, String>
I have some different cells in my tableView, each with different subviews. Each time
I have read some different responses for this same question, so I dont know
I have to do some different views containing 72 LED lights. I built an
I have some 10 buttons with different names each.On selecting each button I need

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.