在RethinkDB中更新Do函数中的对象。

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

Update object in Do-function in RethinkDB

问题

rooms表中的示例数据:

{
    "id": "8ae9865-b602-41f7-8637-9fd5517095d9",
    "members": {
        "12345": {
            "Id": 12345,
            "Name": "User1",
            "LastUpdated": timestamp
        }
    }
}

我的查询:

result, err := r.DB(rethinkdbDatabase).Table("rooms").Filter(
    r.Row.Field("members").HasFields(playerIdStr)).Nth(0).Default(nil).Do(func(room r.Term) interface{} {
    return r.Branch(
        room.Ne(nil),
        room.Update(map[string]interface{}{
            "LastUpdate": r.Now(),
        }),
        r.DB(rethinkdbDatabase).Table("rooms").Insert(map[string]interface{}{
            "id": r.UUID(),
            "members": map[string]interface{}{
                playerIdStr: map[string]interface{}{
                    "LastUpdate": r.Now(),
                    "Name":       request.Player.Name,
                    "Id":         request.Player.Id,
                },
            },
        }, r.InsertOpts{
            Durability: "hard",
        }),
    )
}).Run(rethinkdbClient)

它给出了以下错误:

gorethink: 期望类型为 SELECTION,但找到 DATUM:
{
    "id":   "08ae9865-b602-41f7-8637-9fd5517095d9",
    "members":      {
    "7360165":      {
    "Id":   7360165,
    "LastUpdate":   {
    "$reql_type$":  "TIME",
    "epoch_time":   1486826898.51,
    "timezone":     "+00:00"
    },
    "Name": "Player1"
    }
    }
} in:
r.Do(func(var_10 r.Term) r.Term { return r.Branch(var_10.Ne(<nil>), var_10.Update({LastUpdate=r.Now()}), r.DB("drill_dev").Table("rooms").Insert({id=r.UUID(), members={7360165={LastUpdate=r.Now(), Name="Player1", Id=7360165}}}, durability="hard")) }, r.DB("drill_dev").Table("rooms").Filter(func(var_9 r.Term) r.Term { return r.Row.Field("members").HasFields("7360165") }).Nth(0).Default(<nil>))

据我理解,问题似乎是room变量的类型为OBJECT,与更新函数不兼容,因此调用room.Update({...})无法工作。如何在不再使用room变量重新过滤表的情况下更新嵌套文档中的"LastUpdated"字段?

英文:

Example data in table rooms:

{
    &quot;id&quot;: &quot;8ae9865-b602-41f7-8637-9fd5517095d9&quot;,
    &quot;members&quot;: {
        &quot;12345&quot;: {
            &quot;Id&quot;: 12345,
            &quot;Name&quot;: &quot;User1&quot;,
            &quot;LastUpdated&quot;: timestamp
        }
    }
}

My query:

        result, err := r.DB(rethinkdbDatabase).Table(&quot;rooms&quot;).Filter(
            r.Row.Field(&quot;members&quot;).HasFields(playerIdStr)).Nth(0).Default(nil).Do(func(room r.Term) interface{} {
            return r.Branch(
                    room.Ne(nil),
                    room.Update(map[string]interface{}{
                            &quot;LastUpdate&quot;: r.Now(),
                    }),
                    r.DB(rethinkdbDatabase).Table(&quot;rooms&quot;).Insert(map[string]interface{}{
                            &quot;id&quot;: r.UUID(),
                            &quot;members&quot;: map[string]interface{}{
                                    playerIdStr: map[string]interface{}{
                                            &quot;LastUpdate&quot;: r.Now(),
                                            &quot;Name&quot;:       request.Player.Name,
                                            &quot;Id&quot;:         request.Player.Id,
                                    },
                            },
                    }, r.InsertOpts{
                            Durability: &quot;hard&quot;,
                    }),
            )
    }).Run(rethinkdbClient)

It gives the following error:

gorethink: Expected type SELECTION but found DATUM:
{
    &quot;id&quot;:   &quot;08ae9865-b602-41f7-8637-9fd5517095d9&quot;,
    &quot;members&quot;:      {
    &quot;7360165&quot;:      {
    &quot;Id&quot;:   7360165,
    &quot;LastUpdate&quot;:   {
    &quot;$reql_type$&quot;:  &quot;TIME&quot;,
    &quot;epoch_time&quot;:   1486826898.51,
    &quot;timezone&quot;:     &quot;+00:00&quot;
    },
    &quot;Name&quot;: &quot;Player1&quot;
    }
    }
} in:
r.Do(func(var_10 r.Term) r.Term { return r.Branch(var_10.Ne(&lt;nil&gt;), var_10.Update({LastUpdate=r.Now()}), r.DB(&quot;drill_dev&quot;).Table(&quot;rooms&quot;).Insert({id=r.UUID(), members={7360165={LastUpdate=r.Now(), Name=&quot;Player1&quot;, Id=7360165}}}, durability=&quot;hard&quot;)) }, r.DB(&quot;drill_dev&quot;).Table(&quot;rooms&quot;).Filter(func(var_9 r.Term) r.Term { return r.Row.Field(&quot;members&quot;).HasFields(&quot;7360165&quot;) }).Nth(0).Default(&lt;nil&gt;))

To my understanding, the issue appears to be, that the room variable is of type OBJECT which is not compatible with the update function, therefore the call room.Update({...}) does not work. How can I update the "LastUpdated" field in the nested document without having to filter the table again, using the room variable?

答案1

得分: 1

因为文档 room 有一个 id,所以你可以通过 id 选择文档(而不是再次过滤整个表格),然后进行更新。

英文:

Because the document room has an id, you can select the document by id (instead of filtering the whole table a second time) and update it that way.

huangapple
  • 本文由 发表于 2017年2月12日 00:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/42178117.html
匿名

发表评论

匿名网友

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

确定