2

I am trying to get the first element of the object scan. In my case, the first element's key changes. So I cannot call it with the key. Here is the AQL query I'm using, which is not working.

`FOR d in collection RETURN DISTINCT Object.keys(d.out.scan)[0]`

Object structure:

{
  "out": {
       "scan":{
             "someKeyThatChanges":"someValue"
              }
        }
}

Is there a way to fetch the first key of scan?

Thank you

DeSpider
  • 323
  • 2
  • 16

2 Answers2

2

The relevant AQL functions for this issue are documented at https://docs.arangodb.com/3.3/AQL/Functions/Document.html

In brief, if the object has only one user-defined key, then you will be able to use VALUES(_, true) directly.

Otherwise, you could use ATTRIBUTES() to get an array of the object's attributes. You may want to filter it to avoid keys with names that start with "_". Once you've selected a key, remember:

Attributes can also be accessed using the [] accessor

... the square brackets allow for expressions:

... u[attr1][0][attr2][ CONCAT("fir", "st") ]

Demo

LET x = {
  "out": {
       "scan":{
             "someKeyThatChanges":"someValue"
              }
        }
}

LET y = x.out.scan
LET z = y[ ATTRIBUTES(y)[0] ]
RETURN z
peak
  • 105,803
  • 17
  • 152
  • 177
1

To fetch just the name of the first key of out.scan, the following will work:

FOR d IN collection
  RETURN ATTRIBUTES(d.out.scan)[0]

For returning the mapped value for that key, please refer to the other answer given.

stj
  • 9,037
  • 19
  • 33