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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:06:30+00:00 2026-06-02T01:06:30+00:00

I am having a problem with creating a mapreduce algorithm that will get me

  • 0

I am having a problem with creating a mapreduce algorithm that will get me the stats i need. I have a user object that can create a post and a post can have many likes by other users.

User
–Post
—-Likes

The Post is not embedded in the user because we access posts separately and not just in a user context. The stat I need is the number of likes an author has gotten and i need to get this through the likes of the posts of a user. The problem is that because the posts are not embedded, I cannot access them in my map function. Here are the map and reduce functions I currently have

def reputation_map
 <<-MAP
   function() {      
       var posts = db.posts.find({user_id:this._id});
       emit(this._id, {posts:posts});      
   }
 MAP
end

def reputation_reduce
  <<-REDUCE
    function(key, values) {
      var count = 0;
      while(values.hasNext()){
        values.next();
        count+=1;
      }
      return {posts:count};
     }
 REDUCE
end   

This should only return the posts for each user so I have not even gotten to the likes level yet but instead of a count, this only returns a dbquery for posts. What is the correct way of doing this?

  • 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-02T01:06:32+00:00Added an answer on June 2, 2026 at 1:06 am

    Map Reduce is really designed to operate on a single collection at a time.

    Technically, it is possible to query a separate collection from inside a Map function as you have done, but take caution as this is not recommended nor supported. you may run into issues, especially if the collection is sharded.

    A similar question was asked a while back: How to call to mongodb inside my map/reduce functions? Is it a good practice?

    If you are aggregating results from multiple collections, you may find that the safest and most straight-forward way to do it is in the application.

    Alternatively, if likes per author is a value that will be searched for with some frequency, it may be preferable to include it as a value in each document, and spend a little more overhead on each update to increment this value, rather than periodically performing a potentially resource-heavy calculation of all the votes per author.

    Hopefully this will give you some food for thought for retrieving the values that you need to.

    If you would like some assistance writing a Map Reduce operation for a single collection, the Community is here to help. Please include a sample input document, and a description of the desired output.

    For more information on Map Reduce, the documentation may be found here:
    http://www.mongodb.org/display/DOCS/MapReduce

    Additionally, there are some good Map Reduce examples in the MongoDB Cookbook:
    http://cookbook.mongodb.org/

    The “Extras” section of the cookbook article “Finding Max And Min Values with Versioned Documents” http://cookbook.mongodb.org/patterns/finding_max_and_min/ contains a good step-by-step walkthrough of a Map Reduce operation, explaining how the functions are executed.

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

Sidebar

Related Questions

i am having a problem creating a regular expression that will validate if the
My problem is that I have a user that is having a problem displaying
Having a bit of a problem creating an instance of an object. Bear in
I'm having a problem creating a new CompositeControl. Currently I have a custom control
Iam having problem creating postgresql function in plperl CREATE OR REPLACE FUNCTION zm_json (TEXT,
I'm having a problem creating a layout that's partly liquid. The layout has to
I'm currently creating an AD script that can get the AD groups of one
I'm having a problem with creating shortcuts at versions of WindowsXP that aren't in
I'm having a problem creating an function similar to the nub function. I need
I'm having a problem with a generic class I'm creating in Scala. I have

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.