0

I am new to MySQL and I wonder how to approach the following proplem. I have an object with:

{
  id: number,
  names: [string]
}

If I wanted to insert this object, would I have to insert it in a for loop, since arrays should not be stored in one single cell?

for(let i=0; obj.names.length > i; i++){
  this.service.postObj({id: obj.id, names: obj.names[i]});
}

Would it be the right way? If so, what if I had to iterate through all properties representing an array? As far as I know now, I would have to do it like above but it is really inefficient. Do I oversee something?

I use the MEAN stack (here MySQL, Express, Angular and Nodejs).

Logik
  • 219
  • 2
  • 9
  • 1
    Maybe this link may be helpful: [how-do-i-do-a-bulk-insert-in-mysql-using-node-js](https://stackoverflow.com/questions/8899802/how-do-i-do-a-bulk-insert-in-mysql-using-node-js) – BENARD Patrick May 19 '20 at 12:29
  • Thank you for helping me! I will read it! – Logik May 19 '20 at 12:30

1 Answers1

1

Either:

  • Add a row for each item in the array. This would mean each item gets a unique ID
  • Create another table with a many-to-one relation
Dendrowen
  • 27
  • 1
  • 8