Datsik
Datsik

Reputation: 14824

How to delete an element in an array with 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,
})

Upvotes: 4

Views: 840

Answers (1)

mlucy
mlucy

Reputation: 5289

You probably want something like this:

r.db('gofinder').table('Lobbys').get(ID).update(function(row) {
  return {members: row('members').setDifference(['Gacnt'])};
})

Upvotes: 7

Related Questions