Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: .NET (C#) 1.2
Community Wiki and Resources
Wiki: .NET Client Library
Download Client Library
.NET Client Library
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
7 Update Operations
Chapter Sections
Chapters

7.5. Prepend Methods

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 after 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 followed 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 Callobject.Prepend(key, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsObject ( 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 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 Callobject.Prepend(key, casunique, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsObject ( 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

The Prepend operation may also be used with a CAS value.

var storeResult = client.ExecuteStore(StoreMode.Set, "beers", "Abbey Ale");

Func<string, byte[]> stringToBytes = (s) => Encoding.Default.GetBytes(s);
client.Prepend("beers", storeResult.Cas, new ArraySegment<byte>(stringToBytes(",Three Philosophers")));
API Callobject.ExecutePrepend(key, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsIConcatOperationResult ( Concat operation result )
Arguments 
string key Document ID used to identify the value
object value Value to be stored

The ExecutePrepend operation is used to get an instance of an IConcatOperationResult.

client.ExecuteStore(StoreMode.Set, "beers", "Abbey Ale");
Func<string, byte[]> stringToBytes = (s) => Encoding.Default.GetBytes(s);
var result = client.ExecutePrepend("beers", new ArraySegment<byte>(stringToBytes(",Three Philosophers")));

if (! result.Sucecss)
{
    Console.WriteLine("Prepend failed with message {0} and status code {1}", result.Message, result.StatusCode);
    
    if (result.Exception == null)
    {
        throw result.Exception;
    }
}
API Callobject.ExecutePrepend(key, casunique, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsIConcatOperationResult ( Concat operation result )
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

The ExecutePrepend operation may also be used with a CAS value.

var storeResult = client.ExecuteStore(StoreMode.Set, "beers", "Abbey Ale");

Func<string, byte[]> stringToBytes = (s) => Encoding.Default.GetBytes(s);
var prependResult = client.ExecutePrepend("beers", storeResult.Cas, new ArraySegment<byte>(stringToBytes(",Three Philosophers")));

if (! prependResult.Sucecss)
{
    Console.WriteLine("Prepend failed with message {0} and status code {1}", prependResult.Message, prependResult.StatusCode);
    
    if (prependResult.Exception == null)
    {
        throw prependResult.Exception;
    }
}