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.
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.
The mongoose model used :
const mongoose = require("mongoose");
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("csvRecords", csvSchema);
The node.js snippet used to insert the CSV file in JSON format :
app.post("/upload-csv", uploads.single("csv"), (req, res) => {
//convert csvfile to jsonArray
csv()
.fromFile(req.file.path)
.then((jsonObj) => {
//insertmany is used to save bulk data in database.
//saving the data in collection(table)
sheetModel.insertMany(jsonObj, (err, data) => {
if (err) {
res.status(400).json({
message: "Something went wrong!",
});
} else {
res.status(200).json({
message: "File Uploaded Successfully!",
result: data,
});
}
});
});
});