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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:44:36+00:00 2026-06-07T01:44:36+00:00

I have a collection of entities, which represents a tree. Each entity has a

  • 0

I have a collection of entities, which represents a tree. Each entity has a property containing an array of attributes.

For example:

{
    "_id" : 1,
    "parent_id" : null,
    "attributes" : [ "A", "B", "C" ]
}

I would like to use MapReduce to generate another collection which is similar to the original collection, but for each item in the collection it not only contains the attributes directly associated with the entity, but also those of its ancestors, all the way up to the root of the hiearchy.

So given the following entities:

{
    "_id" : 1,
    "parent_id" : null,
    "attributes" : [ "A", "B", "C" ]
}

{
    "_id" : 2,
    "parent_id" : 1,
    "attributes" : [ "D", "E", "F" ]
}

{
    "_id" : 3,
    "parent_id" : 2,
    "attributes" : [ "G", "H", "I" ]
}

The result of the MapReduce job would be the following:

{
    "_id" : 1,
    "attributes" : [ "A", "B", "C" ]
}

{
    "_id" : 2,
    "attributes" : [ "A", "B", "C", "D", "E", "F" ]
}

{
    "_id" : 3,
    "attributes" : [ "A", "B", "C", "D", "E", "F", "G", "H", "I" ]
}

I’ve managed produce MapReduce jobs which do simple things like count the attributes for each entity but can’t get my head round how I might deal with a hierarchy. I am open to alternative ways of storing the data but don’t want to store the whole hierarchy in a single document.

Is this kind of thin possible with MapReduce in MongoDB or am I just thinking about the problem in the wrong way?

  • 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-07T01:44:37+00:00Added an answer on June 7, 2026 at 1:44 am

    Ok, so I don’t think this will be very performant/scalable, because you have to recursively find the parent ids from the child nodes. However, it does provide the output you want.

    var mapFunc = function(doc, id) {
      // if this is being invoked by mapReduce, it won't pass any parameters 
      if(doc == null) {
        doc = this; 
        id = this._id; 
      } else if (doc.parent_id != null) {
        // if this is a recursive call, find the parent
        doc = db.test.findOne({_id:doc.parent_id});
      }
      // emit the id, which is always the id of the child node (starting point), and the attributes
      emit(id, {attributes: doc.attributes}); 
      // if parent_id is not null, call mapFunc with the hidden parameters
      if(doc.parent_id != null) {
        // recursive mapFunc call
        mapFunc(doc, id); 
      } 
    }
    // since we're going to call this from within mapReduce recursively, we have to save it in the system JS
    db.system.js.save({ "_id" : "mapFunc", "value" : mapFunc});
    
    var reduceFunc = function(key, values) {
      var result = {attributes:[]}; 
      values.forEach(function(value) {
        // concat the result to the new values (I don't think order is guaranteed here)
        result.attributes = value.attributes.concat(result.attributes);
      }); 
      return result; 
    }
    
    // this just moves the attributes up a level
    var finalize = function(key, value) {return value.attributes};
    
    // quick test...
    db.test.mapReduce(mapFunc, reduceFunc, {out: {inline: 1}, finalize: finalize});
    

    Provides:

    "results" : [
        {
            "_id" : 1,
            "value" : [
                "A",
                "B",
                "C"
            ]
        },
        {
            "_id" : 2,
            "value" : [
                "A",
                "B",
                "C",
                "D",
                "E",
                "F"
            ]
        },
        {
            "_id" : 3,
            "value" : [
                "A",
                "B",
                "C",
                "D",
                "E",
                "F",
                "G",
                "H",
                "I"
            ]
        }
    ],
    "timeMillis" : 2,
    "counts" : {
        "input" : 3,
        "emit" : 6,
        "reduce" : 2,
        "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 an entity which has a collection as one of its attributes. That
I have a collection of custom entity objects one property of which is an
I have Class and Student objects. Both have collection of another as property. Which
I have a project containing POCO entities. A database context has been created for
I have a MessageThread entity which contains Message entities. The foreign key is set
I have entities: Tweet and User . Tweet has an author , which is
I have an Entity class which Implements IWeightable: Public Interface IWeightable Property WeightState As
I have an enumerable collection of entities which have come from Linq2Sql (they have
I have a situation where I'm adding existing entities to an entity collection. Before
I have a collection of object (Entity) which contains a sub-collection (Result is Double[]).

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.