給定實體的 stringId 鍵,如何檢查數據存儲中是否有相應的實體。我不想完全獲取實體。我想檢查實體是否存在。如果我獲取完整實體以檢查其存在,是否會對性能產生影響?或者,還有更好的方法?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 entitydatastore.Get(ctx, datastore.NewKey(ctx, entityKind, stringId, 0, nil), entity);有沒有更好的方法來檢查實體是否存在,而不是檢索給定 stringId 的完整實體?
1 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
要僅檢索鍵添加KeysOnly()
到查詢的末尾,即
q := datastore.NewQuery(entityKind).Filter(...).KeysOnly()
是的,僅鍵查詢應該更快,引用自文檔:
僅鍵查詢僅返回結果實體的鍵而不是實體本身,與檢索整個實體相比,延遲和成本更低
順便說一句,要通過其鍵檢索實體,您還可以使用特殊屬性__key__
,即
qKey := datastore.NewKey(ctx, entityKind, stringId, 0, nil) q := datastore.NewQuery(entityKind).Filter("__key__ =", qKey)
- 1 回答
- 0 關注
- 213 瀏覽
添加回答
舉報
0/150
提交
取消