seppevs / migrate-mongo

A database migration tool for MongoDB in Node
MIT License
902 stars 160 forks source link

Unable to debug and generate any result #413

Closed abhimanusharma closed 9 months ago

abhimanusharma commented 2 years ago

Here is my complete migration file.

const moment = require('moment-timezone');

module.exports = {
  async up(db, client) {
    // TODO write your migration here.
    // See https://github.com/seppevs/migrate-mongo/#creating-a-new-migration-script

    try {
      const objective = await db.collection('ChildObjectives').findOne({
        "ObjectiveConfig.Title": {$regex: /test parent/},
        "SelectedItems.ItemList.StartDate": {$type: 'date'},
        "CreatedDate": new Date('2021-07-20T07:39:46.371+00:00')
      });
      console.log('objective', objective.SelectedItems[0].ItemList.length);
      for (let i = 0; i < objective.SelectedItems[0].ItemList.length; i++) {
        const item = objective.SelectedItems[0].ItemList[i];
        await db.collection('ChildObjectives').updateOne(
          {
            "ObjectiveConfig.Title": {$regex: /test parent/},
            "SelectedItems.ItemList.StartDate": {$type: 'date'},
            "CreatedDate": new Date('2021-07-20T07:39:46.371+00:00')
          },
          [
            {
              $set: {"SelectedItems.$[0].ItemList.$[i].StartDate": moment(item.StartDate).format('YYYY-MM-DD')},
            },
            {
              arrayFilters: [{
                "i": i
              }]
            }
          ]
        );
      }
    } catch (err) {
      throw err;
    }
  },

  async down(db, client) {
    // TODO write the statements to rollback your migration (if possible)
    // Example:
    await db.collection('ChildObjectives').updateOne(
      {
        "ObjectiveConfig.Title": {$regex: /test parent/},
        "SelectedItems.ItemList.StartDate": {$type: 'string'},
        "CreatedDate": new Date('2021-07-20T07:39:46.371+00:00')
      },
      {
        $unset: {"SelectedItems.$.ItemList.$.StartDate": new Date("2021-07-20T07:39:50.975+00:00")}
      }
    );
  }
};
seppevs commented 9 months ago

Sorry, this description is too vague. I'm closing this.