title: Uploading CSV files as MongoDB documents using node.js
body: <p>I am trying to save multiple CSV files into different MongoDB collections in JSON format using node.js express and mongoose, and then compare these files, currently, I managed to upload the CSV files to the MongoDB database but as documents, so every time I upload a new one it just gets added along with the other documents.
  which will make comparing the CSV files impossible cause you can't tell which documents will belong to what file.</p>
  <p>A solution that seemed logical to me was to upload the CSV files as separate Collections and then compare the collections is this the best possible solution if so how could I implement that.</p>
  <p>The mongoose model used :</p>
  <pre><code>const mongoose = require(&quot;mongoose&quot;);
  
  const csvSchema = new mongoose.Schema({
    Sr: {
      type: String,
      required: true,
    },
    Cheque_No: {
      type: String,
      required: true,
    },
    Amount: {
      type: String,
      required: true,
    },
  });
  
  module.exports = mongoose.model(&quot;csvRecords&quot;, csvSchema);
  
  </code></pre>
  <p>The node.js snippet used to insert the CSV file in JSON format :</p>
  <pre><code>app.post(&quot;/upload-csv&quot;, uploads.single(&quot;csv&quot;), (req, res) =&gt; {
    //convert csvfile to jsonArray
    csv()
      .fromFile(req.file.path)
      .then((jsonObj) =&gt; {
        //insertmany is used to save bulk data in database.
        //saving the data in collection(table)
        sheetModel.insertMany(jsonObj, (err, data) =&gt; {
          if (err) {
            res.status(400).json({
              message: &quot;Something went wrong!&quot;,
            });
          } else {
            res.status(200).json({
              message: &quot;File Uploaded Successfully!&quot;,
              result: data,
            });
          }
        });
      });
  });
  </code></pre>