I work with Node.js , I'm trying to delete an object from my RethinkDB database, however, I do not know what methods to use to remove the object with the condition id = index
, as I do in the following code to edit a certain object from an array.
static async updateArray(table, id, uArray, index, doc) {
if (typeof index === "number") {
return r.table(table).get(id).update({ [uArray]: r.row(uArray).changeAt(index, r.row(uArray).nth(index).merge(doc)) }).run();
}
return r.table(table).get(id).update({ [uArray]: r.row(uArray).map(d => r.branch(d("id").eq(index), d.merge(doc), d)) }).run();
}
However, now I want the same thing, but removing the object in the same way I edit with the code above. For now the following code works, but I need to remove the object where condition id = index
is fulfilled.
static async removeFromArray(table, id, uArray, index) {
if (typeof index === "number") {
return r.table(table).get(id).update({ [uArray]: r.row(uArray).deleteAt(index) }).run();
}
}
Additionally: the use I make with updateArray is as follows: .updateArray("localScores", this.guild.id, "scores", this.id, { score });
, which would update the table localScores
, get the document in which the ID corresponds to this.guild.id
, modify the array scores
, get the document whose ID corresponds to this.id
, and modify the document.
{
"id": "256566731684839428",
"scores": [
{
"id": "178951279190605824",
"points": 2000
},
{
"id": "251484593859985411",
"points": 14300
},
{
"id": "211591629486686208",
"points": 15
}
]
}
Well now, what I want is that with the removeFromArray function, I can delete the following object:
{
"id": "251484593859985411",
"points": 14300
}
That is, when doing .removeFromArray("localScores", "256566731684839428", "scores", "251484593859985411");
, the function must delete the object from the array. And for that, I need the function to eliminate the object of the array in which id
equals 251484593859985411
.