How to get a node of nested arrays without knowing the deep

Hi guys,

I have searched in this forum but I didn’t find any answer for my problem :frowning:
What I need is to find a node within a nested n-level array, by knowing the id.

"children": [
	{
	  "children": [
		{
		  "children": [
			{
			  "children": [],
			  "id": 8,
			},
			{
			  "children": [],
			  "id": 9,
			}
		  ],
		  "id": 5,
		},
		{
		  "children": [],
		  "id": 7,
		}
	  ],
	  "id": 2,
	},
	{
	  "children": [],
	  "id": 3,
	},
	{
	  "children": [],
	  "id": 4,
	}
]I 

It is, I would like to find a node inside my document (for instance node with id=4), independently of the level, only knowing the id.
I am not really sure if it is possible in CB, but It would be fine if someone solve my doubt :wink:

Thanks!

There should be a way to do with N1QL, but I’m not an expert in that area so I don’t know off the top of my head exactly how :wink:

However, you’re data structure looks a bit curious to me - it appears you’re trying to represent an arbitrary tree, but not with a simple nested dictionary. Perhaps you could restructure to directly represent the tree using the JSON document structure? - for example:

{
  "2": {
    "5": {
      "8": null,
      "9": null
    },
    "7": null
  },
  "3": null,
  "4": null
}

That would allow you to directly lookup nodes by path using the Sub-Document API - for example “2.5” would refer to the following sub-document:

{
      "8": null,
      "9": null
},

SELECT FIRST v FOR v WITHIN children WHEN v.id = 4 END FROM default;