dongnaoxia0927 2016-01-03 05:29
浏览 100
已采纳

如何使用RethinkDB删除数组中的元素

I have a table that contains Lobbys which are essentially just Party Rooms, it has a Members array and a Messages array, this is an example:

{
"id":  "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" ,
"members": [
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt" ,
"Gacnt"
] ,
"messages": [ ]
}

Right now when a users websocket connects to the Lobby it will add their username to the Members list, and I'm trying to make it so that when the user leaves the Party it removes them from the members list, I came up with this:

r.db("gofinder").table("Lobbys").get("a77be9ff-e10f-41c1-8a4c-66b5a55d823c")("members").update({
  members: r.db("gofinder").table("Lobbys").get("a77be9ff-e10f-41c1-8a4c-66b5a55d823c")("members").ne("Gacnt")
})

But I get the error:

e: Could not prove argument deterministic.  Maybe you want to use the non_atomic flag? in:
r.db("gofinder").table("Lobbys").get("a77be9ff-e10f-41c1-8a4c-66b5a55d823c")("members").update({"members": r.db("gofinder").table("Lobbys").get("a77be9ff-e10f-41c1-8a4c-66b5a55d823c")("members").ne("Gacnt")})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

But obviously there is an error, I'm new to Rethink so I'm not sure exactly how to do this, unless I'm supposed to select the array, modify it myself and then put the new array back in.

Right now I am currently using this solution but I feel like it could probably be simpler, especially if I could leave it to ReQL to modify the array instead of having to do it manually

This is my current solution and not relevant to the question

query = gorethink.Table("Lobbys").Get(userinfo.LobbyID).Field("members")
res, err := query.Run(db)
if err != nil {
    log.Println(err)
}
defer res.Close()
var members []string
if err = res.All(&members); err != nil {
    log.Println(err)
}

for i := 0; i < len(members); i++ {
    if members[i] == userinfo.Username {
        members = append(members[:i], members[i+1:]...)
        i--
    }
}

query = gorethink.Table("Lobbys").Get(userinfo.LobbyID).Update(map[string]interface{}{
    "members": members,
})
  • 写回答

1条回答 默认 最新

  • doumeng9188 2016-01-03 05:50
    关注

    You probably want something like this:

    r.db('gofinder').table('Lobbys').get(ID).update(function(row) {
      return {members: row('members').setDifference(['Gacnt'])};
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果