Databases Reference
In-Depth Information
to have a different key for each line item or else they will end up getting stored together
as versions of the same key/value pair. For example, call the product name column
product_name_1
instead of calling all of them
product_name
.
The next example uses Redis to illustrate creating data in a key/value map.
Using the Create Operation in Key/Value Maps
Redis is a simple, yet powerful, data structure server that lets you store values as a simple key/value
pair or as a member of a collection. Each key/value pair can be a standalone map of strings or
reside in a collection. A collection could be any of the following types: list, set, sorted set, or hash.
A standalone key/value string pair is like a variable that can take string values.
You can create a Redis string key/value map like so:
./redis-cli set akey avalue
You can confi rm that the value is created successfully with the help of the
get
command as follows:
./redis-cli get akey
The response, as expected, is
avalue
. The
set
method is the same as the
create
or the
put
method.
If you invoke the
set
method again but this time set
anothervalue
for the key,
akey
, the original
value is replaced with the new one. Try out the following:
./redis-cli set akey anothervalue
./redis-cli get akey
The response, as expected, would be the new value:
anothervalue
.
The familiar
set
and
get
commands for a string can't be used for Redis collections, though. For
example, using
lpush
and
rpush
creates and populates a list. A nonexistent list can be created along
with its fi rst member as follows:
./redis-cli lpush list_of_books 'MongoDB: The Definitive Guide'
Available for
download on
Wrox.com
books_list_redis.txt
You can use the range operation to verify and see the fi rst few members of the list —
list_of_
books
— like so:
./redis-cli lrange list_of_books 0 -1
1. “MongoDB: The Definitive Guide”
Available for
download on
Wrox.com
books_list_redis.txt
The range operation uses the index of the fi rst element, 0, and the index of the last element, -1, to
get all elements in the list.