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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:48:18+00:00 2026-06-06T10:48:18+00:00

Just started mongo and started having issue with querying already. i have a collection

  • 0

Just started mongo and started having issue with querying already. i have a collection called ‘externalTransaction’ and i want to write a equivalent of this mysql query:

select transactionCode, 
       sum(amount) as totalSum, 
       count(amount) as totalCount 
from externalTransaction 
where transactioncode in ('aa','bb','cc') 
group by sum(amount)

below is my attempt:

{
 "collectionName": "externalTransaction",
  sort: {transactionCode:-1},
  query: {this._id: {$in:['aa','bb','cc']}},
  mapReduce:{
   'map': 'function(){

        emit(this.transactionCode, this.amount);

   }',
 'reduce': 'function(key, values){
    var result = {count: 0, sum: 0.0};

    values.forEach(function(value) {
      result.count++
       result.sum += value.amount;
    });
    return result;
 }',
   'out' : 'sumAmount'
 }

}

the above query give me a result set looking like this:

_id     value.count        value.sum
ct      2.0                NaN
bb      40.0           NaN
fg      71.0           NaN
fd      36.0           NaN
sd      5.0                NaN
as      4.0            NaN
aa      71.0           NaN
df      4.0                NaN
cc      10.0               NaN

From the documentation with the version 2.0.6 i can’t use the aggregation framework just yet so how to handle simple queries like mine in mongo. thanks for reading and excuse the triviality of my question.

  • 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-06T10:48:19+00:00Added an answer on June 6, 2026 at 10:48 am

    You have a few errors in your map and reduce functions. First, in map you emit a simple number, and in reduce you try to take amount of a number. I bet, it doesn’t have that property. Second, outputs of map and reduce must be uniform, because reduce is supposed to be runnable over partially reduced results. Try these functions:

    var map = function() {
      emit(this.transactionCode, {sum: this.amount, count: 1})
    }
    
    var reduce = function(k, vals) {
      var result = {sum: 0, count: 0};
    
      vals.forEach(function(v) {
        result.sum += v.sum;
        result.count += v.count;
      });
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
Just started checking out android development, but having problems already. The activity shuts down
Just started working with .NET and MVC(1). I'm having a problem wherein in my
I've just started working with Mono for Android, and am having trouble getting a
Just started with Scheme. I'm having problem with printing on console. A simple list
Just started tinkering with Dart and I decided to write a simple Http Server
Just started using carrierwave with Rails and things have been going smoothly with one
Just started using fadein/fadeout - it's functioning but with problems. I have a set
Just started learning NHiberate. It seems rather tedious to write proper hbm.xml config files
I just started getting into node.js and followed this great tutorial on node.js at

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.