automatic consultations to mongodb once a month

3

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}´);
          });
        });
      });
    });
  });
    
asked by soldat25 27.12.2016 в 17:28
source

2 answers

1

maybe this will help you:

link

Greetings

    
answered by 27.12.2016 в 21:06
0

I've taken this package: node-cron . You could use a loop to work a number of files. If one fails, it handles the exceptions and at the end generates a report of failed files or updates.

While your server has good features you can support n number of files.

The use is simple, I give an example any:

var cron = require('node-cron');

var manejoDeArchivos = require('./server/services/v1/manejoDeArchivos');
cron.schedule('0 0 1 * *', function () {
    manejoDeArchivos.execute();
});

Then in the fileArchive.js file you can read the files with a package, if they are csv I recommend fast-csv and work with mongoose.

var mongoose = require('mongoose'),
    User = mongoose.model('User');

exports.execute = function () {

    var data = {
        "puntos.temporales" : 600
    };
    User.update({isActive: true}, {$set: data}, {multi: true}, function (err, docs) {
        if(err)
            console.error('Errot at executing cron job. Error: ' + err, {colors: true});
        if(docs) {
            console.log(docs);
            console.dir('Cron job executed correctly', {colors: true});
        }
    })
};
    
answered by 04.01.2017 в 17:06