0

我正在尝试将 csv 文件导入到 mongoDB 中的单个文档中,而不是将 csv 文件导入到多个文档中。我希望该文档是 csv 文件中包含的所有数据的数组。提前谢谢你!

router.post('/upload', (req,res) =>
{
    if(req.files)
    {
      var file = req.files.file
      var filename = file.name
      file.mv('./csvfiles/' + filename, function (err)
      {
        if(err)
          res.send(err)
        else
          res.send("File Uploaded")
      })
      csv().fromFile("./csvfiles/" + filename).then(source =>
      {
        for (var i = 0; i < source.length; i++)
        {
          var oneRow =
          {
            Date: source[i]["Date"],
            Time: source[i]["Time"],
            cpu: source[i]["Core Usage (avg) [%]"],
            CpuAvg: source[i]["Total CPU Usage [%]"],
            Memory: source[i]["Memory Clock [MHz]"],
            PhysicalMemoryAvailable: source[i]["Physical Memory Available [MB]"]
          };
          arrayToInsert.push(oneRow);
          console.log(arrayToInsert);
        }
        var collectionName = 'benchmarkrecords';
        var collection = dbConn.collection(collectionName);
        collection.insertMany(arrayToInsert, (err, result) =>
        {
          if (err){
            console.log(err);
            return res.status(400).json({
              message: 'Upload Failed',
            });
          }
          if(result)
          {
            console.log("Import CSV into database successfully.");
            return res.status(200)
          }
        })
      })
    }
})
  {
    Date: '13.10.2021',
    Time: '54:33.1',
    cpu: '6.3',
    CpuAvg: '6.3',
    Memory: '1796.6',
    PhysicalMemoryAvailable: '9288'
  },
  {
    Date: '13.10.2021',
    Time: '54:35.1',
    cpu: '9.4',
    CpuAvg: '9.4',
    Memory: '1796.6',
    PhysicalMemoryAvailable: '9285'
  },
  {
    Date: '13.10.2021',
    Time: '54:37.1',
    cpu: '11',
    CpuAvg: '11',
    Memory: '1796.6',
    PhysicalMemoryAvailable: '9285'
  },

这是我尝试导入 mongodb 的 csv 文件 </p>

Date,Time,Core Usage (avg) [%],Total CPU Usage [%],Memory Clock [MHz],Physical Memory Available [MB]
13.10.2021,53:54.4,13.6,13.6,1796.6,9198
13.10.2021,53:56.5,12.7,12.7,1796.6,9204
13.10.2021,53:58.5,21.3,21.3,1796.6,9288
13.10.2021,54:00.5,11.2,11.2,1796.6,9294
13.10.2021,54:02.6,5.5,5.5,1796.6,9295
13.10.2021,54:04.6,13.1,13.1,1796.6,9294
13.10.2021,54:06.7,14.6,14.6,1796.6,9292
13.10.2021,54:08.7,16.9,16.9,1796.6,9288
13.10.2021,54:10.7,15.7,15.7,1796.6,9281
13.10.2021,54:12.7,11.2,11.2,1796.6,9277
13.10.2021,54:14.8,7.6,7.6,1796.6,9278
13.10.2021,54:16.8,5.5,5.5,1796.6,9282
13.10.2021,54:18.8,5.7,5.7,1796.6,9285
13.10.2021,54:20.9,9.3,9.3,1796.6,9283
13.10.2021,54:22.9,10.1,10.1,1796.6,9286
13.10.2021,54:24.9,10.2,10.2,1796.6,9288
4

0 回答 0