How to add new field in Couchbase document using GO SDK

459 Views Asked by At

The register structure below captures the data coming from the registration UI:

register struct {
   Email string
   Password string }

The collection.insert command creates the following document:

{  "id": "1",
   "email" : "[email protected]",
   "password" : "pwd"
}

The demographics struct captures the data coming from Demographics UI:

demographics struct {
    Name string
   Address string
}

I want to update the document so the resulting document is as follows:

 { "id": "1",
      "email" : "[email protected]",
      "password": "pwd",
      "name" : "John Doe",
      "address" : "100 Main Street"
    }

Using N1QL I could write the following:

Update bucket set name="John Doe", address="100 Main Street" where id="1"

I couldn't find update API in GO SDK.

1

There are 1 best solutions below

1
On BEST ANSWER

I'm not a Go developer, but I think what you're looking for is called "sub-document" operations in Couchbase (and it's available in all the Couchbase SDKs, including Go)

That is, the ability to insert/update/delete parts of a document without moving the whole thing across the wire. For instance, here's a snippet to add a "fax" field to a document (upsert creates or replaces the field as necessary):

bucket.MutateIn("customer123", 0, 0).Upsert("fax", "311-555-0151", true).Execute()

There are many subdocument options: insert, replace, exists, arrayappend, arrayprepend, etc.