使用GetMulti列出单个Datastore Kind的所有实体。

huangapple go评论77阅读模式
英文:

List all Entities of single Datastore Kind using GetMulti

问题

有没有办法让我使用datastore的GetMulti函数,或者其他内置于“appengine/datastore”包中的函数,来获取同一种类的所有实体?

例如,我有一个名为“Queue”的种类,其中有许多具有两到三个属性的实体。每个实体都有一个唯一的stringID,我想要获取的是每个唯一stringID的切片或其他可比较的数据类型。

Queue的目的是存储一些元数据和我将循环遍历并执行cron任务的唯一键名(例如,键“user1”、“user2”和“user3”作为Queue种类存储,然后在cron中循环遍历并与之交互)。

谢谢。

英文:

Is there a way for me to use datastore's GetMulti, or another function built into the "appengine/datastore" package, to get all entities of a single kind?

For instance, I have a kind "Queue" with many entities that have two to three properties. Each entity has a unique stringID and what I'm trying to get is a slice or other comparable data type of each unique stringID.

The purpose of Queue is to store some metadata and the unique key names that I'll be looping over and performing a cron task on (e.g. keys "user1", "user2", and "user3" are stored as kind Queue, then - during cron - are looped over and interacted with).

Thanks.

答案1

得分: 1

我是新手使用Google App Engine,没有在深入了解文档之前就开始使用。现在我实际上已经阅读了文档,看起来我将回答自己的问题。可以通过简单的查询、循环遍历键,并将每个键的StringID添加到字符串切片中来实现:

var queuedUsers []string

q := datastore.NewQuery("Queue").KeysOnly()
keys, _ := q.GetAll(c, nil)
for _, v := range keys {
    queuedUsers = append(queuedUsers, v.StringID())
}
英文:

I'm new to Google App Engine and I didn't read the documentation before diving in. Now that I actually read the docs, it looks like I'll be answering my own question. This can be accomplished via a simple query, looping over the Keys, and appending the StringID of each key to a slice of strings:

var queuedUsers []string

q := datastore.NewQuery("Queue").KeysOnly()
keys, _ := q.GetAll(c, nil)
for _, v := range keys {
	queuedUsers = append(queuedUsers, v.StringID())
}

huangapple
  • 本文由 发表于 2014年12月8日 08:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/27349445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定