Ticket: Migration - Help needed please

Please could someone tell me why I’m not able to pass the test with this code?

  (async () => {
  try {
    const host = process.env.MFLIX_DB_URI
    const client = await MongoClient.connect(host, { useNewUrlParser: true })
    const mflix = client.db(process.env.MFLIX_NS)
    const collMovies = mflix.collection("movies")

    // 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 collMovies.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 { modifiedCount } = await collMovies.bulkWrite(moviesToMigrate)

    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)
  }
})()

You shared your code but you did not say what is happening?

You get an error?
Invalid answer?
A crash?

You have 2 console.log, which one do you get? Which one to you get when you run the conversion twice in a row?

Lastupdated is not changing from String to Date, so on the test it returns null

Hi @Wicleice_Fabio, can you please post the console output when you run the query, and NOT when you run the tests?

Thanks & Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha, thanks for replying! Honestly, I don’t know how to get those logs. I thought they’d show up on the terminal when I run the test.

Post screenshot of the command terminal where you ran the migration.

Your migration code crashes so it is normal that the unit test find unconverted documents.

You have to fix that first.

You run the script from a wrong directory.
Change your command to this:

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

if it’s still not working follow the next instructions:

in package.json
add this line under “scripts”:

"migrate": "node ./src/migrations/movie-last-updated-migration.js",

like here:

  "scripts": {
    "start": "nodemon -L ./index.js",
    "migrate": "node ./src/migrations/movie-last-updated-migration.js",
    "test": "jest --passWithNoTests",

then run this command from the shell:

npm run migrate
2 Likes

Hi @Rafael_Green, thank you so much for your help!
it works just fine.
I have a question though!

Why did I have to run the script “migrate”: “node ./src/migrations/movie-last-updated-migration.js”, before running npm test -t migration? I’m asking that because even though the TODOs on the “migrations/movie-last-updated-migration.js” file was completed the test wasn’t going through

Thats because the test doesn’t run the script for you.
You’re expected to run it yourself before running the test.

@Rafael_Green Why does node command alone not work, why is necessary to add it in the package then run it ??

It is not necessary but it is one way to do it. The other way is to follow the lab instructions that reads like:

To perform the migration, run the script:

node movie-last-updated-migration.js

2 Likes

Oh but when I ran
node movie-last-updated-migration.js
It showed
Found 0 documents to update
No documents to update
So it didn’t work and my test failed

But adding it to package and running worked fine, and the tests passed as well

If you already ran the migration successfully then it is normal that there is no documents to update. They have been updated already.

No this was before running the migration successfully, after I got 0 documents with node command then I tried with the package method, then it worked and I got some “25530 documents updated” output.

But I wanted to know why “node movie-last-updated-migration.js” command didn’t work, and package one did

I would be surprised if it worked one way but not the other because it is the same code, the same data and, in principal, the same configuration.

Since all evidences are now gone, the only way to find out why it did not worked the simple way, would be to reload the sample data set and see what it does.