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

181
Vistas
How to use mongoose updateMany middleware to increase performance?

SOLVED: SOLUTION AT THE BOTTOM

I have the following Code where I am updating element by element:

//registerCustomers.js
const CustomerRegistrationCode = require("../models/CustomerRegistrationCode");

const setRegCodesToUsed = async (regCodes) => {
  for (let regCode of regCodes) {
    await setRegCodeToUsed(regCode._id);
  }
};

const setRegCodeToUsed = async (id) => {
  await CustomerRegistrationCode.findByIdAndUpdate(id, { used: true });
};

The Code works fine but is to slow and i want to update many (1000) CustomerRegistrationCodes at once. I had a look at the updateMany middleware function but found not much info online and on the official docs. I changed my code to the following but don't know how further.

//registerCustomers.js
const setRegCodesToUsed = async (regCodes) => {
  await CustomerRegistrationCode.updateMany(regCodes);
}
//CustomerRegistrationCode.js
CustomerRegistrationCodeSchema.pre('updateMany', async function (next, a) {
  console.log('amount arguments: ', arguments.length); //is 2
  console.log(arguments); //both parameters are functions.
  next();
});

What would be the best way to update many CustomerRegistrationCodes with 1000 different id's?

SOLUTION, thanks to Murat Colyaran

const setRegCodesToUsed = async (regCodes) => {
  const ids = [];
  regCodes.map(code => ids.push(code._id));
  await setRegCodeToUsed(ids);
};

const setRegCodeToUsed = async (ids) => {
  await CustomerRegistrationCode.updateMany(
    { _id: { $in: ids } },
    { used: true }
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This should work:

//registerCustomers.js
const CustomerRegistrationCode = require("../models/CustomerRegistrationCode");

const setRegCodesToUsed = async (regCodes) => {
    let ids = [];
    regCodes.map((code) => ids.push(code._id.toString()));
    await setRegCodeToUsed(ids);
};

const setRegCodeToUsed = async (ids) => {
    await CustomerRegistrationCode.updateMany(
        { 
            id : { $in: ids }
        },
        {
            used: true
        }
    );
};

Instead of sending a query for every records, we just parse the id and send a bulk request with $in

about 4 years ago · Juan Pablo Isaza Denunciar
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