Aggregate and Group the inner doucment array by the id

50 views
Skip to first unread message

Soorya Prakash

unread,
Oct 30, 2015, 9:07:05 AM10/30/15
to mongodb-user, boopathi s, Baskaran R
Hi,
 I am using the mongo db 2.2.X  version and implementing the aggregate function to get the below result from the available document in the collection.


The documents in the collection looks like below

    {
        "_id": "A1",
        "children": [
            "C1",
            "C2",
            "C3"
        ],
        "name": "A1"
    },
    {
        "_id": "A2",
        "children": [
            "C1",
            "C2"
        ],
        "name": "A2"
    },
    {
        "_id": "C1",
        "children": [],
        "name": "C1"
    },
    {
        "_id": "C2",
        "children": [],
        "name": "C2"
    },
    {
        "_id": "C3",
        "children": [],
        "name": "C3"
    }

The result i am trying to get like below
[
    {
      "_id" : "A1",
      "name" : "A1",
      "Children" : [
        {
        "_id" : "C1",
         "children" : [],
         "name" : "C1"
        },
        {
         "_id" : "C2",
         "children" : [],
         "name" : "C2"
        },
        {
         "_id" : "C3",
         "children" : [],
         "name" : "C3"
        }
       ]
    },
        {
      "_id" : "A2",
      "name" : "A2",
      "Children" : [
        {
        "_id" : "C1",
         "children" : [],
         "name" : "C1"
        },
        {
         "_id" : "C2",
         "children" : [],
         "name" : "C2"
        }
       ]
    }
]


Is it possible to t=get the result like above using my current version of mongo. If so can anyone help me how can i achieve this result?

 

Wan Bachtiar

unread,
Nov 4, 2015, 5:18:49 PM11/4/15
to mongodb-user, boopathi...@gmail.com, baska...@gmail.com

Is it possible to get the result like above using my current version of mongo. If so can anyone help me how can i achieve this result?

Hi Soorya,

As of MongoDB v3.0, there is no operator in the aggregation framework that would be able to nest the documents from the same collection. I would suggest to write a script to achieve the result as you have described.

A simple MongoDB JavaScript example for a fixed depth levels of 2 would be:

/* For each document in the collection */
db.collection.find({}).forEach(function(doc){
        var children = [];

        /* For each child in the children field */
        doc.children.forEach(function(key){
                var tmp = db.collection.findOne({_id:key});
                children.push(tmp);
        });
        /* Substitute children field with the newly constructed children field */
        doc.children = children;

        /* Only print document with children */
        if(children.length > 0) printjson(doc);
});

You would need a recursive function if the levels depth is unknown.

There are a number of example patterns in MongoDB documentation to Model Tree Structures that you might find useful. i.e. data model referencing.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages