How to change a record in Mongo through a function and update node?

3

I have a problem. I have a data set inside Mongo, a json that was returned from a request from url pa/ with

app.get('/pa', function (req, res) {
    paController.list(function(resp) {
        res.jsonp(resp);
    });
});

similar to this:

[
  {
    "_id": "579a865cb742f206523961af",
    "codPA": 150440,
    "latPA": 5345345,
    "lonPA": 5345345,
    "qtdPesPA": 37,
    "qtdDiaPA": 0,
    "adminPA": "Caio",
    "nomePA": "RIACH�O 1",
    "capacPA": 8,
    "cidadePA": "Caruaru",
    "estadoPA": "PE",
    "statusPA": true,
    "__v": 0,
    "cadPA": "2016-08-04T19:02:26.616Z"
  },
  {
    "_id": "579a865cb742f206523961b0",
    "codPA": 150441,
    "latPA": 5345345,
    "lonPA": 5345345,
    "qtdPesPA": 47,
    "qtdDiaPA": 0,
    "adminPA": "Caio",
    "nomePA": "RIACH�O 1",
    "capacPA": 8,
    "cidadePA": "Caruaru",
    "estadoPA": "PE",
    "statusPA": true,
    "__v": 0,
    "cadPA": "2016-08-04T19:02:26.616Z"
  },
  {
    "_id": "579a865cb742f206523961b1",
    "codPA": 150442,
    "latPA": 5345345,
    "lonPA": 5345345,
    "qtdPesPA": 188,
    "qtdDiaPA": 0,
    "adminPA": "Caio",
    "nomePA": "RIACH�O 1",
    "capacPA": 10,
    "cidadePA": "Caruaru",
    "estadoPA": "PE",
    "statusPA": true,
    "__v": 0,
    "cadPA": "2016-08-04T19:02:26.617Z"
  }
]

Given that I have a regression function, to change the log of all fields qtdDiaPA. I am not able to do a function to update the field in question for each record within the Mongo.

I want a node function that applies a linear regression that has been calculated from this same dataset in the qtdDiaPA field. I invoke this calculation through a request, however I want to put it into a timeout every 6 hours to calculate the regression and then apply it to the bank in the qtdDiaPA record that receives the regression calculation and updates another function that goes check if some date parameters of the Delivery set containing a date field will be made a difference calculation of the current date for the days calculated and entered in qtdDiaPA.

In summary, I want to get a json, access its values, change them. Then put it back into an object and have it updated in the database with qtdDiaPA getting the calculation of my regression for each codPA in the mongo. That's it.

The function I tried to develop is this, but it does not perform the modifications in the file.

exports.atua = function(data, palavra) {

    var calcA = parseFloat(palavra[0]);
    var calcB = parseFloat(palavra[1]);
    var erroP = parseFloat(palavra[2]);
    var consDia = parseFloat(palavra[3]);
    var result;

    var codPA = 0;
    var latPA = 0;
    var lonPA = 0;
    var qtdPesPA = 0;
    var qtdDiaPA = 0;
    var adminPA = 0;
    var nomePA = 0;
    var capacPA = 0;
    var cidadePA = 0;
    var estadoPA = 0;
    var statusPA = 0;
    var cadPA;

    var test = data.paa;

    for (var i = 0; i < test.length; i++) {
        var ta = test[i];
        var linhaY = at.capacPA / (at.qtdPesPA * consDia);
        ta.codPA = ta.codPA;
        ta.latPA = ta.latPA;
        ta.lonPA = ta.lonPA;
        ta.qtdPesPA = ta.qtdPesPA;
        ta.qtdDiaPA = Math.round(((linhaY - ((calcA * ta.capacPA) + calcB) + erroP) + erroP + linhaY) * erroP / 10);
        ta.adminPA = ta.adminPA;
        ta.nomePA = ta.nomePA;
        ta.capacPA = ta.capacPA;
        ta.cidadePA = ta.cidadePA;
        ta.estadoPA = ta.estadoPA;
        ta.statusPA = ta.statusPA;
        ta.cadPA = new Date
        result = ta.qtdDiaPA;
    }
    return result;
};


/*
ta.save(function(error, pa) {

    if (error) {
        result = ({
            error: 'Valor de campo inválido'
        });
    } else {
        result = (pa);
    }

});
*/

It's hard, I do not have much experience in JS and Node.

    
asked by anonymous 04.08.2016 / 22:13

1 answer

3

Here is a help in the function logic to modify the data you are receiving and return a json in it, modified:

module.exports = function (data, palavra) {

    var calcA = parseFloat(palavra[0]);
    var calcB = parseFloat(palavra[1]);
    var erroP = parseFloat(palavra[2]);
    var consDia = parseFloat(palavra[3]);

    data.paa = data.paa.map(function(teste){
        var linhaY = at.capacPA / (at.qtdPesPA * consDia);
        teste.qtdDiaPA = Math.round(((linhaY - ((calcA * teste.capacPA) + calcB) + erroP) + erroP + linhaY) * erroP / 10);
        teste.cadPA = new Date();
        return teste.qtdDiaPA;
    });
    return data;
};

You are basically modifying only data.paa so I used data.paa = data.paa.map(function(teste){ to change this array to the new results.

Here in sweden it is bedtime. Tomorrow I can take a look here if you could solve the problem. But how did you say you had difficulty in function logic , here it is.

    
05.08.2016 / 00:37