Skip to content

Instantly share code, notes, and snippets.

@chvck

chvck/subdoc.go Secret

Created February 25, 2019 19:41
Show Gist options
  • Save chvck/889a1097d3f28d46df0945d34c1c9031 to your computer and use it in GitHub Desktop.
Save chvck/889a1097d3f28d46df0945d34c1c9031 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/couchbaselabs/gojcbmock"
"gopkg.in/couchbase/gocb.v1"
)
func main() {
gocb.SetLogger(gocb.VerboseStdioLogger())
mpath, err := gojcbmock.GetMockPath()
if err != nil {
panic(err.Error())
}
mock, err := gojcbmock.NewMock(mpath, 4, 1, 64, []gojcbmock.BucketSpec{
{Name: "default", Type: gojcbmock.BCouchbase},
{Name: "memd", Type: gojcbmock.BMemcached},
}...)
if err != nil {
panic(err.Error())
}
connStr := fmt.Sprintf("http://127.0.0.1:%d?use_enhanced_errors=true", mock.EntryPort)
// connStr := "couchbase://localhost"
cluster, err := gocb.Connect(connStr)
if err != nil {
panic("Error connecting to cluster:" + err.Error())
}
err = cluster.Authenticate(gocb.PasswordAuthenticator{
Username: "",
Password: "",
})
// err = cluster.Authenticate(gocb.PasswordAuthenticator{
// Username: "Administrator",
// Password: "password",
// })
if err != nil {
panic("Error creating authenticator:" + err.Error())
}
bucket, err := cluster.OpenBucket("default", "")
if err != nil {
panic("Error opening bucket:" + err.Error())
}
docJson := `{
"name": "Emmy-lou Dickerson",
"age": 26,
"animals": ["cat", "dog", "parrot"],
"attributes": {
"hair": "brown",
"dimensions": {
"height": 67,
"weight": 175
},
"hobbies": [
{
"type": "winter sports",
"name": "curling"
},
{
"type": "summer sports",
"name": "water skiing",
"details": {
"location": {
"lat": 49.282730,
"long": -123.120735
}
}
}
]
}
}`
_, err = bucket.Upsert("test", docJson, 0)
if err != nil {
panic("Error upserting doc:" + err.Error())
}
_, err = bucket.LookupIn("test").Get("attributes.hobbies[1].details.location").Execute()
if err != nil {
panic("Error looking up subdoc:" + err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment