Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!