How to update an array in fauna

I have an array as a field in a document in fauna that I need to append a number to. See document below. Right now, my query (also below) just erases the old value instead of appending to it.

"data": {
      "name": "nine",
      "stars": [
        1
      ]
    }

await client.query(
  q.Update(
    q.Ref(q.Collection('test'), `${doc_id`),
    { data: { {stars:[1] } },
  )
)
1 Like

Hi @lmb and welcome,

to append elements to an array, you should:

Let(
  {
    ref:Ref(Collection("appendArray"), "281066568786379269"),
    doc:Get(Var('ref')),
    array:Select(['data','stars'],Var('doc'))
  },
  Update(Var('ref'),{data:{stars:Append([2],Var('array'))}})
)

Hope this helps.

Luigi

3 Likes