Skip to content

Commit aca8ece

Browse files
Add kv operations examples for dev-reorg (#228)
1 parent b10fbd5 commit aca8ece

2 files changed

Lines changed: 341 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Run this using dotnet-script: https://github.com/filipw/dotnet-script
2+
//
3+
// dotnet script KvBulkHelloWorld.csx
4+
//
5+
6+
#r "nuget: CouchbaseNetClient, 3.2.5"
7+
8+
using System;
9+
using System.Threading.Tasks;
10+
using Couchbase;
11+
using Couchbase.KeyValue;
12+
13+
await new KvBulkHelloWorld().ExampleAsync();
14+
15+
public class KvBulkHelloWorld
16+
{
17+
public async Task ExampleAsync()
18+
{
19+
var cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");
20+
var bucket = await cluster.BucketAsync("travel-sample");
21+
var tenantScope = await bucket.ScopeAsync("tenant_agent_00");
22+
var usersCollection = await tenantScope.CollectionAsync("users");
23+
24+
// tag::kv-users[]
25+
var documents = new[]
26+
{
27+
new { id = "user_111", email = "tom_the_cat@gmail.com"},
28+
new { id = "user_222", email = "jerry_mouse@gmail.com"},
29+
new { id = "user_333", email = "mickey_mouse@gmail.com"}
30+
};
31+
// end::kv-users[]
32+
33+
{
34+
Console.WriteLine("\n[kv-bulk-insert]");
35+
// tag::kv-bulk-insert[]
36+
// Collection of things that will complete in the future.
37+
var tasks = new List<Task<IMutationResult>>();
38+
39+
// Create tasks to be executed concurrently.
40+
foreach (var document in documents)
41+
{
42+
Console.WriteLine($"Inserting document: {document.id}");
43+
var task = usersCollection.InsertAsync(document.id, document);
44+
tasks.Add(task);
45+
}
46+
47+
// Wait until all of the tasks have completed.
48+
await Task.WhenAll(tasks);
49+
50+
// Iterate task list to get results.
51+
foreach (var task in tasks)
52+
Console.WriteLine($"CAS: {task.Result.Cas}");
53+
// end::kv-bulk-insert[]
54+
}
55+
56+
{
57+
Console.WriteLine("\n[kv-bulk-upsert]");
58+
// tag::kv-bulk-upsert[]
59+
var newDocuments = new[]
60+
{
61+
new { id = "user_111", email = "tom@gmail.com"},
62+
new { id = "user_222", email = "jerry@gmail.com"},
63+
new { id = "user_333", email = "mickey@gmail.com"}
64+
};
65+
66+
// Collection of things that will complete in the future.
67+
var tasks = new List<Task<IMutationResult>>();
68+
69+
// Create tasks to be executed concurrently.
70+
foreach (var newDocument in newDocuments)
71+
{
72+
Console.WriteLine($"Upserting document: {newDocument.id}");
73+
var task = usersCollection.UpsertAsync(newDocument.id, newDocument);
74+
tasks.Add(task);
75+
}
76+
77+
// Wait until all of the tasks have completed.
78+
await Task.WhenAll(tasks);
79+
80+
// Iterate task list to get results.
81+
foreach (var task in tasks)
82+
Console.WriteLine($"CAS: {task.Result.Cas}");
83+
// end::kv-bulk-upsert[]
84+
}
85+
86+
{
87+
Console.WriteLine("\n[kv-bulk-get]");
88+
// tag::kv-bulk-get[]
89+
// Collection of things that will complete in the future.
90+
var tasks = new List<Task<IGetResult>>();
91+
92+
// Create tasks to be executed concurrently.
93+
foreach (var document in documents)
94+
{
95+
Console.WriteLine($"Getting document: {document.id}");
96+
var task = usersCollection.GetAsync(document.id);
97+
tasks.Add(task);
98+
}
99+
100+
// Wait until all of the tasks have completed.
101+
await Task.WhenAll(tasks);
102+
103+
// Iterate task list to get results.
104+
foreach (var task in tasks)
105+
Console.WriteLine($"Document: {task.Result.ContentAs<dynamic>()}");
106+
// end::kv-bulk-get[]
107+
}
108+
109+
{
110+
Console.WriteLine("\n[kv-bulk-remove]");
111+
// tag::kv-bulk-remove[]
112+
// Collection of things that will complete in the future.
113+
var tasks = new List<Task>();
114+
115+
// Create tasks to be executed concurrently.
116+
foreach (var document in documents)
117+
{
118+
Console.WriteLine($"Removing document: {document.id}");
119+
var task = usersCollection.RemoveAsync(document.id);
120+
tasks.Add(task);
121+
}
122+
123+
// Wait until all of the tasks have completed.
124+
// NOTE: RemoveAsync returns void, so no need to loop through each task.
125+
await Task.WhenAll(tasks);
126+
// end::kv-bulk-remove[]
127+
}
128+
}
129+
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Run this using dotnet-script: https://github.com/filipw/dotnet-script
2+
//
3+
// dotnet script KvHelloWorldScoped.csx
4+
//
5+
6+
#r "nuget: CouchbaseNetClient, 3.2.5"
7+
8+
using System;
9+
using System.Threading.Tasks;
10+
using Couchbase;
11+
using Couchbase.KeyValue;
12+
using Newtonsoft.Json.Linq;
13+
14+
await new KvHelloWorldScoped().ExampleAsync();
15+
16+
public class KvHelloWorldScoped
17+
{
18+
public async Task ExampleAsync()
19+
{
20+
var cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");
21+
var bucket = await cluster.BucketAsync("travel-sample");
22+
var inventoryScope = await bucket.ScopeAsync("inventory");
23+
var hotelCollection = await inventoryScope.CollectionAsync("hotel");
24+
25+
{
26+
Console.WriteLine("[kv-insert]");
27+
// tag::kv-insert[]
28+
// Create a document object.
29+
var document = new
30+
{
31+
id = 123,
32+
name = "Medway Youth Hostel ",
33+
address = "Capstone Road, ME7 3JE",
34+
url = "http://www.yha.org.uk",
35+
geo = new
36+
{
37+
lat = 51.35785,
38+
lon = 0.55818,
39+
accuracy = "RANGE_INTERPOLATED"
40+
},
41+
country = "United Kingdom",
42+
city = "Medway",
43+
state = (string)null,
44+
reviews = new[]
45+
{
46+
new {
47+
content = "This was our 2nd trip here and we enjoyed it more than last year.",
48+
author = "Ozella Sipes",
49+
date = DateTime.UtcNow
50+
}
51+
},
52+
vacancy = true,
53+
description = "40 bed summer hostel about 3 miles from Gillingham."
54+
};
55+
56+
// Insert the document in the hotel collection.
57+
var insertResult = await hotelCollection.InsertAsync("hotel-123", document);
58+
59+
// Print the result's CAS metadata to the console.
60+
Console.WriteLine($"Cas: {insertResult.Cas}");
61+
// end::kv-insert[]
62+
}
63+
64+
{
65+
Console.WriteLine("\n[kv-insert-with-opt]");
66+
// tag::kv-insert-with-opts[]
67+
var document = new
68+
{
69+
id = 456,
70+
title = "Ardèche",
71+
name = "La Pradella",
72+
address = "rue du village, 07290 Preaux, France",
73+
phone = "+33 4 75 32 08 52",
74+
url = "http://www.lapradella.fr",
75+
country = "France",
76+
city = "Preaux",
77+
state = "Rhône-Alpes",
78+
vacancy = false
79+
};
80+
81+
// Insert the document with an expiry time option of 60 seconds.
82+
var insertResult = await hotelCollection.InsertAsync("hotel-456", document, options =>
83+
{
84+
options.Expiry(TimeSpan.FromSeconds(60));
85+
});
86+
87+
// Print the result's CAS metadata to the console.
88+
Console.WriteLine($"CAS: {insertResult.Cas}");
89+
// end::kv-insert-with-opts[]
90+
}
91+
92+
{
93+
Console.WriteLine("\n[kv-get]");
94+
// tag::kv-get[]
95+
var getResult = await hotelCollection.GetAsync("hotel-123");
96+
97+
// Print some result metadata to the console.
98+
Console.WriteLine($"CAS: {getResult.Cas}");
99+
Console.WriteLine($"Data: {getResult.ContentAs<JObject>()}");
100+
// end::kv-get[]
101+
}
102+
103+
{
104+
Console.WriteLine("\n[kv-get-with-opts]");
105+
// tag::kv-get-with-opts[]
106+
var getResult = await hotelCollection.GetAsync("hotel-456", options =>
107+
{
108+
options.Expiry();
109+
});
110+
111+
// Print some result metadata to the console.
112+
Console.WriteLine($"CAS: {getResult.Cas}");
113+
Console.WriteLine($"Data: {getResult.ContentAs<JObject>()}");
114+
Console.WriteLine($"Expiry: {getResult.ExpiryTime}");
115+
// end::kv-get-with-opts[]
116+
}
117+
118+
{
119+
Console.WriteLine("\n[kv-get-subdoc]");
120+
// tag::kv-get-subdoc[]
121+
var lookupInResult = await hotelCollection.LookupInAsync("hotel-123",
122+
specs => specs.Get("geo")
123+
);
124+
125+
Console.WriteLine($"CAS: {lookupInResult.Cas}");
126+
Console.WriteLine($"Geo: {lookupInResult.ContentAs<JObject>(0)}");
127+
// end::kv-get-subdoc[]
128+
}
129+
130+
{
131+
Console.WriteLine("\n[kv-update-replace]");
132+
// tag::kv-update-replace[]
133+
// Fetch an existing hotel document.
134+
var getResult = await hotelCollection.GetAsync("hotel-123");
135+
var existingDoc = getResult.ContentAs<JObject>();
136+
137+
// Get the current CAS value.
138+
var currentCas = getResult.Cas;
139+
Console.WriteLine($"Current Cas: {currentCas}");
140+
141+
// Add a new review to the reviews array.
142+
var reviews = (JArray)existingDoc["reviews"];
143+
reviews.Add(new JObject(
144+
new JProperty("content", "This hotel was cozy, conveniently located and clean."),
145+
new JProperty("author", "Carmella O'Keefe"),
146+
new JProperty("date", DateTime.UtcNow))
147+
);
148+
149+
// Update the document with new data and pass the current CAS value.
150+
var replaceResult = await hotelCollection.ReplaceAsync("hotel-123", existingDoc, options =>
151+
{
152+
options.Cas(currentCas);
153+
});
154+
155+
// Print the new CAS value.
156+
Console.WriteLine($"New Cas: {replaceResult.Cas}");
157+
// end::kv-update-replace[]
158+
}
159+
160+
{
161+
Console.WriteLine("\n[kv-update-upsert]");
162+
// Create a document object.
163+
var document = new
164+
{
165+
id = 123,
166+
name = "Medway Youth Hostel ",
167+
address = "Capstone Road, ME7 3JE",
168+
url = "http://www.yha.org.uk",
169+
country = "United Kingdom",
170+
city = "Medway",
171+
state = (string)null,
172+
vacancy = true,
173+
description = "40 bed summer hostel about 3 miles from Gillingham."
174+
};
175+
176+
// tag::kv-update-upsert[]
177+
// Update or create a document in the hotel collection.
178+
var upsertResult = await hotelCollection.UpsertAsync("hotel-123", document);
179+
180+
// Print the result's CAS metadata to the console.
181+
Console.WriteLine($"Cas: {upsertResult.Cas}");
182+
// end::kv-update-upsert[]
183+
}
184+
185+
{
186+
Console.WriteLine("\n[kv-update-subdoc]");
187+
// tag::kv-update-subdoc[]
188+
var mutateInResult = await hotelCollection.MutateInAsync("hotel-123",
189+
specs => specs.Upsert("pets_ok", true)
190+
);
191+
Console.WriteLine($"Cas: {mutateInResult.Cas}");
192+
// end::kv-update-subdoc[]
193+
}
194+
195+
{
196+
Console.WriteLine("\n[kv-remove-subdoc]");
197+
// tag::kv-remove-subdoc[]
198+
var mutateInResult = await hotelCollection.MutateInAsync("hotel-123",
199+
specs => specs.Remove("url")
200+
);
201+
Console.WriteLine($"Cas: {mutateInResult.Cas}");
202+
// end::kv-remove-subdoc[]
203+
}
204+
205+
{
206+
Console.WriteLine("\n[kv-remove]");
207+
// tag::kv-remove[]
208+
await hotelCollection.RemoveAsync("hotel-123");
209+
// end::kv-remove[]
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)