Database Reference
In-Depth Information
Serializing the collection
One approach is to simply store the list of users in a text column in some serialized form.
JSON is a versatile serialization format for such scenarios, so we'll use it. First, we'll add
the column to the
user_status_updates
table, recalling the technique from
Adding
columns to tables
section in
Chapter 7
,
Expanding Your Data Model
:
ALTER TABLE "user_status_updates"
ADD "starred_by_users" text;
Simple enough. Now let's suppose that
bob
wants to star one of the status updates of
alice
. From the application's standpoint, this means appending
bob
to the list of users
who have starred
alice
's update. First, we'll read the existing list:
SELECT "starred_by_users"
FROM "user_status_updates"
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
We see that there's currently no value in the
starred_by_users
column, which we can
just interpret as an empty list:
Since the column was empty before, we know that the new value of the column should just
be a one-element array containing
bob
. We can update the status update row accordingly:
UPDATE "user_status_updates"
SET "starred_by_users" = '["bob"]'
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
Note that the value in the CQL above is simply a string literal containing a JSON represent-
ation of the array
["bob"]
. From Cassandra's standpoint, it is simply text.