When running a query i get an error that only happens in test environment but not in dev environment

MERGE INTO order as o USING 
(SELECT 'order::' || h.orderId  AS id, h.date FROM history AS h WHERE   IS_STRING(h.orderId)   ) AS h 
ON KEY  h.id 
WHEN MATCHED THEN UPDATE SET o.paidDate = h.date

I am running this query and it works fine in my Dev environment. My dev environment has less records for one than my test environment, 5 to 71. However when i run this on my test environment i get a
{
“code”: 12009,
“msg”: “DML Error, possible causes include CAS mismatch or concurrent modificationFailed to perform update -
cause: MCResponse status=KEY_EEXISTS, opcode=SET, opaque=0, msg: Data exists for key”
}

I am trying to get more info on what causes this but figure i try the forum where experts reside. How do i fix the problem and what is the cause

Multiple updates might be going on same document. Try this.

MERGE INTO order as o USING 
(SELECT 'order::' || h.orderId  AS id,  MAX(h.date) AS date  FROM history AS h WHERE   IS_STRING(h.orderId)   GROUP BY  'order::' || h.orderId ) AS h 
ON KEY  h.id 
WHEN MATCHED THEN UPDATE SET o.paidDate = h.date