From 12b5307a71b5265bac2eb4069902b2f5b31b5fbc Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Tue, 2 Dec 2025 22:35:12 +0700 Subject: [PATCH] feat(core): replace increment with get & set --- couchbase/main.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/couchbase/main.go b/couchbase/main.go index 4667fc7..e91a6fb 100644 --- a/couchbase/main.go +++ b/couchbase/main.go @@ -116,13 +116,20 @@ func main() { func incrementCounter(col *gocb.Collection) error { counterDocId := "counter" - // Increment by 1, creating doc if needed. - // By using `Initial: 1` we set the starting count(non-negative) to 1 if the document needs to be created. - // If it already exists, the count will increase by the amount provided in the Delta option(i.e 1). - increment, err := col.Binary().Increment(counterDocId, &gocb.IncrementOptions{Initial: 1, Delta: 1}) + docOut, err := col.Get(counterDocId, &gocb.GetOptions{}) if err != nil { return err } - log.Printf("Counter : %d\n", increment.Content()) + var current uint64 + err = docOut.Content(¤t) + if err != nil { + return err + } + current++ + _, err = col.Upsert(counterDocId, current, &gocb.UpsertOptions{}) + if err != nil { + return err + } + log.Printf("Counter : %d\n", current) return nil }