在我的代码中,我想做或不做一些操作,这取决于具有给定密钥存在的文档。但无法避免额外的网络开销,检索所有文档内容。
现在我在用
cas, err := bucket.Get(key, &value)并寻找err == gocb.ErrKeyNotFound来确定文档丢失的案例。
有没有更有效的方法?
发布于 2018-03-15 12:44:07
您可以使用子文档API并检查字段是否存在。
来自使用子文档API获取(仅)您想要的的示例
rv = bucket.lookup_in('customer123', SD.exists('purchases.pending[-1]'))
rv.exists(0) # (check if path for first command exists): =>; False编辑:添加go示例
您可以使用子文档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")
}
}https://stackoverflow.com/questions/49296315
复制相似问题