dpymrcl269187540 2016-01-25 04:34
浏览 26
已采纳

google app引擎转到数据存储区,使用键检查实体是否存在

Given a stringId key for an entity, how do I check if there is a corresponding entity in datastore. I do not want to fetch the entity completely. I want to check if the entity exists or not.
Is there a performance impact if I fetch the complete entity to check its existence? Or is there a better way?

var Person struct {
   stringId string //string id which makes the key
   //many other properties.
}

//insert into datastore
_, err := datastore.Put(ctx, datastore.NewKey(ctx, entityKind, stringId, 0, nil), entity)
//retrieve the entity
datastore.Get(ctx, datastore.NewKey(ctx, entityKind, stringId, 0, nil), entity);


Instead of retrieving complete entity for a given stringId, is there a better way to check the entity exists?

  • 写回答

1条回答 默认 最新

  • dtbsezxw28056 2016-01-25 06:52
    关注

    To retrieve only key(s) add KeysOnly() to the end of your query, ie

    q := datastore.NewQuery(entityKind).Filter(...).KeysOnly()
    

    And yes, keys only query should be faster, quote from the doc:

    A keys-only query returns just the keys of the result entities instead of the entities themselves, at lower latency and cost than retrieving entire entities

    BTW, to retrieve an entity by it's key, you can also use the special property __key__, ie

    qKey := datastore.NewKey(ctx, entityKind, stringId, 0, nil)
    q := datastore.NewQuery(entityKind).Filter("__key__ =", qKey)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计