Estoy insertando datos del archivo CSV
, quiero actualizar si la clave del objeto tiene la misma, entonces el nuevo precio solo se actualizará, he usado csvtojson
para analizar archivos CSV y multer
para cargar archivos.
El archivo CSV tiene el número de artículo, el título y la columna de precio
muestra.csv
itemNumber,title,price STY0047,Men Straight Fit Jeans,2500 STY0048,Men Straight Fit Jeans,2450 STY0049,Men Straight Fit Jeans,2550 STY0050,Men Straight Fit Jeans,3000 STY0048,Men Straight Fit Jeans,4000 STY002,Men Straight Fit Jeans,2550 STY0053,Men Straight Fit Jeans,3000 STY0054,Men Straight Fit Jeans,3000 STY0043,Men Straight Fit Jeans,4000
Mi código
router.post("/file-insert-data", upload, async function (req, res, next) { if (!req.file) { return res.status(400).send("no file found"); } const results = await csv().fromFile(req.file.path); for (let obj in results ) { const csvData = new csvModel(results [obj]) csvData.save((err, data) => { if(err) { return res.status(400).send("csv data is not inserted") } return res.status(201).send(data) }) } });
ayúdame gracias
ACTUALIZADO
Después de una colaboración con el OP, aquí hay una versión compatible con mangostas que se alinea con la pregunta original. Destaca la funcionalidad upsert
y presenta la adición genérica de nuevos campos.
results.forEach(function (item) { var prx = item['price']; // capture price.... // ...and now REMOVE it. This is because fields cannot appear in // both $set and $setOnInsert. Our trick here is to make the $setOnInsert // generic. ANY fields that are NOT itemNumber or price will automatically // be added upon first insert. It should be obvious that if you need // to upsert more things (ie more things to $set), then delete them from // the object too. Note: $setOnInsert will only store the fields from // item upon first insert. New fields in later items with an existing // key will NOT be updated. Comment out the delete and see the error // message re. path conflict: delete item['price']; rc = csvModel.findOneAndUpdate( { itemNumber: item["itemNumber"] }, { $set: { price: prx }, $setOnInsert: item // Ah! Set the WHOLE OBJECT. }, { upsert: true } ); rc.then(data => data) });
findOne
para verificar si el elemento ya existe (supongo que itemNumber
es clave para el modelo).csvModel
router.post('/file-insert-data', upload, async function (req, res, next) { if (!req.file) { return res.status(400).send('no file found'); } try { const results = await csv().fromFile(req.file.path); let updatedData = []; // Array to store the updated data for (const item of results) { const { itemNumber, title, price } = item; let csvData = await csvModel.findOne({ itemNumber }); if (!csvData) { // Create new data if not present csvData = new csvModel(); csvData.itemNumber = itemNumber; csvData.title = title; } // Update the price csvData.price = price; // Save the created / updated data await csvData.save(); updatedData.push(csvData); } return res.status(201).send(updatedData); } catch (err) { return res.status(500).send('Error updating data'); } });