Database Reference
In-Depth Information
Discrete list manipulation
Lists, like sets, allow us to discretely add and remove elements without reading the existing
contents or overwriting the entire collection. However, since elements in a list have a
defined order, we have many more ways to specify what we'd like to add or remove, and
where in the list we'd like it to go.
One of the simplest operations we can do is to append an element to the end of a list. This
looks just like adding an element to a set, correcting for the different literal syntax:
UPDATE "user_status_updates"
SET "shared_by" = "shared_by" + ['carol']
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
This append operation specifically instructs Cassandra to put carol at the end of the list.
If we want to add a value to the beginning of the list instead, we simply flip the order of the
values on the right side of the assignment:
UPDATE "user_status_updates"
SET "shared_by" = ['dave'] + "shared_by"
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
Let's check the current contents of the list to see the effects of our operations:
The order of the values of the list is exactly what we asked for: dave at the beginning;
carol at the end, and bob , the initial sole occupant of the list, taking up the middle.
Writing data at a specific index
What if we want to replace the data at a specific place in the list, rather than adding it to the
beginning or the end? We can perform a direct assignment to the index we want as follows:
Search WWH ::




Custom Search