Nested document - Query Data

Dears

We have JSON documents in following structure in couchbase

{
“a”: “value 1”,
“b”: “value 2”,
“childs”: [{
“a”: “value 3”,
“b”: “value 4”
},
{
“a”: “value 5”,
“b”: “value 6”,
“childs”: [{
“a”: “value 7”,
“b”: “value 8”
}]
}]
}

It is a repetitive nested structure where the attributes of parent and child are same
Need an N1QL to be able fetch the values of all those elements with attribute name as “a”

Is there a regex expression that we can have in SELECT query to go through the entire document and get the results?
Result neednt be in the same parent child hierarchy but can be a flat

Expectation is that we have something like this

{
“a”: “value 1”
“a”: “value 3”
“a”: “value 5”
“a”: “value 7”
}

Regards
Srinivas

You can’t get your desired format because object fields must be unique. If not unique previous one will be overwritten.
You can get as array of values

SELECT ARRAY v.a FOR v WITHIN {d} WHEN v.a IS NOT MISSING END AS a
FROM <bucketname> AS d;

SELECT ARRAY v.a FOR v WITHIN {d} WHEN v.a IS NOT MISSING END AS a FROM { "a": "value 1", "b": "value 2", "childs": [{ "a": "value 3", "b": "value 4" }, { "a": "value 5", "b": "value 6", "childs": [{ "a": "value 7", "b": "value 8" }] }] } AS d;
{
    "requestID": "3caa1f13-67c1-4285-b2da-d951bb39e080",
    "signature": {
        "a": "array"
    },
    "results": [
    {
        "a": [
            "value 1",
            "value 3",
            "value 5",
            "value 7"
        ]
    }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "1.990673ms",
        "executionTime": "1.937231ms",
        "resultCount": 1,
        "resultSize": 123
    }
}

Thanks @vsr1 n array would help and that should do the job.
Some new key words to learn in this query :slight_smile: