# Increment Counter in a Map

**URL:** <https://forum.yugabyte.com/t/increment-counter-in-a-map/759>\
**Category:** General\
**Created:** [July 10, 2020, 5:30pm UTC](https://forum.yugabyte.com/t/increment-counter-in-a-map/759 "2020-07-10T17:30:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![llKetanll](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/llketanll/32/241_2.png) [@llKetanll](https://forum.yugabyte.com/u/llKetanll)\
**Post date:** [July 10, 2020, 5:30pm UTC](https://forum.yugabyte.com/t/increment-counter-in-a-map/759/1 "2020-07-10T17:30:27Z")

</div>

In `YCQL` how can we increment a counter inside a MAP?

For example: `UPDATE a SET m['k'] = m['k'] + 1 WHERE...`  
The usual increment/decrement operation for a counter in the map produces this error `Operator not allowed as right hand value`

---

<div class="post-metadata">

**Author:** ![nmalladi](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/nmalladi/32/236_2.png) [@nmalladi](https://forum.yugabyte.com/u/nmalladi)\
**Post date:** [July 10, 2020, 6:28pm UTC](https://forum.yugabyte.com/t/increment-counter-in-a-map/759/2 "2020-07-10T18:28:05Z")

</div>

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](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](https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/cqlUpdate.html#cqlUpdate__assignment)

---

<div class="post-metadata">

**Author:** ![Oleg](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/oleg/32/79_2.png) [@Oleg](https://forum.yugabyte.com/u/Oleg)\
**Post date:** [July 10, 2020, 6:46pm UTC](https://forum.yugabyte.com/t/increment-counter-in-a-map/759/3 "2020-07-10T18:46:49Z")

</div>

@llKetanll  
At the moment `map['key']` is not supported as a right-hand-side expression in CQL (YCQL or Apache Cassandra).  
For example, in Apache Cassandra the test like this gives the following error:

```
cqlsh:k> update t1 set m['k'] = 3 where id=1;
cqlsh:k> select * from t1;
 id | m
----+----------
  1 | {'k': 3}
(1 rows)
cqlsh:k> update t1 set m['k'] = m['k'] where id=1;
SyntaxException: line 1:24 no viable alternative at input '[' (... set m['k'] = [m][...)
cqlsh:k> update t1 set m['k'] = m['k']+1 where id=1;
SyntaxException: line 1:24 no viable alternative at input '[' (... set m['k'] = [m][...)

```

As an alternative way you can use common columns (with using of the `map-key` as a clustering column for better performance):

```
ycqlsh:k> create table t2 (id int, m_key text, m_val int, primary key((id), m_key));
ycqlsh:k> insert into t2 (id, m_key, m_val) values (100, 'k', 1);
ycqlsh:k> select * from t2;
 id | m_key | m_val
-----+-------+-------
 100 | k | 1
(1 rows)
ycqlsh:k> update t2 set m_val = m_val + 1 where id = 100 and m_key = 'k';
ycqlsh:k> select * from t2;
 id | m_key | m_val
-----+-------+-------
 100 | k | 2
(1 rows)

```
