M220JS - migration issue

I have a small issue with the check migration in the m220JS course.
After I have reimported the original sample dataset I checked the record which I believed is queried to check the migration test

I CHECKED THE ORIGINAL RECORD: lastupdated IS String TYPE (as it has to be)

MongoDB Enterprise atlas-xrgeun-shard-0:PRIMARY> db.movies.findOne({_id: ObjectId(‘573a13a5f29313caabd1333a’)})
{
“_id” : ObjectId(“573a13a5f29313caabd1333a”),
“fullplot” : “A clown fish named Marlin lives in the Great Barrier Reef loses his son, Nemo. After he ventures into the open sea, Nemo and the other sea animals in the dentist’s fish tank plot a way to return to Sydney Harbor to live their lives free again.”,
“imdb” : {
“rating” : 8.2,
“votes” : 600064,
“id” : 266543
},
“year” : 2003,
“plot” : “After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets out on a journey to bring him home.”,
“genres” : [
“Animation”,
“Adventure”,
“Comedy”
],
“rated” : “G”,
“metacritic” : 90,
“title” : “Finding Nemo”,
"lastupdated" : “2015-09-02 00:24:42.757000000”,
“languages” : [
“English”
],
.
.
TRYING TO RUN THE TEST npm test -t migration WE NATURALLY OBTAIN A FAILURE

FAIL test/migration.test.js
Migration
✕ migration (35ms)

● 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.<computed> [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: 1.405s
Ran all test suites matching /migration/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.

RESULT IS FAIL (correct at this stage as conversion was not made)

NOW RUN THE MIGRATION PROGRAM
NOTE THAT I HAVE TO SPECIFY THE TS=‘UTC’ VARIABLE AS OTHERWISE THE DATES/TIMES ARE MESSED UP BECUASE I AM IN A DIFFERENT TIMEZONE FROM THE ORIGINAL ONE
SO LET’S RUN THE TEST PRETENDING TO BE GMT TIME

TZ=‘UTC’ node src/migrations/movie-last-updated-migration.js

[
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },

]… 23430 more items
Found 23530 documents to update
23530 documents updated

DOCUMENTS SHOULD NOW BE UPDATED.

LET’S CHECK IT QUERYING THE SAME OFFENDING DOCUMENT

MongoDB Enterprise atlas-xrgeun-shard-0:PRIMARY> db.movies.findOne({_id: ObjectId(‘573a13a5f29313caabd1333a’)})
{
“_id” : ObjectId(“573a13a5f29313caabd1333a”),
“fullplot” : “A clown fish named Marlin lives in the Great Barrier Reef loses his son, Nemo. After he ventures into the open sea, Nemo and the other sea animals in the dentist’s fish tank plot a way to return to Sydney Harbor to live their lives free again.”,
“imdb” : {
“rating” : 8.2,
“votes” : 600064,
“id” : 266543
},
“year” : 2003,
“plot” : “After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets out on a journey to bring him home.”,
“genres” : [
“Animation”,
“Adventure”,
“Comedy”
],
“rated” : “G”,
“metacritic” : 90,
“title” : “Finding Nemo”,
"lastupdated" : ISODate(“2015-09-02T00:24:42.757Z”),
“languages” : [
“English”
],
“writers” : [
“Andrew Stanton (original story by)”,
“Andrew Stanton (screenplay)”,
“Bob Peterson (screenplay)”,
“David Reynolds (screenplay)”
],

RECORD WAS UPDATED CORRECTLY.
INFACT lastupdated IS NOW ISODate
LOOKING AT THE lastupdated FIELD:
BEFORE IT WAS: “2015-09-02 00:24:42.757000000” (String)
NOW IT IS: “lastupdated” : ISODate(“2015-09-02T00:24:42.757Z”) (ISODate)
CONVERSION LOOKS GOOD AND ALSO TIMES ARE CONGRUENT

RUN AGAIN THE MIGRATION CHECK TEST

MacBook-Pro-di-andrea:mflix-js sicktoy$ npm test -t migration

server@1.0.0 test /Users/sicktoy/sviluppo/mongodb/m220JS/mflix-js
jest --passWithNoTests “migration”

Determining test suites to run…Setup Mongo Connection
(node:34532) Warning: Accessing non-existent property ‘count’ of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:34532) Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency
(node:34532) Warning: Accessing non-existent property ‘remove’ of module exports inside circular dependency
(node:34532) Warning: Accessing non-existent property ‘updateOne’ of module exports inside circular dependency
(node:34532) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
PASS test/migration.test.js
Migration
✓ migration (38ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.016s, estimated 2s
Ran all test suites matching /migration/i.
Teardown Mongo Connection

TEST SUCCEDED. HOORAY…

NOW WE RUN npm start , go to the status page and cLick “Migration”.
AS THE PREVIOUS TEST WAS SUCCESSFUL I EXPECTED TO GET A POSITIVE ANSWER
BUT THE RESULT IS: Migration: It does not appear that you correctly converted the type

Is there anything I am really missing here???

So I have installed a mongodb on my localhost and set set profiler enabled.
I returned to the status page and when I click cwonto “Migration” the test fails
but I noticed that the query that was run on the server is the following:

db.system.profile.find({user:“m220student@admin”,appName: {$ne :“Mongo Shell”}, ts: {$gt: ISODate(“2021-01-20T23:39:13.720Z”)}}).sort({ts:-1}).limit(1).pretty()





“command” : {
“aggregate” : “movies”,
“pipeline” : [
{
“$match” : {
“_id” : ObjectId(“573a13a5f29313caabd1333a”)
}
},
{
lookup" : { "from" : "comments", "let" : { "id" : "_id”
},
“pipeline” : [
{
“$match” : {
“$expr” : {
“$eq” : [
“$movie_id”,
“$$id”
]
}
}
},
{
“$sort” : {
“date” : -1
}
}
],
“as” : “comments”
}
},
{
“$project” : {
“title” : 1,
“comments” : 1
}
}
],
“cursor” : {

            },
            "lsid" : {
                    "id" : UUID("a3402e38-7ffa-485f-8ffc-baf9b2305644")
            },
            "$db" : "sample_mflix"
    },

