简体   繁体   中英

Node JS read file insert to mongo db

I have a text file with this content

title: A, alert: notice, desc: Starting
title: B, alert: notice, desc: Process Step 1 and 2
 Step 1 - Execute
 Step 2 - Log
title: C, alert: notice, desc: "Ending"

I want to insert it to my mongo db with this format

{
 title: A,
 alert: notice,
 desc: Starting
},
{
 title: B,
 alert: notice,
 desc: Process Step 1 and 2 Step 1 - Execute Step 2 - Log
},
{
 title: C,
 alert: notice,
 desc: Ending
},

I've been trying to find node packages that could help but almost all file reader packages can only read the file and not allow line by line editing. For example if the next line is indented it should merge with previous line.

const fs = require('fs');
const rl = require('readline');
const mongoose = require('mongoose');

let readline = [];
let readlinesplit = [];
let finalarrobj = [];
let keyarr = [
  'variable',
  'identification',
  'value',
  'unit',
  'status',
  'date',
  'time'
];
let splitline = [];
let filenamearr = []
let getfile;
let scSchema = new mongoose.Schema(
  {
    variable: {
      type : String
    },
    identification: {
      type : String
    },
    value: {
      type : String
    },
    unit: {

    },
    status: {
      type : String 
    },
    date: {
      type: Date
    },
    time: {
      type: Date
    }  
  }
)
const SC = mongoose.model('SC', scSchema);

try {
  fs.readdir('D:/scnner_data/public', (err, data) => {
    if (err) throw err;
    data.forEach((element) => {
      if (element.match(/SC/)) {
        filenamearr.push(element);
      }
    });
    filenamearr.map(async (element) => {
      getfile = fs.createReadStream(`public/${element}`);
      readline = rl.createInterface({
        input: getfile,
        crlfDelay: Infinity 
      });
      for await (let line of readline) {
        splitline.push(line.split('\n'));
      };
      splitline.forEach((element) => {
        readlinesplit.push(element[0].split(';'));
      });
      for (let i = 0; i < readlinesplit.length; i++) {
        readobj = {};
        for (let j = 0; j < keyarr.length; j++) {
          readobj[keyarr[j]] = readlinesplit[i][j];
        };
        finalarrobj.push(readobj);
      };
      try {
        SC.insertMany(finalarrobj);
        console.log('data inserted for SC');
      } catch (error) {
        throw  error.message;
      }
    });
  })
} catch(e) {
   throw e.message
}

you can use this approach to read your directory and store data this is just a basic approach to insert data from text file to databse. i hopw this may help you

This will be a two step process. The first will be parsing your file. It doesn't appear to be in any common standard (that I'm aware of) so you'll likely need to write this yourself.

The second part will be writing files to a mongodb. This is a separate issue, though if you follow the documentation it's fairly straightforward. I'd suggest you try these things and then ask again if you have any problems.

Edit: Here's the file parsed to a js object: https://jsfiddle.net/cyksd7o3/

 var fileInput = 'title: A, alert: notice, desc: Starting\ntitle: B, alert: notice, desc: Process Step 1 and 2 Step 1 - Execute Step 2 - Log \ntitle: C, alert: notice, desc: "Ending"'; var parseFile = function(input){ var rows = input.split('\n'); var returnObj = []; for (var i=0; i < rows.length; i++){ var currRow = rows[i]; returnObj.push(getDataObject(currRow)); } return returnObj; }; var getDataObject = function(inputRowString){ var newObject = {}; var propSplit = inputRowString.split(','); for (var i = 0; i < propSplit.length; i++){ var currSplit = propSplit[i]; var keyValue = currSplit.split(':'); newObject[keyValue[0].replace(/\s/g,'')] = keyValue[1]; } return newObject; }; console.dir(parseFile(fileInput));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM