---
title: "Function: Advanced REPLACE operation"
description: Perform the Advanced REPLACE operation where Eventing interacts
  with the Data service.
pubDate: 2026-08-17T09:53:44.266Z
antora:
  editUrl: https://github.com/couchbase/docs-server/edit/release/7.2/modules/eventing/pages/eventing-handler-advancedReplaceOp.adoc
  xref: xref:7.2@server:eventing:eventing-handler-advancedReplaceOp.adoc[]
---

[Consult the llms.txt file for a full list of contents](/llms.txt)
[View original HTML](/server/7.2/eventing/eventing-handler-advancedReplaceOp.html)

# Function: Advanced REPLACE operation

**Goal**: Perform the Advanced REPLACE operation where Eventing interacts with the Data service.

* This function **advancedReplaceOp** merely demonstrates the Advanced REPLACE operation.
* Requires Eventing Storage (or metadata collection) and a "source" collection.
* Needs a Binding of type "bucket alias" (as documented in the Scriptlet).
* Will operate on any mutation where doc.type === "control\_adv\_replace".
* Always tries to insert the test document, any insert error will be ignored.
* There are 3 modes of operation: no\_cas, bad\_cas, and good\_cas.
* For more information refer to [Advanced REPLACE operation](eventing-advanced-keyspace-accessors.md#advanced-replace-op) in the detailed documentation.

* advancedReplaceOp
* Input Data/Mutation
* Output Data
* Output Log

```javascript
// To run configure the settings for this Function, advancedReplaceOp, as follows:
//
// Version 7.1+
//   "Function Scope"
//     *.* (or try bulk.data if non-privileged)
// Version 7.0+
//   "Listen to Location"
//     bulk.data.source
//   "Eventing Storage"
//     rr100.eventing.metadata
//   Binding(s)
//    1. "binding type", "alias name...", "bucket.scope.collection", "Access"
//       "bucket alias", "src_col",       "bulk.data.source",        "read and write"
//
// Version 6.6.1
//   "Source Bucket"
//     source
//   "MetaData Bucket"
//     metadata
//   Binding(s)
//    1. "binding type", "alias name...", "bucket",     "Access"
//       "bucket alias", "src_col",       "source",     "read and write"

function OnUpdate(doc, meta) {
    if (!meta.id.startsWith("control_adv_replace")) return;

    log('input meta', meta);
    log('input doc ', doc);

    // Setup, make sure we have our doc to "replace", ignore any errors
    couchbase.insert(src_col,{"id":"test_adv_replace:" + doc.ins_id},{"a:": 1});

    var new_meta;
    if (doc.mode && doc.mode === "no_cas") {
        // Here we pass no CAS it will always succeed
        new_meta = {"id":"test_adv_replace:" + doc.ins_id};
        // optional set an expiry 60 seconds in the future
        // new_meta.expiry_date = new Date(Date.now() + 60 * 1000);
    }
    if (doc.mode && doc.mode === "bad_cas") {
        // Here we pass a non-matching CAS it will always fail
        new_meta = {"id":"test_adv_replace:" + doc.ins_id, "cas":"1111111111111111111"};
    }
    if (doc.mode && doc.mode === "good_cas") {
        // Here we will pass the matching or current CAS it will succeed
        var tmp_r = couchbase.get(src_col,{"id":"test_adv_replace:" + doc.ins_id});
        if (tmp_r.success) {
            // Here we use the current CAS just read via couchbase.get(...)
            new_meta = {"id":"test_adv_replace:" + doc.ins_id, "cas": tmp_r.meta.cas};
        } else {
            log('Cannot replace due to no such key',"test_adv_replace:" + doc.ins_id);
            return;
        }
    }
    var new_doc = {id: doc.ins_id, type: "test_adv_replace", random: Math.random()};
    var result = couchbase.replace(src_col,new_meta,new_doc);
    if (result.success) {
        log('success adv. replace: result',result);
    } else {
        log('failure adv. replace: id',new_meta.id,'result',result);
    }
}
```

```json
Mutation #1

INPUT: KEY control_adv_replace::1

{
  "id": 1,
  "type": "control_adv_replace",
  "ins_id": 10,
  "mode": "no_cas"
}

Mutation #2

INPUT: KEY control_adv_replace::2

{
  "id": 2,
  "type": "control_adv_replace",
  "ins_id": 10,
  "mode": "bad_cas"
}

Mutation #3

INPUT: KEY control_adv_replace::3

{
  "id": 3,
  "type": "control_adv_replace",
  "ins_id": 10,
  "mode": "good_cas"
}
```

We try to replace three (3) documents the first and last replacements succeed but the second, fails because of a CAS missmatch.

```json
KEY: test_adv_replace:10

{
  "id": 10,
  "type": "test_adv_replace",
  "random": 0.40223830137164085
}
```

```json
Logs from Mutation #1

2021-01-08T10:58:39.846-08:00 [INFO] "input meta"
{
    "cas": "1610131902064820224",
    "id": "control_adv_replace::1",
    "expiration": 0,
    "flags": 33554438,
    "vb": 417,
    "seq": 4
}
2021-01-08T10:58:39.846-08:00 [INFO] "input doc "
{
    "id": 1,
    "type": "control_adv_replace",
    "ins_id": 10,
    "mode": "no_cas"
}
2021-01-08T10:58:39.847-08:00 [INFO] "success adv. replace: result"
{
    "meta": {
        "id": "test_adv_replace:10",
        "cas": "1610132319847055360"
    },
    "success": true
}

Logs from Mutation #2

2021-01-08T10:59:04.151-08:00 [INFO] "input meta"
{
    "cas": "1610132344113397760",
    "id": "control_adv_replace::2",
    "expiration": 0,
    "flags": 33554438,
    "vb": 168,
    "seq": 3
}
2021-01-08T10:59:04.151-08:00 [INFO] "input doc "
{
    "id": 2,
    "type": "control_adv_replace",
    "ins_id": 10,
    "mode": "bad_cas"
}
2021-01-08T10:59:04.154-08:00 [INFO] "failure adv. replace: id" "test_adv_replace:10" "result"
{
    "error": {
        "code": 272,
        "name": "LCB_KEY_EEXISTS",
        "desc": "The document key exists with a CAS value different than specified",
        "cas_mismatch": true
    },
    "success": false
}

Logs from Mutation #3

2021-01-08T10:59:35.692-08:00 [INFO] "input meta"
{
    "cas": "1610132375634706432",
    "id": "control_adv_replace::3",
    "expiration": 0,
    "flags": 33554438,
    "vb": 943,
    "seq": 3
}
2021-01-08T10:59:35.692-08:00 [INFO] "input doc "
{
    "id": 3,
    "type": "control_adv_replace",
    "ins_id": 10,
    "mode": "good_cas"
}
2021-01-08T10:59:35.696-08:00 [INFO] "success adv. replace: result"
{
    "meta": {
        "id": "test_adv_replace:10",
        "cas": "1610132375695589376"
    },
    "success": true
}
```