通过Go语言更新Couchbase文档的字段,如果文档存在的话。

huangapple go评论83阅读模式
英文:

Updating fields of a Couchbase document if it exists by Go

问题

我正在使用gocb库。我想要更新文档的特定字段。

然而,如果文档不存在,我不想做任何操作,只是生成一个错误消息。

你可以说首先检索完整的文档本身,然后进行更新并插入。这是可能的。但是,如果有的话,我想要使用一个现成的方法来实现这个目的。因为我不想检索整个文档,我只想更新其中的一些字段。

gocb库中是否有这样的方法?

英文:

I am using gocb library. I want to update specific field of a document.

However if the document does not exist, I don't want to do anything I will just produce an error message.

You can say that first retrieve the full document itself and make update and then insert it.It is possible right. But I want to use a ready for use method for this purpose if there is any. Since I don't want to retrieve the document. I just want to update some fields of it.

Is there a way for this in gocb library?

答案1

得分: 1

如果您想更新文档的部分内容,您可以查看sub-document operations。它只传输文档中被访问的部分,使得对于小的更改更加高效。

示例:https://couchbase.live/examples/basic-go-subdoc-mutate

如果您想重写整个文档,您可以使用Replace()方法,它可以用新文档替换现有文档。它类似于Upsert(),但只能替换现有文档,而不能创建新文档。

常规参考:https://docs.couchbase.com/server/current/guides/updating-data.html

英文:

If you want to update parts of the document, you can look at sub-document operations. It only transmits the accessed sections of the document over the network making it more efficient for small changes.
Example: https://couchbase.live/examples/basic-go-subdoc-mutate

If you want to rewrite the entire document, you are looking for Replace() which replaces an existing document with a new one. It is similar to Upsert() except that it can only replace existing documents & not create new ones.

General Reference:https://docs.couchbase.com/server/current/guides/updating-data.html

huangapple
  • 本文由 发表于 2022年11月17日 04:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/74466874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定