Ticket: migration - urgent need help!

Hello, I wrote this code. It worked fine, but when I ran it twice, Mongodb lost some dates as “Invalid Dates”. So the test is not working. I don’t know how can I restore the entire documents collection from scratch.

const predicate = { lastupdated: { $exists: true } }
const projection = {}
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)).toISOString() },
      $convert: {input: lastupdated, to: 'date'},
    },
  },
}))

The ticket asks you to convert from string to date.

Why to you call toISOString()?

Check the method documentation at Date.prototype.toISOString() - JavaScript | MDN

I do not think $convert is available as a update operator. See the update operators at Update Operators — MongoDB Manual

Thanks! I changed it and it worked fine. Here it is the final code:

const predicate = { lastupdated: { $exists: true} }
const projection = {}
const cursor = await mflix
.collection(“movies”)
.find(predicate, projection)
.toArray()
const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({
updateOne: {
filter: { _id: ObjectId(_id) },
$currentDate: {
lastupdated: { $type: “date” }
},
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)