The Prepend() methods allow you to add
information to an existing key/value pair in the database. You can
use this to add information to a string or other data before the
existing data.
The Prepend() methods prepend raw
serialized data on to the end of the existing data in the key. If
you have previously stored a serialized object into Couchbase and
then use Prepend, the content of the serialized object will not be
extended. For example, adding an List of
integers into the database, and then using
Prepend() to add another integer will
result in the key referring to a serialized version of the list,
immediately preceded by a serialized version of the integer. It
will not contain an updated list with the new integer prepended to
it. De-serialization of objects that have had data prepended may
result in data corruption.
| API Call | object.Prepend(key, value) | ||
| Asynchronous | no | ||
| Description | Prepend a value to an existing key | ||
| Returns | Object (
Binary object
) | ||
| Arguments | |||
string key | Document ID used to identify the value | ||
object value | Value to be stored | ||
The Prepend() method prepends information
to the end of an existing key/value pair.
The sample below demonstrates how to create a csv string by prepending new values.
client.Store(StoreMode.Set, "beers", "Abbey Ale"); Func<string, byte[]> stringToBytes = (s) => Encoding.Default.GetBytes(s); client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Three Philosophers,"))); client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Witte,")));
You can check if the Prepend operation succeeded by using the checking the return value.
var result = client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Hennepin,"))); if (result) { Console.WriteLine("Prepend succeeded"); } else { Console.WriteLine("Prepend failed"); }
| API Call | object.Prepend(key, casunique, value) | ||
| Asynchronous | no | ||
| Description | Prepend a value to an existing key | ||
| Returns | Object (
Binary object
) | ||
| Arguments | |||
string key | Document ID used to identify the value | ||
ulong casunique | Unique value used to verify a key/value combination | ||
object value | Value to be stored | ||
Prepend() may also be used with a CAS
value. With this overload, the return value is a
CasResult, where success is determined by
examining the CasResult's Result property.
var casv = client.GetWithCas("beers"); var casResult = client.Prepend("beers", casv.Cas, new ArraySegment<byte>(stringToBytes("Adoration,"))); if (casResult.Result) { Console.WriteLine("Prepend succeeded"); } else { Console.WriteLine("Prepend failed"); }