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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:32:27+00:00 2026-06-15T22:32:27+00:00

I am reading Mongodb’s docs about Aggregation Framework and Mapreduce, but still have no

  • 0

I am reading Mongodb’s docs about Aggregation Framework and Mapreduce, but still have no clue where to begin with aggregating “columns” of integers in array. F.i. having these documents:

[{ "_id" : "A", "agent" : "006", "score" : [ 1, 0, 0 ], "qv" : [ 1, 0, 1, 0, 1 ] },
 { "_id" : "B", "agent" : "006", "score" : [ 0, 1, 0 ], "qv" : [ 1, 0, 1, 0, 1 ] },
 { "_id" : "C", "agent" : "006", "score" : [ 1, 0, 0 ], "qv" : [ 1, 0, 1, 0, 0 ] },
 { "_id" : "D", "agent" : "007", "score" : [ 1, 0, 0 ], "qv" : [ 1, 0, 1, 0, 0 ] }] 

The expected result should be like:

[
  {"agent": "006", "score": [2, 1, 0], "qv": [3, 0, 3, 0, 2]},
  {"agent": "007", "score": [1, 0, 0], "qv": [1, 0, 1, 0, 0]}
]

Is Aggregation Framework enough for this task or should I aim for Mapreduce?

  • 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-15T22:32:28+00:00Added an answer on June 15, 2026 at 10:32 pm

    I think you’ll need map reduce for this, in order to write a function that can access specific positions in the array. You could try something like this:

    Mapping function:

    var M = function() { 
        emit( this.agent, { score : this.score, qv : this.qv } )
    }
    

    Reduce function:

    var R = function(key, values) {
        var result = { score : [0, 0, 0], qv : [0, 0, 0, 0, 0] };
        values.forEach( function(value) {
            for ( var i = 0; i < value.score.length; i ++ ) {
                result.score[i] += parseInt(value.score[i]);
            }
            for ( var i = 0; i < value.qv.length; i ++ ) {
                result.qv[i] += parseInt(value.qv[i]);
            }
        });
        return result;     
    }
    

    You can then run the following mapReduce function on your collection:

    db.foo.mapReduce( M, R, { out : "resultCollection" } )
    

    And that should give you the following desired result !

    {
    "_id" : "006",
    "value" : {
        "score" : [2, 1, 0],
        "qv" : [ 3, 0, 3, 0, 2 ]
    }
    }
    {
    "_id" : "007",
    "value" : {
        "score" : [ 1, 0, 0],
        "qv" : [ 1, 0, 1, 0, 0]
    }
    

    }

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

Sidebar

Related Questions

I have been reading about tag aware sharding..These are the links I referred: http://www.mongodb.org/display/DOCS/Tag+Aware+Sharding
I am reading the book MongoDB in Action . I have a question about
I'm just learning how to use Mapreduce and reading this documentation: http://django-mongodb.org/topics/mapreduce.html But it
I have been reading about mongodb since 2 weeks back. I have given a
I've been reading up on MongoDB. I am particularly interested in the aggregation frameworks
So I was reading this documentation on changing config servers: http://www.mongodb.org/display/DOCS/Changing+Config+Servers While I am
I'm reading about Mongodb's indexes for my Mongodb project I am well aware that
I am reading that document: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24mod $mod The $mod operator allows you to do
Im reading the mongodb guide, but I dont get this: mongodb://fred:foobar@localhost It says I
Ok I am new to MongoDB and been reading a book about it, some

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.