duandaodao6951 2017-10-08 18:57
浏览 13
已采纳

如何在Go Lang中将实体追加到数据存储

Need info on how to append an entity in Google cloud data store.

Put function is always overwritten with new values.

Any suggestions would be helpful.

09-OCT-2017:

I Used below code, but still updating entity instead of appending.(its erasing old & updated with new value, but i want to retain both values)

client, err := datastore.NewClient(ctx, projectID)
tx, err := client.NewTransaction(ctx)
if err != nil {
    log.Fatalf("Failed to create client: %v", err)
}

fmt.Fprint(w, input)
taskKey := datastore.NameKey("Entity", "stringID", nil)
var task Entity
if err := tx.Get(taskKey, &task); err != nil {
    log.Fatalf("tx.Get: %v", err)
}
task.Value = input
if _, err := tx.Put(taskKey, &task); err != nil {
    log.Fatalf("tx.Put: %v", err)
}
if _, err := tx.Commit(); err != nil {
    log.Fatalf("tx.Commit: %v", err)
}
  • 写回答

2条回答 默认 最新

  • dpylt7626401 2017-10-09 08:49
    关注

    There can be only one instance, only one entity bound / denoted by the same datastore key.

    And entities (bound to a key) can only be overwritten, not updated / extended / appended gradually.

    So if you already have an entity saved, to update/ modify it, you have to first load it, then modify the entity in memory, and write out (save) the modified entity. This save will overwrite the existing entity in the datastore.

    If for a property you want to store multiple values, the type of that property must support storing multiple values. Slices in Go are such types.

    So in your example your entity should look like this:

    type Entity struct {
        Values []string 
    }
    

    When you load an existing Entity, you have to append the new value to its Values field, something like this (in pseudo code):

    e := ... // load existing entity
    e.Values = append(e.Values, input) // Append new data to Values
    // And now save Entity (e) with the same key
    

    In code:

    client, err := datastore.NewClient(ctx, projectID)
    tx, err := client.NewTransaction(ctx)
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }
    
    fmt.Fprint(w, input)
    taskKey := datastore.NameKey("Entity", "stringID", nil)
    var task Entity
    if err := tx.Get(taskKey, &task); err != nil {
        log.Fatalf("tx.Get: %v", err)
    }
    task.Values = append(task.Values, input)
    if _, err := tx.Put(taskKey, &task); err != nil {
        log.Fatalf("tx.Put: %v", err)
    }
    if _, err := tx.Commit(); err != nil {
        log.Fatalf("tx.Commit: %v", err)
    }
    

    If you need to index by this Values property, you might run into troubles if it contains many values. See this possible duplicate for more details: App Engine Datastore: How to set multiple values on a property using golang?

    If you run into this problem, you should consider modeling and storing your data in a different format, e.g. saving in multiple entities, where one entity would only store a single input, connected with the key it belongs to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM