1

What I want to do is for every customer add a property named orders with the datatype array<Order>[] and move all Orders associated with a Customer into this property. In the target entity type and then delete the redundant property orders.person id afterwards.

I have done this so far:

MERGE INTO `Customers` AS cust
USING `Orders` AS ord ON cust.id = ord.personId

WHEN MATCHED THEN
    UPDATE 
        SET cust.orders = {"order_id": ord.id, "person_id": ord.person_id, "total_price": ord.total_price}
    WHERE cust.id = ord.PersonId
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • I am getting the Error which says this: { "code": 5320, "msg": "Multiple UPDATE/DELETE of the same document (document key '46aef9cc-7068-4a4b-b0b3-b83f65820c46') in a MERGE statement" } – Umair Qureshi Jun 04 '21 at 23:29

1 Answers1

0

Target row can't be mutated repeatedly.

MERGE INTO `Customers` AS cust
USING  (SELECT ord.personId, ARRAY_AGG({"order_id": ord.id,  "total_price": ord.total_price}) AS orders
        FROM `Orders` AS ord
        WHERE ........
        GROUP BY ord.personId) AS s
 ON cust.id = s.personId
WHEN MATCHED THEN UPDATE SET cust.orders = s.orders
vsr
  • 7,149
  • 1
  • 11
  • 10