douba9776 2018-03-15 09:56
浏览 71
已采纳

如何使用golang SDK在不获取完整内容的情况下检查长沙发文档是否存在?

In my code I want to do or not to do some actions depending on document with given key existence. But can't avoid additional network overhead retrieving all document content.

Now I'm using

cas, err := bucket.Get(key, &value)

And looking for err == gocb.ErrKeyNotFound to determine document missed case.

Is there some more efficient approach?

  • 写回答

1条回答 默认 最新

  • dongsunny1113 2018-03-15 12:44
    关注

    You can use the sub-document API and check for the existence of a field.

    Example from Using the Sub-Document API to get (only) what you want :

    rv = bucket.lookup_in('customer123', SD.exists('purchases.pending[-1]'))
    rv.exists(0) # (check if path for first command exists): =>; False
    

    Edit: Add go example

    You can use the sub-document API to check for document existence like this:

    frag, err := bucket.LookupIn("document-key").
        Exists("any-path").Execute()
    
    if err != nil && err == gocb.ErrKeyNotFound {
        fmt.Printf("Key does not exist
    ")
    } else {
        if frag.Exists("any-path") {
            fmt.Printf("Path exists
    ")
        } else {
            fmt.Printf("Path does not exist
    ")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度