Ticker: Migration

The following code seems like it should work but I must be missing something simple.

// 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!
// db.movies.find({lastupdated: { $exists: true, $type : "string"}}).count()
const predicate = {lastupdated: { $exists: true, $type: "string" } }
const projection = {lastupdated: true}

This direct query returns the correct values:

db.movies.find({lastupdated: { $exists: true, $type : “string”}}, {lastupdated:true})

Hi @Andrew_74578,

This looks absolutely correct to me. Is there any error while running Migration? If yes, then please share.

Kanika

When I run the migration js file on its own:

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 =“Left Out on purpose” // Connection is working
const client = await MongoClient.connect(
host,
{ useNewUrlParser: true },
)
const mflix = client.db(“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!
// db.movies.find({lastupdated: { $exists: true, $type : "string"}}, {lastupdated:true})

//code commented out for reason

console.log(
  "\x1b[32m",
  `Found ${moviesToMigrate.length} documents to update`,
)
// TODO: Complete the BulkWrite statement below
const { modifiedCount } = await "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)
}
})()

Images and code posted

Try running after removing new keyword.

$set: { lastupdated: Date.parse(lastupdated) },

Also, check your database to see if the migration run successfully by checking the type of lastupdated field.

Kanika

I missed a whole To Do on line 53. I didn’t see it. I work on it and let you know if I have any other questions.

// TODO: Complete the BulkWrite statement below
const { modifiedCount } = await “some bulk operation”

I am stuck on the same issue and working on line 53 and haven’t been able to get it to work yet.

moviesToMigrate is an object constructed. You just need to issue the bulkWrite function

Curlybraces-Mac-mini:migrations curlybraces$ node movie-last-updated-migration.js
Found 45993 documents to update
45993 documents updated
Curlybraces-Mac-mini:migrations curlybraces$ npm test -t migration

server@1.0.0 test /Users/curlybraces/Downloads/mflix-js
jest --passWithNoTests “migration”

Determining test suites to run…Setup Mongo Connection
PASS test/migration.test.js
Migration
✓ migration (49ms)

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


I entered this number the very first time and its not the correct answer but my migration test passes.

Whoops… totally forgot I had to get the output from the frontend app status page. After that, it succeeds! thanks!

Hi kanikasingla
I follow your advice and below code
typeof db.movies.findOne({_id: ObjectId(“573a1391f29313caabcd82da”)}).lastupdated
returns “number”
Result of migration looks good
C:\nosql\mongodb\M220JS\mflix-js\src\migrations>node movie-last-updated-migration.js
Found 23539 documents to update
23539 documents updated
Not sure how to proceed. Any advice

I passed the ticket by now:

  1. const predicate = { “lastupdated”: {$exists: true},“lastupdated”: {$type:“number”} }
    and use
  2. $set: { lastupdated: new Date(Date.parse(lastupdated)) }
1 Like

At first, I changed database name “test” on “sample_mflix”

// ensure you update your host information below!
const host .mongodb.net /sample_mflix/?retryWrites=true&w=majority

After, I writed .bulkWrite(moviesToMigrate) on mflix object with proper collection name

If you want that script run without errors, change host string to
const host = “mongodb+srv://m220student:m220password@abc.mongodb.net?retryWrites=true&w=majority”

where abc.mongodb.net is your own host adress

1 Like