Ticket: Migration

I got the following error when I tried to execute the migration

I wonder this is about the problem on connection (URI malform!?), which was not part of the ticket TODO.
And it is more interesting that when I run the original version (freshly downloaded, without my intervention) of movie-last-updated-migration.js , exactly the same error received.

Anybody know why this happened?

Around line 19 (in my version) there is a comment that reads

19 // ensure you update your host information below!
20 // const host = “mongodb://<your atlas connection uri from your .env file”

I do not

What is the value of process.env.MFLIX_DB_URI?

The content of movie-last-updated-migration.js I downloaded:

const ObjectId = require("mongodb").ObjectId
const MongoError = require("mongodb").MongoError

 * Ticket: Migration
 * Update all the documents in the `movies` collection, such that the
 * "lastupdated" field is stored as an ISODate() rather than a string.
 * The Date.parse() method build into Javascript will prove very useful here!
 * Refer to http://mongodb.github.io/node-mongodb-native/3.1/tutorials/crud/#bulkwrite

// This leading semicolon (;) is to signify to the parser that this is a new expression. This expression is an
// Immediately Invoked Function Expression (IIFE). It's being used to wrap this logic in an asynchronous function
// so we can use await within.
// To read more about this type of expression, refer to https://developer.mozilla.org/en-US/docs/Glossary/IIFE
;(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)

    // 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 = { somefield: { $someOperator: true } }
    const projection = {}
    const cursor = await mflix
      .find(predicate, projection)
    const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({
      updateOne: {
        filter: { _id: ObjectId(_id) },
        update: {
          $set: { lastupdated: new Date(Date.parse(lastupdated)) },
      `Found ${moviesToMigrate.length} documents to update`,
    // TODO: Complete the BulkWrite statement below
    const { modifiedCount } = await "some bulk operation"

    console.log("\x1b[32m", `${modifiedCount} documents updated`)
  } 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}`)

The MalformedUri comes from MongoClient.connect. So for some reason host and more specifically process.env.MFLIX_DB_URI is wrong. May be the configuration file is not accessible from the directory where you run the script.

I can eventually execute the migration successfully by copying the .env file to the migration folder, which I never expected as compared to the previous tickets. What a surprise!


Yes. We needed to run the migration script from mflix-js directory like as @steevej-1495 mentioned, it won’t be able to read cluster uri.


Running the migration script from mflix-js directory did not work for me. It returned with a “Cannot find module” error.

Running it with the following command worked for me.
node src/migrations/movie-last-updated-migration.js