I have to perform an operation for each document of a collection on a specific day of the month and save the results in other documents from different collections, my question would be which is the best way to do this, it would be with a bash script, a cron in nodejs.
What I do:
I bring all the files and keep them in two different collections while doing the cycle.
Problems:
-
I do not know how many documents are (possibly many) so if one fails how it could recover the cycle?
-
When loading the files in memory in case there are too many, would it be better to do this with a''''worker''?
I have used cron already worked but I can not understand if at that time the task will be repeated constantly since apparently it do, I leave an example of my code
pepinosModelo.find()
.exec()
.then(pepinos => {
pepinos.forEach(pepino => {
let frecuencia = pepino.frecuente();
let nuevoTrans = [{
chocolate:true,
abena:"de dos a tres porcines",
lsd: "No aplica"
}];
nuevoTrans.push({
chocolate:false,
coco:"Completo",
lsd: "Aplica"
});
return TransModel.create(nuevoTrans)
.then((trans) => {
fumadorModelo
.update({
pepino: pepino._id
}, {
'comprador': false,
'nuevaDocis': zero
})
.exec()
.then((usuario) => {
pepino.cambio += frecuencia;
pepino.balance = zero;
pepino.puntos = zero;
return pepino
.save()
.then(() => {
console.log(´fin actualizacion usuario ciclo ${usuario}´);
});
});
});
});
});