Increment Counter in a Map

For Collection Datatypes map/list/set, + and - operator is used to append or remove the <key, value> entries from the collection. Also, we cannot use counter datatype in collections, every time you try to update counter will just be re-initialized.

UPDATE counter_test SET mapcounter['c1'] = + 1 WHERE id = 'a1';
id | mapcounter ----+------------ a1 | {'c1': 1}

UPDATE counter_test SET mapcounter['c1'] = + 2 WHERE id = 'a1';
id | mapcounter ----+------------ a1 | {'c1': 2}

You will need to handle the counter increment on the application side.

Please refer to docs here -

  1. https://docs.yugabyte.com/latest/api/ycql/type_collection/#collection-expressions
  2. https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/cqlUpdate.html#cqlUpdate__assignment
1 Like