Ticket Migration (confused)

Hi I have been trying to make this work and cross referenced other posts and am unsure whats wrong.
when I run the test case it fails saying movie is null.

confused why the date field is not changing, in atlas when I search the film id from the test case it shows
lastupdated: “2015-09-02 00:38:57.710000000”

changed the code a few times but this is how it is now.


    // TODO: Create the proper predicate and projection
    // add a predicate that checks that the `lastupdated` field exists, and then
    // check that its type is a string
    // a projection is not required, but may help reduce the amount of data sent
    // over the wire!
    const predicate = { lastupdated: { $exists: true, $type: "string" } }
    const projection = { lastupdated: 1 }
    const cursor = await mflix
      .collection("movies")
      .find(predicate, projection)
      .toArray()
    const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({
      updateOne: {
        filter: { _id: ObjectId(_id) },
        update: {
          $set: { lastupdated: new Date(Date.parse(lastupdated)) },
        },
      },
    }))
    console.log(
      "\x1b[32m",
      `Found ${moviesToMigrate.length} documents to update`,
    )

    // TODO: Complete the BulkWrite statement below
    const movies = mflix.collection('movies')
    const { modifiedCount } = await movies.bulkWrite(
      moviesToMigrate,
      (err, r) =>{
        assert.equal(null, err);
    }) // "some bulk operation"

    console.log("\x1b[32m", `${modifiedCount} documents updated`)
    client.close()
    process.exit(0)
  } catch (e) {
    if (
      e instanceof MongoError &&
      e.message.slice(0, "Invalid Operation".length) === "Invalid Operation"
    ) {
      console.log("\x1b[32m", "No documents to update")
    } else {
      console.error("\x1b[31m", `Error during migration, ${e}`)
    }
    process.exit(1)
  }
})()

Haha oops I forgot to run node movie-last-updated-migration.js
which btw the script is in the wrong place if you downloaded teh handout. Move it from src/migrations into the root of the project…

Hi @Billy_Best, thank you for sharing that.
Instead of moving the migration file from the actual folder to the root folder, we’d recommend you to run the command like the following from the root directory itself:

node src/migrations/movie-last-updated-migration.js

Alternatively, you can simply move into the respective directory and run the migration script like this:

cd src/migrations
node movie-last-updated-migration.js 

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer