Sunday, 2 April 2017

unexpected result when updating fields in mongodb node.js

I want to update my make and model fields in my cars table. make: from Mercedes to Mercedes-Benz and ancien-models to the correspondant models.

I did this:

const Car = require('model/car');
const async = require('async');
const S = require('string');

module.exports = function (done) {

  const toFix = {
    'Mercedes': 'Mercedes-Benz'
  };

  const toUpdate = {
    'A-CLASSE': 'Classe A',
    'B-CLASSE': 'Classe B',
    'CLA-CLASSE': 'Classe CLA'
  };

  async.series([

    function (cb) {
      console.log('Clean cars...');

      async.forEachOfSeries(toFix, function (to, from, cb1) {
        console.log(`FixMakeForCars ${from} -> ${to}`);
        Car.update({'make': {$regex: S(from).trim().s, $options: 'i'}},
          {$set: {'make': to}}, {multi: true}, cb);
      }, cb);
    },


      function (cb) {
        async.forEachOfSeries(toUpdate, function (to1, from1, cb1) {
          console.log(`UpdateModelForCars ${from1} -> ${to1}`);
          Car.update({'make': 'Mercedes-Benz',
            'model': {$regex: S(from1).trim().s, $options: 'i'}},
            {$set: {'model': to1}}, {multi: true}, function (err, res) {
            if (err) return cb1(err);
            cb1()
          });
        }, cb);

      },

  ], function () {

  });
};

The make is updated perfectly, but when it comes to update to models, I found an expected result. All Mercedes-Benz cars got the same model.

the result of console.log(UpdateModelForCars ${from1} -> ${to1}) is showing that everything is OK.

Can anyone Help please? the result of my script Thank you.



via Amine Haddar

No comments:

Post a Comment