英文:
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:
{
"id": "8ae9865-b602-41f7-8637-9fd5517095d9",
"members": {
"12345": {
"Id": 12345,
"Name": "User1",
"LastUpdated": timestamp
}
}
}
My query:
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)
It gives the following error:
gorethink: Expected type SELECTION but found 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>))
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论