'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!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
