Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda