Google app engine + Go + datastore + add / update / delete record

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

Google app engine + Go + datastore + add / update / delete record

问题

以下是如何向DataStore插入记录的代码示例:

type User struct {
    Id    string
    Name  string
    Pass  string
    Email string
}

user := User{
    Id:    "4be0f045-6ce8-4d3e-751e-15bd84f2b925",
    Name:  "test",
    Pass:  "test",
    Email: "",
}

datastore.Put(context, datastore.NewIncompleteKey(context, "User", nil), &user)

以下是如何更新或删除记录的示例代码。所有的示例都使用了一个"key"。我需要像下面这样简单的代码(伪代码):

user.Email = "new@email.com"
filter := fmt.Sprintf("Id='%s'", "4be0f045-6ce8-4d3e-751e-15bd84f2b925")
datastore.Put(context, "User", user, filter, &user)

另外,可能相关的是,如何在执行以下操作后获取键:

querySize := 1000
query := datastore.NewQuery("User").Limit(querySize)
queryResult := make([]User, 0, querySize)
query.GetAll(context, &queryResult)

"queryResult"包含了所有记录,它们对应的键是什么?这样我就可以在需要时进行更新。

英文:

Here is how I insert records in to DataStore

type User struct {
    Id string
    Name string
    Pass string
    Email string
}

user := User {
    Id:     "4be0f045-6ce8-4d3e-751e-15bd84f2b925",
    Name:   "test",
    Pass:   "test",
    Email:  "",
}

datastore.Put(context, datastore.NewIncompleteKey(context, "User", nil), &user)

How do I update or delete a record ? All the examples I see use some "key". I need simple things like (pseudo code):

user.Email = "new@email.com"
filter := string.Format("Id='{0}'", "4be0f045-6ce8-4d3e-751e-15bd84f2b925");
datastore.Put(context, "User", user, filter ), &user)

Also, probably related, how to get keys after I do

querySize := 1000
query := datastore.NewQuery("User").Limit(querySize)
queryResult := make([]User, 0, querySize)
query.GetAll(context, &queryResult)

"queryResult" contains all the records, what's their corresponding keys ? so I can do an update if I want to.

答案1

得分: 4

GetAll返回一个键的数组,如果出现错误则返回一个错误。只需遍历键即可。
https://developers.google.com/appengine/docs/go/datastore/reference#Query.GetAll

您可以使用Delete而不是Put来删除记录。这是Go的App Engine Datastore参考,解释了所有操作:
https://developers.google.com/appengine/docs/go/datastore/reference

并且请查看Google的这个示例。它展示了如何获取键并更新或删除记录:
https://github.com/GoogleCloudPlatform/appengine-angular-gotodos/blob/master/gotodos.go

英文:

GetAll returns an array of keys and in case of an error an Error. Just iterate over the keys.
https://developers.google.com/appengine/docs/go/datastore/reference#Query.GetAll

You can delete a record with Delete instead of Put. Here is the App Engine Datastore reference for Go that explains all operations:
https://developers.google.com/appengine/docs/go/datastore/reference

And have a look at this example from Google. It shows how to get keys and update or delete records:
https://github.com/GoogleCloudPlatform/appengine-angular-gotodos/blob/master/gotodos.go

答案2

得分: 4

为了更新或删除数据存储项,我们需要该项的键。

查询 := datastore.NewQuery("User").Filter("ID =", "4be0f045-6ce8-4d3e-751e-15bd84f2b925")
var u []User
key, err := query.GetAll(context, &u)

现在,我们有一个数组u,其中包含与过滤器id = 4be...匹配的所有条目,这种情况下u只有一个条目,并且我们有一个键的数组key,其中包含相应的条目键。

要更新条目:

u[0].Email = "new.email@org"
datastore.Put(context,key[0],&u[0])

这将为u的第一个条目分配新的电子邮件,并将更改后的条目存储在数据存储中。

要删除条目:

datastore.Delete(context,key[0])

英文:

In order to update or delete a datastore entry, we need the key for the entry in question.

query := datastore.NewQuery("User").Filter("ID =", "4be0f045-6ce8-4d3e-751e-15bd84f2b925")
var u []User
key, err := query.GetAll(context, &u)

Now we have an array u with all entries that match the filter id = 4be..., in this case u has only one entry, and we have an array of keys, key, with the corresponding key for the entry.

To update the entry:

u[0].Email = "new.email@org"
datastore.Put(context,key[0],&u[0])

This assigns the first entry of u the new email and stores the changed entry in the datastore.

To delete the entry:

datastore.Delete(context,key[0])

huangapple
  • 本文由 发表于 2013年11月11日 12:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/19899080.html
匿名

发表评论

匿名网友

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

确定