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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:34:13+00:00 2026-05-22T21:34:13+00:00

I have a collection of documents like: { browser : firefox, version : 4.0.1

  • 0

I have a collection of documents like:

{
"browser" : "firefox",
"version" : "4.0.1"
}

{
"browser" : "firefox",
"version" : "3.6.2"
}

{
"browser" : "ie",
"version" : "8.0"
}

How to count the average of all browsers so the results would be:

global firefox: 66%
global ie: 33%

precise firefox:
4.0.1: 50%
3.6.3: 50%

The tricky part is that I don’t want to provide all Firefox verions available in an array. MongoDB query should find all distinct versions in a collection and count average for all of them.

Thanks in advance!

  • 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-05-22T21:34:14+00:00Added an answer on May 22, 2026 at 9:34 pm

    Here is a solution that produces your statistics with pure numbers (e.g. 0.5 instead of 50%):

    var m = function() {
      emit('global', this.browser);
      emit('local', [this.browser, this.version]);
    };
    
    var r = function(key, values) {
      var global={}, local={}, total=0, i, j, x;
      if (key == 'global') {
        values.forEach(function(v) {
          global[v] = (global[v]||0) + 1;
          total += 1;
        });
        for (i in global) { global[i] = global[i] / total; }
        return global;
      } else if (key == 'local') {
        values.forEach(function(v) {
          if (!local[v[0]]) { local[v[0]] = {}; }
          x = local[v[0]];
          x[v[1]] = (x[v[1]]||0) + 1;
        });
        for (i in local) {
          total = 0;
          x = local[i];
          for (j in x) { total += x[j]; }
          for (j in x) { x[j] = x[j] / total; }
        }
        return local;
      };
    };
    
    db.browsers.mapReduce(m, r, {out:'bout'});
    db.bout.find();
    // => { "_id" : "global", "value" : { "firefox" : 0.6666666666666666, "ie" : 0.3333333333333333 } }
    // => { "_id" : "local", "value" : { "firefox" : { "4.0.1" : 0.5, "3.6.2" : 0.5 }, "ie" : { "8.0" : 1 } } }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginning with mongodb and have a collection with documents that look like
I have a MongoDB collection called employees where there are documents like this: {
I have a collection of documents that I'm attempting to parse. Like HTML, they
I have documents collection Messages in my base (RavenDB) Document definition like: class Message
I have a collection of data stored in XDocuments and DataTables, and I'd like
I have a collection of HTML documents for which I need to parse the
I have a large collection of documents scanned into PDF format, and I wish
I have a collection of objects to which I'd like to just add a
I have a list box that contains a collection of documents that a user
I have already implemented and tested the data structure and would now like to

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.