|
| 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