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

190
Vistas
MongoDB Atlas multiple triggers for counters

im trying to make a counters collection. So far Ive managed to make the increment part with a Trigger (in Mongo Atlas) and use that number to set an autoincreased field, but what im trying to do is decrease the counter when I delete a document (in this case, a category). My trigger to add 1 works fine like so:

const countercollection = context.services.get("Cluster0").db(changeEvent.ns.db).collection("counters");
    const categoriescollection = context.services.get("Cluster0").db(changeEvent.ns.db).collection(changeEvent.ns.coll);
    
    var counter = await countercollection.findOneAndUpdate({_id: changeEvent.ns },{ $inc: { category_counter: 1 }}, { returnNewDocument: true, upsert : true});
    
    var updateRes = await categoriescollection.updateOne({_id : docId},{ $set : {categoryId : counter.category_counter}});

I tought this would be possible doing kind of the same that I did to add but changing to something like:

$inc: { item_counter: -1 }

For some reason i cant make it work with both triggers enabled. Is it one trigger per collection or some restriction like that? Should i approach it differently? (maybe with a ModelSchema.pre()?) Thanks in advance!

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