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

The Archive Base Latest Questions

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

I have a set of objects in Mongodb that each have a set of

  • 0

I have a set of objects in Mongodb that each have a set of values embedded in them, e.g.:

[1.22, 12.87, 1.24, 1.24, 9.87, 1.24, 87.65] // ... up to about 150 values

Is a map/reduce the best solution for finding the median (average) and mode (most common value) in the embedded arrays? The reason that I ask is that the map and the reduce both have to return the same (structurally) set of values. It looks like in my case I want to take in a set of values (the array) and return a set of two values (median, mode).

If not, what’s the best way to approach this? I want it to run in a rake task, if that’s relevant. It’d be an overnight data crunching kind of thing.

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

    I assume you want to find the mode & median of each document, you can do this with map reduce. In this case you calculate median & mode in the map function and reduce will return the map result untouched

    map = function() {
       var res = 0;
       for (i = 0; i < this.marks.length; i++) {
          res = res + this.marks[i];
       }
       var median = res/this.marks.length;
       emit(this._id,{marks:this.marks,median:median});
    }
    
    
    reduce = function (k, values) {
        values.forEach(function(value) {
          result = value;
        });
        return result;
    }
    

    and for this collection

    { "_id" : ObjectId("4f02be1f1ae045175f0eb9f1"), "name" : "ram", "marks" : [ 1.22, 12.87, 1.24, 1.24, 9.87, 1.24, 87.65 ] }
    { "_id" : ObjectId("4f02be371ae045175f0eb9f2"), "name" : "sam", "marks" : [ 1.32, 11.87, 12.4, 4.24, 9.37, 3.24, 7.65 ] }
    { "_id" : ObjectId("4f02be4c1ae045175f0eb9f3"), "name" : "pam", "marks" : [ 3.32, 10.17, 11.4, 2.24, 2.37, 3.24, 30.65 ] }
    

    you can get the median by

      db.test.mapReduce(map,reduce,{out: { inline : 1}})
    
    {
        "results" : [
            {
                "_id" : ObjectId("4f02be1f1ae045175f0eb9f1"),
                "value" : {
                    "marks" : [
                        1.22,
                        12.87,
                        1.24,
                        1.24,
                        9.87,
                        1.24,
                        87.65
                    ],
                    "median" : 16.475714285714286
                }
            },
            {
                "_id" : ObjectId("4f02be371ae045175f0eb9f2"),
                "value" : {
                    "marks" : [
                        1.32,
                        11.87,
                        12.4,
                        4.24,
                        9.37,
                        3.24,
                        7.65
                    ],
                    "median" : 7.155714285714285
                }
            },
            {
                "_id" : ObjectId("4f02be4c1ae045175f0eb9f3"),
                "value" : {
                    "marks" : [
                        3.32,
                        10.17,
                        11.4,
                        2.24,
                        2.37,
                        3.24,
                        30.65
                    ],
                    "median" : 9.055714285714286
                }
            }
        ],
        "timeMillis" : 1,
        "counts" : {
            "input" : 3,
            "emit" : 3,
            "reduce" : 0,
            "output" : 3
        },
        "ok" : 1,
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of objects that I want to conform to an interface,
I have an odd problem with Django. I have a set of objects that
I have a class that serializes a set of objects (using XML serialization) that
I have an API that filters a set of objects based on the ID
I have a class that holds another objects inside it (List, Set and objects
I have a set of objects, each of which has a collection of objects
I have a set of objects and for each object in this set I
Consider you have a set of objects X (e.g., A, B, C, D) that
I have an object that is mapped to have a set of objects, very
Assume that I have a set of objects that need to be analyzed in

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.