Delete object from an array (RethinkDB)

0

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 .

    
asked by Antonio Roman 08.05.2017 в 13:18
source

1 answer

0

Solution: Access the table r.table(table) , get the document to update .get(id) and update it .update() . The parameters to update is the name of the property of the object that has the array to edit. For example, if it is called 'scores' , then we will edit { scores: [...{}] } .

Now the fun part: We get the row / property r.row(uArray) that we want to update ( 'scores' in my case) and filter their entries .filter(() => ...) . The condition is that the property 'id' of each object must not be equal to the value we want to eliminate, that is, the variable index . As a result, the filter must be item => item('id').ne(index) , that is, for each item in the array, access the property 'id' and filter those whose value does not equal% 'index' , therefore, you would have as a result array but without the / s element whose property 'id' is different from the one that was given; In other words, all the elements except the one we want

/**
 * Borra un elemento de un array dado el documento seleccionado por su 'id', el nombre del array en cuestión, y la propiedad 'id' del objeto dentro del array.
 * @param {string} table El nombre de la tabla.
 * @param {string|number} id La ID del documento a actualizar.
 * @param {string} uArray El nombre de la propiedad del documento el cual contiene un array.
 * @param {string} index El valor de la propiedad 'id' que debe tener el objeto a ser borrado del array.
 * @returns {Promise<Object>}
 */
removeFromArrayByID(table, id, uArray, index) {
    return this.db.table(table).get(id).update({ [uArray]: this.db.row(uArray).filter(it => it('id').ne(index)) }).then(resolvePromise);
}
    
answered by 14.12.2017 / 20:35
source