Ticket: Migration

Hi,

I am unable to complete the migration ticket and I couldn’t figure out what is an issue with my code.
I have updated uri and db name.

const MongoClient = require(“mongodb”).MongoClient
const ObjectId = require(“mongodb”).ObjectId
const MongoError = require(“mongodb”).MongoError

/**

// This leading semicolon (:wink: is to make this Immediately Invoked Function Expression (IIFE).
// To read more about this type of expression, refer to https://developer.mozilla.org/en-US/docs/Glossary/IIFE
;(async () => {
try {
// ensure you update your host information below!
const host = “mongodb+srv://m220student:m220password@mflix-m9bux.mongodb.net”
const client = await MongoClient.connect(
host,
{ useNewUrlParser: true },
)
const mflix = client.db(‘sample_mflix’)

// 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, _id: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);

console.log(`${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(“No documents to update”)
} else {
console.error(Error during migration, ${e})
}
process.exit(1)
}
})()

Please advise.
Thanks

Hi @Mozhgan_72887,

What is the output when you run the script?

Kanika

This is the test result :
Determining test suites to run…Setup Mongo Connection
FAIL test/migration.test.js
Migration
× migration (280ms)

● Migration › migration

expect(received).not.toBeNull()

Received: null

  12 |       lastupdated: { $type: "date" },
  13 |     })
> 14 |     expect(movie).not.toBeNull()
     |                       ^
  15 |   })
  16 | })
  17 |

  at toBeNull (test/migration.test.js:14:23)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.632s, estimated 3s
Ran all test suites matching /migration/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.

And this is result after running script: Found 0 documents to update
No documents to update

Because your predicate is looking for documents having string type, but when I looked in the dataset you are using, it is showing integer values (maybe epoch). You may need to run the script without specifying condition for $type.

Kanika

1 Like

It worked.
Many thanks for your help.