Ticket: Migration

; (async () => {

try {

  // ensure you update your host information below!

  const host =

    "mongodb+srv://user:************@eco-chain-qlvm1.mongodb.net/test?retryWrites=true&w=majority"

  const client = await MongoClient.connect(host, { useNewUrlParser: true })

  const mflix = client.db(process.env.MFLIX_NS)

 

  const predicate = { $and: [{ lastupdated: { $exists: true } }, { lastupdated: { $type: "string" } }] }

  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 { modifiedCount } = await mflix.collection("movies").bulkWrite([moviesToMigrate]

    , { ordered: true, w: 1 })//"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)

}

})()

I have spent many hours on this figuring what could be wrong with it. Ever time I run “node movie-last-updated-migration.js” I get the following error:
Found 0 documents to update
** Error during migration, TypeError: Cannot read property ‘collation’ of undefined**
today is the last day to submit this ticket. Anybody can guide me what could possibly be wrong with it.
Any help would be much appreciated.
Thank you.

Although I took the Python variation of this course, I’ve made a couple of observations:

  1. Based on the code snippet above, you’ve made more changes than was instructed. For example you most likely removed const and equal to (=) operator from the projection line, and youv’e set a write concern and ordered bulk write which was not part of the requirement.
  2. Your projection needs to include all the fields that will be used further down the line. Review what fields are being used.
  3. In the $set part, the objective is not to create a new Date object. All that’s required is a typecast to Date which will then be parsed.

Suggest you revert back to the original script and only make changes to the parts as instructed in the TODO comments.

1 Like

Hi @mubeenullhaq,

Can you try just writing “sample_mflix” in place of “process.env.MFLIX_NS”. Another way is to include below line in the top of file:

     require("dotenv").config()

I am not sure if this is required here.

Kanika

1 Like