The prepend methods insert information
before the existing data for a given key. Note that as with the
append method, the information will be
inserted before the existing binary data stored in the key, which
means that serialization of complex objects may lead to corruption
when using prepend.
| API Call | object.prepend(key, value [, ruby-prepend-options ]) | ||
| Asynchronous | no | ||
| Description | Prepend a value to an existing key | ||
| Returns | fixnum (
The CAS value for the object stored. A fixed number
) | ||
| Arguments | |||
string key | Document ID used to identify the value | ||
object value | Value to be stored | ||
hash ruby-prepend-options | Hash of options containing key/value pairs | ||
| Structure definition: | |||
:cas (fixnum) | CAS Value | ||
| The CAS value for an object. This value was created on the server and is guaranteed to be unique for each value for a given key. You provide this value as an option when you want basic optimistic concurrency control while doing sets. | |||
:format (symbol) | How to represent value in storage. | ||
| Determines how a value is represented in storage. Possible values include :plain for string storage. | |||
| Exceptions | |||
ArgumentError | Exception object indicating failed attempt to pass a block in synchronous mode. | ||
Couchbase::Error::Connect | Exception object specifying failure to connect to a node. | ||
Couchbase::Error::KeyExists | Exception object indicating the key already exists on the server. | ||
Couchbase::Error::NotStored | Exception object indicating the key/value does not exist in the database. | ||
For example, to prepend a string to an existing key:
#set inital key, foo couchbase.set("foo", "world!") #prepend text 'Hello, ' to foo couchbase.prepend("foo", "Hello, ") #get new foo value couchbase.get("foo") #=> "Hello, world!"
Other examples of using prepend are as follows:
#simple prepend example c.set("foo", "aaa") c.prepend("foo", "bbb") c.get("foo") #=> "bbbaaa" #Perform optimistic locking. The operations fails if the #given CAS does not match the CAS for the key ver = c.set("foo", "aaa") c.prepend("foo", "bbb", :cas => ver) #Use explicit format options c.default_format #=>defaults to :document, or JSON document c.set("foo", {"y" => "z"}) #sets added text to plain data and adds c.prepend("foo", '[', :format => :plain) c.append("foo", ', {"z": "y"}]', :format => :plain) #get updated value c.get("foo") #=> [{"y"=>"z"}, {"z"=>"y"}]