0

I want to do a bulk insert into MySQL. I have followed this question as well for understanding. The user has posted an answer which takes array of data and then inserting into DB.

I have a data stored like this

'116': 
   {
     lat: '31.53867',
     long: '74.41279666666667',

     timestamp: '2018-07-28 15:56:15'
     },
  '117': 
   { 
     lat: '31.53863',
     long: '74.41278',
     timestamp: '2018-07-28 15:56:20'
     },
  '118': 
   { 
     lat: '31.538645',
     long: '74.41285',

     timestamp: '2018-07-28 15:58:39'
     }
   }

Since I am new in node so I have no idea how can I put whole dictionary into the database.

Or If not possible then how can I convert data to this format so It can be inserted into query

var values = [
    ['31.53863', '74.41285', '2018-07-28 15:58:39'],
    ['31.53863', '74.41285', '2018-07-28 15:58:39'],
    ['31.53863', '74.41285', '2018-07-28 15:58:39']

];

Any help would be really appreciated.

user1hjgjhgjhggjhg
  • 1,237
  • 4
  • 15
  • 34

1 Answers1

0

You can use for in loop to create an array like this:

var values = [
    ['31.53863', '74.41285', '2018-07-28 15:58:39'],
    ['31.53863', '74.41285', '2018-07-28 15:58:39'],
    ['31.53863', '74.41285', '2018-07-28 15:58:39']
];

Try this

var oldData = [{
        lat: '31.53867',
        long: '74.41279666666667',
        timestamp: '2018-07-28 15:56:15'
    },
    {
        lat: '31.53863',
        long: '74.41278',
        timestamp: '2018-07-28 15:56:20'
    },
    {
        lat: '31.538645',
        long: '74.41285',
        timestamp: '2018-07-28 15:58:39'
    }
];
var newArr = [];
oldData.map(function(obj, index) {
    var val = [];
    for (index in obj) {
        val.push(obj[index]);
    }
    newArr.push(val)
});
console.log(newArr);



// Expected Output: 

/*
[
    ['31.53863', '74.41285', '2018-07-28 15:58:39'],
    ['31.53863', '74.41285', '2018-07-28 15:58:39'],
    ['31.53863', '74.41285', '2018-07-28 15:58:39']
];
*/
Hello World
  • 2,673
  • 7
  • 28
  • 60