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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题