Adding new sub-array based on another sub-array

Using Couchbase Server, 6.6…
How can I add a new sub-array to a document that is based loosely on an existing array?
Given a document such as the following that already has an ‘asdf’ array property,

[
  {
    "beer-sample": {
      "abv": 9.8,
      "asdf": [
        "hello",
        "goodbye"
      ],
      "brewery_id": "21st_amendment_brewery_cafe",
      "category": "North American Ale",
      "description": "Deep, golden,...in Hayward, California.",
      "ibu": 0,
      "name": "Double Trouble IPA",
      "srm": 0,
      "style": "Imperial or Double India Pale Ale",
      "type": "beer",
      "upc": 0,
      "updated": "2010-07-22 20:00:20"
    }
  }
]

… how can I get the same document but with the added ‘asdf2’ array?

[
  {
    "beer-sample": {
      "abv": 9.8,
      "asdf": [
        "hello",
        "goodbye"
      ],
      "asdf2": [
        "hello all!",
        "goodbye all!"
      ],
      "brewery_id": "21st_amendment_brewery_cafe",
      "category": "North American Ale",
      "description": "Deep, golden,...in Hayward, California.",
      "ibu": 0,
      "name": "Double Trouble IPA",
      "srm": 0,
      "style": "Imperial or Double India Pale Ale",
      "type": "beer",
      "upc": 0,
      "updated": "2010-07-22 20:00:20"
    }
  }
]

I have tried the following, but it gives me an sub-document with an extra layer.

UPDATE `beer-sample` bs1
SET bs1.asdf2 = (
    SELECT ARRAY  var1 || ' all!' FOR var1 IN bs2.asdf END AS anExtraLayer
    FROM `beer-sample` bs2
    WHERE META().id = "21st_amendment_brewery_cafe-double_trouble_ipa")
WHERE META().id = "21st_amendment_brewery_cafe-double_trouble_ipa"

@malclear

UPDATE `beer-sample` AS b
SET b.asdf2 = ARRAY v || " all!"  FOR v IN b.asdf END
WHERE IS_ARRAY(b.asdf) AND ......

If you don’t add IS_ARRAY(b.asdf) predicate and asdf is not ARRAY it will set to NULL.

OR

UPDATE `beer-sample` AS b
SET b.asdf2 = IFNULL(ARRAY v || " all!"  FOR v IN b.asdf END, MISSING)
WHERE .....

OR

UPDATE `beer-sample` AS b
SET b.asdf2 =  (SELECT RAW  bs || " all!" FROM b.asdf AS bs ) 
WHERE  .......
1 Like

It seems that “SELECT RAW” is deprecated now? I can choose from one of the top two.

@malclear ,
It used quite often, will not be deprecated. Did you see some where in documentation.
One use case right side of IN clause is subquery want to generate ARRAY of values vs OBJECTS.

For historical reasons the query language also allows the keywords ELEMENT or RAW to be used in place of VALUE (not recommended).

RAW or VALUE or ELEMENT is supported by N1QL for query also.

https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/selectclause.html#section_Syntax