Ticket: Migration

I am getting an error 0 documents found. I get it early on even when I comment out the predicate, and when I try to print the cursor which should be an array of documents I do not get information.

Here is my code:

const MongoClient = require(“mongodb”).MongoClient

const ObjectId = require(“mongodb”).ObjectId

const MongoError = require(“mongodb”).MongoError

//const assert = require(‘assert’);

/**

*/

// 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-0mpz9.mongodb.net/test"

const client = await MongoClient.connect(

  host,

  { useNewUrlParser: true },

)

const mflix = client.db(process.env.MFLIX_NS)

// 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 }, 

  //lastupdated: { $type: "string" }

}

const projection = {lastupdated: 1}

const cursor = await mflix

  .collection("movies")

  .find(predicate, projection)

  .toArray()

console.log(`now printing the content of cursor ${cursor}`)

const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({

  updateOne: {

    filter: { _id: ObjectId(_id) },

    update: {

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

    },

  },

}))

console.log(

  "\x1b[32m",

  `Found ${moviesToMigrate.length} documents to update in the array formation`

)

// TODO: Complete the BulkWrite statement below

const { modifiedCount } = await mflix

/*

.collection(“movies”)

.BulkWrite(moviesToMigrate)

*/

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)

}

})()

I also commented out the bulkwrite routine to simplify and help the debugging since the no documents found seems to be much earlier
Please help

Try after adding this line :

   require("dotenv").config()

Kanika

added the line but still get found 0 documents on the test (using the same code above with the commented parts just adding the “required” you suggested. I also retested the user-report just to make sure my connections are all active and it passes fine.
happy new year

also please note the following line (#46 in the program):
console.log(now printing the content of cursor ${cursor})

I am trying to see if the cursor array contains data and it just print “now printing the content of cursor” to the screen not showing anything for ${cursor}.

Cursor remains to be empty even after including the environment library

It works if you replace:
const mflix = client.db(process.env.MFLIX_NS) // on Line: 26
with
const mflix = client.db("sample_mflix")

Did as you recommended still not documents found.

Hi @Amos_Madanes_89196,

I can see that you have converted the field type to epoch seconds I guess:

    "lastupdated" : 1440572630133

You might need to reconvert this back or re-load the sample data to fix this.

Kanika

how do i re-load the sample data?

Delete the sample databases from your cluster and then “Load the Sample Dataset” again.
or give me some time, let me see if I can fix this for you.

Kanika

One thing you can do, is you can replace the filter for lastupdated from type string to integer or try removing that, just keep $exists and that would work.

Kanika

OK, I removed the string and got these results
Found 23539 documents to update in the array formation
23539 documents updated

when I ran it again it found the same number of documents but updated 0, I guess since it was updated the first run around. when I tried to run it on the mflix app I get the following error:
Migration: It does not appear that you correctly converted the type

Thats good.

Did you check in the database what type is showing for lastupdated field?

Kanika