Selecting distinct tags from documents

n1ql

#1

I have document structure as follows:

doc1:
{
    "Tag": [
        {
            "Text": "tag1"
        },
        {
            "Text": "tag2"
        }
    ],
    "Type:" : "type1",
}

doc2:
{
    "Tag": [
        {
            "Text": "tag2"
        },
        {
            "Text": "tag3"
        }
    ],
    "Type:" : "type1",
}

doc3:
{
    "Tag": [
        {
            "Text": "tag5"
        },
    ],
    "Type:" : "type2",
}

And I want to achieve all tags of the documents (with given type) as a string with a query like for type 1, result would be:

[
    "tag1",
    "tag2",
    "tag3"
]

I’ve tried some queries but couldn’t get any result, like

SELECT DISTINCT ARRAY tag.Text FOR tag IN b.Tag END AS tags
FROM `bucket1` AS b
WHERE b.Type == "type1"

Returns documents with this structure which I couldn’t join the returning arrays and I don’t know where to go from here:

[
    {
        "tags": [
            "tag1",
            "tag2"
        ]
    },
    {
        "tags": [
            "tag2",
            "tag3"
        ]
    }
]

Thank you.


#2
SELECT DISTINCT RAW tag.Text
FROM `bucket1` AS b
UNNEST b.Tag AS tag
WHERE b.Type == "type1";

#3

It works like this, thank you. I didn’t know about the RAW selection. Also

SELECT DISTINCT RAW tag.Text
FROM `bucket1` AS b
UNNEST b.Tag AS tag
WHERE b.Type == "type1";

Returns a list of strings which is better in my case.