如何使用 GO SDK 在 Couchbase 文档中添加新字段

How to add new field in Couchbase document using GO SDK

下面的寄存器结构捕获来自注册的数据UI:

register struct {
   Email string
   Password string }

collection.insert 命令创建以下文档:

{  "id": "1",
   "email" : "john@example.com",
   "password" : "pwd"
}

人口统计结构捕获来自人口统计的数据UI:

demographics struct {
    Name string
   Address string
}

我想更新文档所以生成的文档如下:

 { "id": "1",
      "email" : "john@example.com",
      "password": "pwd",
      "name" : "John Doe",
      "address" : "100 Main Street"
    }

使用 N1QL 我可以编写以下内容:

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

我在 GO SDK 中找不到更新 API。

我不是 Go 开发人员,但我认为您正在寻找的是 Couchbase 中的 "sub-document" operations(它在所有 Couchbase SDK 中都可用,包括 Go)

也就是说,能够 insert/update/delete 部分 文档而无需移动整个文档。例如,这里有一个片段,用于向文档添加 "fax" 字段(upsert 根据需要创建或替换该字段):

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

有很多子文档选项:insert、replace、exists、arrayappend、arrayprepend等