A newer version of this documentation is available.

View Latest

Function: Advanced DECREMENT Operation

    Couchbase Server Enterprise Edition 7.6 reaches End of Life on March 31, 2027. Please upgrade to 8.0.x by following Upgrade, or contact Couchbase Support.

    Perform the Advanced DECREMENT operation where Eventing interacts with the Data Service.

    The advancedDecrementOp function:

    • Performs the Advanced DECREMENT operation

    • Requires Eventing Storage (or a metadata collection) and a source collection

    • Requires a binding of type bucket alias

    • Operates on any mutation

    • Counts down the mutations subject to DCP deduplication

    For more information about the Advanced Self-Recursion Parameter, see Advanced DECREMENT Operation.

    • advancedDecrementOp

    • Input data

    • Output data

    • Output log

    // Configure the settings for the advancedDecrementOp function 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("dec:")) return;
        // log('input meta', meta);
        // log('input doc ', doc);
    
        // Creates doc.count if it does not already exist
        var ctr_meta = {"id": "my_atomic_counter:1" };
        var result = couchbase.decrement(src_col,ctr_meta);
        if (result.success) {
            log('success adv. decrement: result',result);
        } else {
            log('failure adv. decrement: id',ctr_meta.id,'result',result);
        }
    }
    UPSERT INTO `bulk`.`data`.`source` (KEY,VALUE)
      VALUES ( "dec:1",  {"somekey":"someValue"} ),
      VALUES ( "dec:2",  {"somekey":"someValue"} ),
      VALUES ( "dec:3",  {"somekey":"someValue"} ),
      VALUES ( "dec:4",  {"somekey":"someValue"} ),
      VALUES ( "dec:5",  {"somekey":"someValue"} ),
      VALUES ( "dec:6",  {"somekey":"someValue"} ),
      VALUES ( "dec:7",  {"somekey":"someValue"} ),
      VALUES ( "dec:8",  {"somekey":"someValue"} ),
      VALUES ( "dec:9",  {"somekey":"someValue"} ),
      VALUES ( "dec:10", {"somekey":"someValue"} );

    Insert 10 documents and count the mutations through decrementing.

    KEY: my_atomic_counter:1
    
    {
      "count": -10
    }
    2021-01-08T12:30:59.910-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-1},"meta":{"id":"my_atomic_counter:1","cas":"1610137859908960256"},"success":true}
    
    2021-01-08T12:30:59.910-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-2},"meta":{"id":"my_atomic_counter:1","cas":"1610137859909222400"},"success":true}
    
    2021-01-08T12:30:59.911-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-3},"meta":{"id":"my_atomic_counter:1","cas":"1610137859911385088"},"success":true}
    
    2021-01-08T12:30:59.931-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-4},"meta":{"id":"my_atomic_counter:1","cas":"1610137859929997312"},"success":true}
    
    2021-01-08T12:30:59.931-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-5},"meta":{"id":"my_atomic_counter:1","cas":"1610137859930193920"},"success":true}
    
    2021-01-08T12:30:59.932-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-6},"meta":{"id":"my_atomic_counter:1","cas":"1610137859932160000"},"success":true}
    
    2021-01-08T12:30:59.932-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-7},"meta":{"id":"my_atomic_counter:1","cas":"1610137859932356608"},"success":true}
    
    2021-01-08T12:30:59.948-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-8},"meta":{"id":"my_atomic_counter:1","cas":"1610137859946381312"},"success":true}
    
    2021-01-08T12:30:59.948-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-9},"meta":{"id":"my_atomic_counter:1","cas":"1610137859946708992"},"success":true}
    
    2021-01-08T12:30:59.948-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":-10},"meta":{"id":"my_atomic_counter:1","cas":"1610137859948412928"},"success":true}