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
1.3 Try it Out!
Chapter Sections
Chapters

1.3.4. CRUD Operations

The primary CRUD API used by the .NET Client is that of a standard key/value store. You create and update documents by supplying a key and value. You retrieve or remove documents by supplying a value. For example, consider the JSON document that you'll find in the "beer-sample" bucket that's available when you install Couchbase Server and setup your cluster. The key for this document is "new_holland_brewing_company-sundog."

{
 "name": "Sundog",
 "abv": 5.25,
 "ibu": 0,
 "srm": 0,
 "upc": 0,
 "type": "beer",
 "brewery_id": "new_holland_brewing_company",
 "updated": "2010-07-22 20:00:20",
 "description": "Sundog is an amber ale as deep as the copper glow of a Lake Michigan sunset. Its biscuit malt give Sundog a toasty character and a subtle malty sweetness. Sundog brings out the best in grilled foods, caramelized onions, nutty cheese, barbecue, or your favorite pizza.",
 "style": "American-Style Amber/Red Ale",
 "category": "North American Ale"
}

To retrieve this document, you simply call the Get method of the client.

var savedBeer = client.Get("new_holland_brewing_company-sundog");

If you add a line to print the savedBeer to the console, you should see a JSON string that contains the data above.

var savedBeer = client.Get("new_holland_brewing_company-sundog");
Console.WriteLine(savedBeer);

In this example, savedBeer would be of type Object. To get a string back instead and avoid having to cast, simply use the generic version of Get.

var savedBeer = client.Get<string>("new_holland_brewing_company-sundog");

To add a document, first create a JSON string.

var newBeer =
@"{
   ""name"": ""Old Yankee Ale"",
   ""abv"": 5.00,
   ""ibu"": 0,
   ""srm"": 0,
   ""upc"": 0,
   ""type"": ""beer"",
   ""brewery_id"": ""cottrell_brewing"",
   ""updated"": ""2012-08-30 20:00:20"",
   ""description"": ""A medium-bodied Amber Ale"",
   ""style"": ""American-Style Amber"",
   ""category"": ""North American Ale""
}";

For a key, we'll simply take the name of the beer and prefix it with the name of the brewery, separated with a dash and with spaces replaced by underscores. The exact mechanism by which you create your keys need only be consistent. If you are going to query documents by key (not just through views) you should choose predictable keys (e.g., cottrell_brewing-old_yankee_ale).

var key = "cottrell_brewing-old_yankee_ale";

With this new key, the JSON document may easily be stored.

var result = client.Store(StoreMode.Add, "cottrell_brewing-old_yankee_ale", newBeer);

There are three arguments to Store. The first is the store mode. Use StoreMode.Add for new keys, StoreMode.Replace to update existing keys and StoreMode.Set to add when a key doesn’t exist or to replace it when it does. Store will fail if Add is used with an existing key or Replace is used with a non-existent key. The second and third arguments are the key and value, respectively. The return value, assigned to the result variable, is a Boolean indicating whether the operation succeeded.

Removing a document simply entails calling the Remove method with the key to be removed. Like the other methods we've seen so far, Remove returns a Boolean indicating operation success.

var result = client.Remove("cottrell_brewing-old_yankee_ale");