???
This Query doesn’t really check for a specified datatype for the lastupdated field in the movie collection !!!
something is really messed up…
note that all the previous tests from the status page are working just fine…

Please help…

Ok,
I attached the debugger to the process and I think I have found the error

in Chapter 2. - Ticket Get Comments excercise we are required to change the method getMovieByID
so that is retrievies the ordered list of comments for a give movie (using the $lookup function)

Here the code:
const pipeline = [
{
‘$match’: {
‘_id’: ObjectId(id)
}
}, {
lookup': { 'from': 'comments', 'let': { 'id': '_id’
},
‘pipeline’: [
{
‘$match’: {
‘$expr’: {
‘$eq’: [
‘$movie_id’, ‘$$id’
]
}
},
},{"$sort":{“date”:-1}}
],
‘as’: ‘comments’
}
},
{
‘$project’: {
‘title’: 1,
‘comments’: 1
}
}
]

The new pipeline returns somthing which cannot be successfully evaluated by the code in the Ticket Migration excercise.

rolling back the getMovieByID to the original form (simple _id search for a movie) solves the issue.

here the rolled back code:

const pipeline = [
{
‘$match’: {
‘_id’: ObjectId(id)
}
}
]

But it’s tricky to understand you have to roll back previous code from the Nth exercise in order to make a further exercise to work properly.
Should it really be this way?

Thanks

Regards

Andrea.

Hi Andrea,
not sure it worth something but this:

‘lookup': { 'from': 'comments', 'let': { 'id': '_id’

should be written with dollar sign → ‘$lookup’ your code with the dollar sign:

  const pipeline = [
      {'$match': {_id: ObjectId(id)}}, 
      {'$lookup': 
          {
              'from': 'comments', 
              'let': { 'id': '$_id'},
              'pipeline': [
                  {'$match': {'$expr': {'$eq': ['$movie_id', '$$id']}},},
                  {"$sort":{ 'date':-1}}
              ],
              'as': 'comments'
          }
      },
      {'$project': {'title': 1,'comments': 1}}
 ]

Thanks for your answer.

in the code $lookup was written correctly (I edited the code before pasting because it wa sbdaly formatted)
I also tried again now and even with the “$lookup” …it does not work.

it only works when the pipeline is
const pipeline = [
{
‘$match’: {
‘_id’: ObjectId(id)
}
}
]

However I probably learnt more trying to debug this issue than doing the excercise itself :slight_smile:

When publishing code or documents, please enclose between 2 lines of triple back ticks such as ```. This way formatting and appropriate quotes will be preserved.