forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
updating.go
58 lines (48 loc) · 1.3 KB
/
updating.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"github.com/couchbase/gocb"
)
// bucket reference - reuse as bucket reference in the application
var bucket *gocb.Bucket
func main() {
// Connect to Cluster
cluster, err := gocb.Connect("couchbase://127.0.0.1")
if err != nil {
fmt.Println("ERROR CONNECTING TO CLUSTER:", err)
}
// Open Bucket
bucket, err = cluster.OpenBucket("travel-sample", "")
if err != nil {
fmt.Println("ERROR OPENING BUCKET:", err)
}
// Create a document
key := "goDevguideExampleUpdate"
val := "Retrieve Test Value"
_, err = bucket.Upsert(key, &val, 0)
if err != nil {
fmt.Println("ERROR CREATING DOCUMENT:", err)
}
// Retrieve Document
var retValue interface{}
_, err = bucket.Get(key, &retValue)
if err != nil {
fmt.Println("ERROR RETURNING DOCUMENT:", err)
}
fmt.Println("Document Retrieved:", retValue)
// Add something to the value
retValue = retValue.(string) + " with Additional Test Value"
// Replace the existing document
_, err = bucket.Replace(key, &retValue, 0, 0)
if err != nil {
fmt.Println("ERROR REPLACING DOCUMENT:", err)
}
// Retrieve updated document
_, err = bucket.Get(key, &retValue)
if err != nil {
fmt.Println("ERROR RETURNING DOCUMENT:", err)
}
fmt.Println("Document Retrieved:", retValue)
// Exiting
fmt.Println("Example Successful - Exiting")
}