Concurrency query

I have a problem with REST parallel queries. In my function I use code like

const res = col.query(‘SELECT data, meta().cas FROM test WHERE meta().id = “123”’);
const doc = res.rows[0];
doc.data[req.params,type] = {“position”: req.params.position};
col.upsert(‘123’, doc, {cas: doc.cas});

But if many users tried to send data to same ID , data do not saving OR some time cas mismatch error
What is best practice to upsert to one document from many queries?

I found solution like this
try{
await sampleColl.insert(‘123’, doc );
}
catch(e){

        if(e.cause.code !== 305) // if error !-  ID exist
          throw e;
      }

      await  sampleColl.mutateIn("123", [
      couchbase.MutateInSpec.upsert(req.params.type,   req.params.position ),
      ]);

@ericb can you please help here ?