Hello all. I’ve a MongoDB collection as given below -
[
{
"_id": {
"$oid": "658bb87b0a91d2c6ddb8165b"
},
"tenant_code": "11111111111111",
"tenant_id": "xyz",
"name": "root",
"folder_id": "74d97cdf-faef-4bef-9d6e-3e7a5f89db44",
"type": "Folder",
"parent_id": null,
"created_by": "",
"created_on": "2023-12-27 05:39:07.773386",
"children": []
},
{
"_id": {
"$oid": "658bbb78b6deb1a304763712"
},
"name": "Module 1",
"is_private": false,
"type": "Module",
"parent_id": "74d97cdf-faef-4bef-9d6e-3e7a5f89db44",
"children": [],
"folder_id": "dd276945-aaf9-4f31-ada0-b7a64945adac",
"created_by": "",
"created_on": "2023-12-27 05:51:52.472372",
"updated_by": "",
"updated_on": ""
},
{
"_id": {
"$oid": "658bbbef9fcdc9c083618c43"
},
"name": "Module 2",
"is_private": true,
"type": "Module",
"parent_id": "74d97cdf-faef-4bef-9d6e-3e7a5f89db44",
"children": [],
"folder_id": "cfe9627f-e5e9-460f-a1df-a3ccf4b28222",
"created_by": "",
"created_on": "2023-12-27 05:53:51.666959",
"updated_by": "",
"updated_on": ""
},
{
"_id": {
"$oid": "658bbdb44326f526ab9ec95b"
},
"name": "Module 2.1",
"is_private": true,
"type": "Module",
"parent_id": "cfe9627f-e5e9-460f-a1df-a3ccf4b28222",
"children": [],
"folder_id": "86522453-e44b-4134-9570-543c861b5786",
"created_by": "",
"created_on": "2023-12-27 06:01:24.286449",
"updated_by": "",
"updated_on": ""
},
{
"_id": {
"$oid": "658bbdb44326f526ab9ec95c"
},
"name": "Module 2.1.1",
"is_private": true,
"type": "Module",
"parent_id": "86522453-e44b-4134-9570-543c861b5786",
"children": [],
"folder_id": "b60cee64-db32-4e3b-9494-71431270650e",
"created_by": "",
"created_on": "2023-12-27 06:01:24.288442",
"updated_by": "",
"updated_on": ""
},
{
"_id": {
"$oid": "658bbfba81c62f1c1c9b4d18"
},
"name": "Module 2.1.1.1",
"is_private": true,
"type": "Module",
"parent_id": "b60cee64-db32-4e3b-9494-71431270650e",
"children": [],
"folder_id": "df8d7f22-6026-4859-8528-3379ccd14e2f",
"created_by": "",
"created_on": "2023-12-27 06:10:02.222466",
"updated_by": "",
"updated_on": ""
},
{
"_id": {
"$oid": "658bc7c181c62f1c1c9b4d19"
},
"name": "Module 1.1",
"is_private": true,
"type": "Module",
"parent_id": "dd276945-aaf9-4f31-ada0-b7a64945adac",
"children": [],
"folder_id": "e648fd94-32f0-401d-ab75-09fa61a39e0f",
"created_by": "",
"created_on": "2023-12-27 06:44:17.986558",
"updated_by": "",
"updated_on": ""
}
]
I’m dealing with a flat architecture to store all parent and child documents in a single collection. Where children and parents are related using the parents’ folder_id as children’s parent_id.
I’ve tried with a graph lookup to get all children in nested dict like object as shown below -
db.collection.aggregate([
{
"$match": {
"folder_id": "74d97cdf-faef-4bef-9d6e-3e7a5f89db44"
}
},
{
"$graphLookup": {
from: "collection",
startWith: "$folder_id",
connectFromField: "folder_id",
connectToField: "parent_id",
as: "children",
depthField: "depth"
}
}
])
But it returns all children nested under only the matched folder_id. I also tried handling in my code where I tried to create a flattened list of parent child object and recursively tried creating the tree, but that too seems much complicated and not getting my expected result.
Looking for a solution to return the tree structure in native Mongo way. Thank you