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").
    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
    点赞 评论

相关推荐 更多相似问题