Monday, 10 April 2017

remove gives unexpected result from mongodb using async methods

I want to update my table and remove some lines. This is my code.

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


async.series([

    //Mercedes -> Mercede-Benz

    function (cb) {

      async.forEachOfSeries(toFix, function (to, from, cb1) {
        console.log(`Fix car.make ${from} -> ${to}`);
        CarModel.update({make: from},
          {$set: {'make': to}}, {multi: true}, function (err, res) {
            if (err) {
              console.log(err);
            }
            cb1()
          });
      }, cb);

    },

    //   remove duplicated carModels

    function (cb) {

      async.series([

        function (cb) {

          CarModel.findByIdAndRemove(
            {_id: '567'}, function (err) {
              if (err) throw err;
              console.log('car model1 deleted');
            },
            cb);
        },


        function (cb) {
          CarModel.findByIdAndRemove(
            {_id: '568'},
            function (err) {
              if (err) throw err;
              console.log('car model2 deleted!');
            },
            cb);
        }
      ], cb);

    }
  ], done);

the problem is that I got just model1 removed. not all the carModels.

and I got in logs : "model1 deleted".

I don't know why,

Can anyone help please?



via Amine Haddar

No comments:

Post a Comment