如何使用 golang SDK 检查 couchbase 文档是否存在,而不检索完整内容?
How to check if couchbase document exists, without retrieving full content, using golang SDK?
在我的代码中,我想根据给定密钥存在的文档执行或不执行某些操作。但是无法避免检索所有文档内容的额外网络开销。
现在我正在使用
cas, err := bucket.Get(key, &value)
并寻找 err == gocb.ErrKeyNotFound
以确定文档遗漏。
有没有更有效的方法?
您可以使用 sub-document API 并检查字段是否存在。
示例来自 使用 Sub-Document API 获得(仅)您想要的
:
rv = bucket.lookup_in('customer123', SD.exists('purchases.pending[-1]'))
rv.exists(0) # (check if path for first command exists): =>; False
编辑:添加围棋示例
您可以使用 sub-document API 来检查文档是否存在,如下所示:
frag, err := bucket.LookupIn("document-key").
Exists("any-path").Execute()
if err != nil && err == gocb.ErrKeyNotFound {
fmt.Printf("Key does not exist\n")
} else {
if frag.Exists("any-path") {
fmt.Printf("Path exists\n")
} else {
fmt.Printf("Path does not exist\n")
}
}
在我的代码中,我想根据给定密钥存在的文档执行或不执行某些操作。但是无法避免检索所有文档内容的额外网络开销。
现在我正在使用
cas, err := bucket.Get(key, &value)
并寻找 err == gocb.ErrKeyNotFound
以确定文档遗漏。
有没有更有效的方法?
您可以使用 sub-document API 并检查字段是否存在。
示例来自 使用 Sub-Document API 获得(仅)您想要的 :
rv = bucket.lookup_in('customer123', SD.exists('purchases.pending[-1]'))
rv.exists(0) # (check if path for first command exists): =>; False
编辑:添加围棋示例
您可以使用 sub-document API 来检查文档是否存在,如下所示:
frag, err := bucket.LookupIn("document-key").
Exists("any-path").Execute()
if err != nil && err == gocb.ErrKeyNotFound {
fmt.Printf("Key does not exist\n")
} else {
if frag.Exists("any-path") {
fmt.Printf("Path exists\n")
} else {
fmt.Printf("Path does not exist\n")
}
}