Abdulsamet ILERI
Abdulsamet ILERI

Reputation: 165

How to multiple update a nested array within document at couchbase?

I have a bucket called chat which includes document model like

{
  "id": "50542400778551298_5001_abcdef",
  "customer": {
    ...
  },
  "seller": {
    ...
  },
  "complaints": [],
  "messages": [
    {
      "id": "2828911d-b96f-4b90-a52c-252d0324ab69",
      "owner": "SELLER",
      "content": "Merhabalar",
      "readingDate": null,
      "createdAt": "2022-06-27T09:16:42.49158+03:00"
    },
    {
      "id": "8cd8c4b7-d599-4dfa-9fa7-be304a3b72b9",
      "owner": "SELLER",
      "content": "İlan hakkında detay bilgi verir misin?",
      "readingDate": null,
      "createdAt": "2022-06-27T09:17:43.329741+03:00"
    },
    {
      "id": "4f0ae912-7d65-4e79-9058-2b048c4d9845",
      "owner": "SELLER",
      "content": "Huhuu",
      "readingDate": null,
      "createdDate": "2022-06-27T09:18:37.491531+03:00"
    },
    {
      "id": "d2dd61b3-7ff1-4139-a411-9a917c00d4f3",
      "owner": "SELLER",
      "content": "Orada kimse var mı?",
      "readingDate": null,
      "createdAt": "2022-06-27T09:18:45.917564+03:00"
    },
    {
      "id": "ea2d8dca-2f8c-4987-9c07-7515a6a0063f",
      "owner": "SELLER",
      "content": "Döner misininiz?",
      "readingDate": null,
      "createdAt": "2022-06-27T09:18:52.80337+03:00"
    }
  ],
  "status": "ACTIVE"
}

I want to update all messages readingDate a specific time like (2022-06-27T09:18:52.80337+03:00)

But I cannot find an easy way to accomplish this task.

I tried to write an N1QL like

UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()
FROM chat AS c 
WHERE META().id = '50542400778551298_5001_abcdef';

but it is not working and gives error

[
  {
    "code": 3000,
    "msg": "syntax error - at SET",
    "query": "UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()\nFROM chat AS c \nWHERE META().id = '50542400778551298_5001_abcdef';"
  }
]

I saw https://docs.couchbase.com/server/current/guides/bulk-operations.html.

As far as to my understanding, the general solution gets all messages and updates them based on cas.

But maybe I believe there is an easy way so I wanted to ask the StackOverflow community.

Upvotes: 1

Views: 302

Answers (1)

Abdulsamet ILERI
Abdulsamet ILERI

Reputation: 165

Thanks to this question I wrote n1ql like

UPDATE chat USE KEYS '50542400778551298_5001_abcdef'
SET c.readAt = NOW_LOCAL() FOR c IN messages END
RETURNING messages;

Upvotes: 3

Related Questions