Problem in Migration

Hello, there…!

This problem now have been trun into ordeal for me as I have struggling for about five to six hours but can’t able to solve it.

The problem is in Ticket Migration. In the file it self they have instructed us that we have to do four steps to generate the token. I have gone through all four steps like in the

Step - 1 : Connect to my Atlas with the respected URI
Step - 2 : Create the predicate as per the need
Step - 3 : Create the projection (though it was the optional)
Step - 4 : Produce a bulkWrite statement for to update the data

The program also run pretty well but it could not able to change the data type of date field in collection so my tests in migration.test.js file landed up with failer.

Someone can help me out of this problem. I am also giveing you my predicate logic and bulkWrite logic…

Hoping for eagarly replay, Thank in advance…!!!

> predicate = {
  '$and': [
      'lastupdated': {
        '$exists': true
    }, {
      'lastupdated': {
        '$type': 'string'

> const { modifiedCount } = await mflix.collection("movies").bulkWrite( moviesToMigrate)


Ok, the bulkWrite() must to include an array. Try to fix that and it’ll run.

We know that moviesToMigrate have an array, but there is a way to make it work.

Hello @canelacho, thank you for showing your insterest…

But I guess, they have made a ready made skeleton for this so we don’t required to create our own array. I may be wrong, but have a look at this peice of code…

 const projection = {'_id' : 1, 'lastupdated' : 1}
    const cursor = await mflix
      .find(predicate, projection)

    const moviesToMigrate ={ _id, lastupdated }) => ({
      updateOne: {
        filter: { _id: new 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 mflix.collection("movies").bulkWrite( moviesToMigrate) 

Isn’t it moviesToMigrate is array?

What is the error when you run the migrations.js file??
I mean what is the output in your terminal when you run node movie-last-updated-migration.js ??

1 Like

Are you receiving an error when running your script?

As an aside, your projection of '_id': 1 isn’t necessary. The _id field is always included unless explicitly removed. In this case, you definitely want to keep it though.

Edited to add:

I just noticed your predicate as well. The use of $and isn’t necessary. The following will suffice:

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

Yes I know that you don’t have to make any new var and load it an array etc. I solved using a syntax to load the array in the bulk with a simple spread. I know that someone will see this like it’s not the way, but in programming that is valid or not?.

For example: Yo can use a “$and” or not and it will work, you can cut something in the “$set” statement and it’ll work too, so as I said, everything is valid as long as it works.

Writing and testing is important to learn how to improve your code and learn to use best practices.

I made my ticket work, updated my group and passed the test.
If I tell you, it’s just to help but it’s up to you.


Hii, @ayam1701

Actually, I am not getting error of any kind while running migration.js file. It say that “Found 0 documents to update” so I don’t think there is any kind of error, but when I try to run a test file for same it shows that the data type of date is string and expected value is date, and this is a real problem.

Thank you for your responce @nathan.leniz

I have change the predicate as per your syntex, yet not able to produce a desire result.

Okay @canelacho,

I will also try that out, and inform you if I can get success…!!

1 Like

Where did new come from in this line?

This means there is a problem in your query and it doesn’t filter the right files. I think using console.log() will help you to debug your code.

1 Like

Correct, different ways will work. But we can always point out more concise ways to each other, that’s the point of honing our craft!

Can you share your host information?

1 Like

const host = ‘mongodb+srv://’

1 Like

Hello, @ayam1701

The new ObjectId(_id) is accidentally put my be, I have also try the default version of the same but recieve the same error, and thank you for your kind advice :grinning: of console.log(), but I have also try that out and can’t able to success :disappointed_relieved:!!


I’ve connected to your cluster and can verify there are no documents to update, because all documents in your comments collection have been migrated.

I’m guessing it ran successfully and you may have missed it. Please try running the migration test and let me know the result.

1 Like

Hello @nathan.leniz,

Sorry to say but I am still not able to pass the test.!

Thank you for your support guys. !!

I have found the solution for this. Actually, I have entered a wrong host uri and that was a core mistake. Now I am successfully able to generate validation code.!!!

Thank you once again @nathan.leniz, @ayam1701, @canelacho for all the valuable advices…!:grin:


I’m using this
const predicate = { lastupdated: { $exists: true , $type: “string” } }
and I’m still getting a failure:
FAIL test/migration.test.js
✕ migration (118ms)

● Migration › migration


Received: null

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

If I change the migration.test.js to have $type: “string” everything checks out ok…
So I’m assuming that somewhere my date isn’t showing up as a string as it’s in date format.
I’ve spent a ton of time trying different things and have hit a wall.

Do not change tests, EVER.

Point is that it is meant to do it that way. Read the test:

  test("migration", async () => {
    const movie = await MoviesDAO.movies.findOne({
      _id: ObjectId("573a1391f29313caabcd8526"),
      lastupdated: { $type: "date" },

What that means is that the test is expecting the returned document matching that _id value to actually have it’s lastupdated field *changed from the “string” that it was into a Date type.

That is what your migration script was meant to to. By finding all the documents which have a “string” in the lastupdated and casting that into a Date type. Therefore { $type: "date" } is actually meant to match every document in the collection.

If this test fails then your migration is incorrect and you need to fix it.

1 Like

Hii, @Bernard_74913

Have you run the movies-last-updated-migration.js file which in in migration directory of src. Try to run that one first having all the necessary change, then after run migration.test.js…