Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
Get failed rows when using Mongoose Model.insertMany

We are trying to import a csv with around 30.000 lines of data into a MongoDB database using Mongoose. We have created a model with some validators so only correct rows will be added to the database.

First we read the csv with papaparse and create an array out of it. Next we insert the data into the database with the mongoose Model.insertMany method. To continu inserting data when a row fails we use the option ordered: false.

This all works but we are looking for a way to collect the failed rows so we can check why a row didn't get passed the validation.

Is there a way to get the failed rows with the insertMany method?

...

const readCSV = async (filePath) => {
    const csvFile = fs.readFileSync(filePath);
    const csvData = csvFile.toString();

    return new Promise(resolve => {
        Papa.parse(csvData, {
            header: true,
            transformHeader: header => header.trim(),
            complete: results => {
                console.log('Complete', results.data.length, 'records.'); 
                resolve(results.data);
            }
        });
    });
};

const start = async () => {
    try {
        await connectDB(process.env.DATABASE_URL);
        const parsedData = await readCSV(csvFilePath);
        const response = await Company.insertMany(parsedData, { ordered: false });
        console.log(response);
    } catch(error) {
        console.log(error);
    }
}

start();
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda