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

Asked

Viewed 649 times

3

I have a problem. I have a dataset inside Mongo, a json that has been returned from a url request 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"
  }
]

Being that I am with a regression function, to change the record of all fields qtdDiaPA. I am unable to do a function to update the field in question for each record within Mongo.

I want a Node function that applies in the qtdDiaPA field a linear regression that has been calculated from this same data set. I invoke that calculation through a request, but I want to put it on a timeout every six hours to calculate the regression and then, apply it to the database in the qtdDiaPA record that receives the regression calculation and updates another function that will check if some date parameters of the Delivery set that contains a date field where a difference calculation will be made from the current date to the days calculated and entered in qtdDiaPA.

In short, I want to take a json, access its values, change them. Then put back on an object and have it updated in the database with qtdDiaPA receiving the calculation of my regression for each Codpa in 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 don’t have much experience in JS and Node.

1 answer

3


Here is a help in 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;
};

Basically you’re only modifying data.paa hence I used the data.paa = data.paa.map(function(teste){ share exchange this array for new results.

It’s bedtime here in Switzerland. Tomorrow I can take a look here if you have solved the problem. But as you said you had difficulty in function logic, here.

  • 1

    I can only thank you immensely for your help! Thank you very much Sergio... I have to deliver this work by midnight tomorrow... Tomorrow, you have more questions!

  • I found the error that cost me... var paSchema = Mongoose. Schema({&#xA;&#xA; codPA: Number,&#xA; latPA: Number,&#xA; lonPA: Number,&#xA; qtdPesPA: Number,&#xA; qtdDiaPa: Number,&#xA; adminPA: String,&#xA; nomePA: String,&#xA; capacPA: Number,&#xA; cidadePA: String,&#xA; estadoPA: String, statusPA: Boolean, cadPA: Date }); I don’t believe... Happens! :)

  • @Henningsummer you got so it’s working?

  • Yes @Sergio! Your help was important. Now, I have another question... How can I talk to you, to see if you can help me?

  • @HENNINGSUMMER I help here in stackoverflow, out of this I have no time. So everyone learns. If my answer solved the problem, mark as accepted and launch new question with the new problem.

  • I put a new question http://answall.com/questions/145706/howto invoke-uma-url-no-node-para-dentro-de--pagina-html thank you for your help since.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.