PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How can I create the below specify output?

 May 09, 2022     product     No comments   

Issue

Hello Below I have specify the data which is to be used and get an output which I have mentioned below, Can you please help me on that as I tried alot but couldn't find a way.

//This is the data
let data = [
  {'name': 'A', 'parent': null},
  {'name': 'B', 'parent': null},
  {'name': 'C', 'parent': 'A'},
  {'name': 'D', 'parent': 'A'},
  {'name': 'E', 'parent': 'D'},
  {'name': 'F', 'parent': 'D'},
  {'name': 'G', 'parent': 'B'},
  {'name': 'H', 'parent': 'B'},
 ];

//And want the output like this by using mongodb aggregation
{
 "null": [
  {
   "A": [
    {
     "C": []
    },
    {
     "D": [
      {
       "E": []
      },
      {
       "F": []
      }
     ]
    }
   ]
  },
  {
   "B": [
    {
     "G": []
    },
    {
     "H": []
    }
   ]
  }
 ]
}

I have tried using graphlookup aggregation but couldn't find a way out of it.

Thanks for your help !


Solution

Solution in Mongoplayground.

Try this:

db.collection.aggregate([
    {
        $lookup: {
            from: "collection",
            let: { name: "$name" },
            pipeline: [
                {
                    $match: {
                        $expr: { $eq: ["$parent", "$$name"] }
                    }
                },
                {
                    $lookup: {
                        from: "collection",
                        let: { name: "$name" },
                        pipeline: [
                            {
                                $match: {
                                    $expr: { $eq: ["$parent", "$$name"] }
                                }
                            },
                            {
                                $replaceRoot: {
                                    newRoot: {
                                        $arrayToObject: [[{ k: "$name", v: [] }]]
                                    }
                                }
                            }
                        ],
                        as: "children"
                    }
                },
                {
                    $replaceRoot: {
                        newRoot: {
                            $arrayToObject: [[{ k: "$name", v: "$children" }]]
                        }
                    }
                }
            ],
            as: "children"
        }
    },
    {
        $match: {
            children: { $ne: [] },
            parent: null
        }
    },
    {
        $group: {
            _id: "$parent",
            array: {
                $push: {
                    array: [{ k: "$name", v: "$children" }]
                }
            }
        }
    },
    {
        $addFields: {
            array: {
                $map: {
                    input: "$array",
                    as: "item",
                    in: { $arrayToObject: "$$item.array" }
                }
            }
        }
    },
    {
        $replaceRoot: {
            newRoot: { $arrayToObject: [[{ k: "null", v: "$array" }]] }
        }
    }
]);

Output:

{
    "null" : [
        {
            "A" : [
                {
                    "C" : [ ]
                },
                {
                    "D" : [
                        {
                            "E" : [ ]
                        },
                        {
                            "F" : [ ]
                        }
                    ]
                }
            ]
        },
        {
            "B" : [
                {
                    "G" : [ ]
                },
                {
                    "H" : [ ]
                }
            ]
        }
    ]
}


Answered By - Dheemanth Bhat
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing