Problem with "Ticket: Migration"

Hi,
i ran into the problem, that my script finds 0 results. If i connect with the shell, everything works fine.

Here my script:
// 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@cluster0-cniqc.mongodb.net/test”
const client = await MongoClient.connect(
host,
{ useNewUrlParser: true, useUnifiedTopology: 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()

const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({
  updateOne: {
    filter: { _id: ObjectId(_id) },
    update: {
      $set: { lastupdated: new Date(Date.parse(lastupdated)) },
    },
  },
}))
console.log(
  "\x1b[32m",
  `Found ${moviesToMigrate.length} documents to update`,
)

And here my .env file:

Ticket: Connection

Rename this file to .env after filling in your MFLIX_DB_URI and your SECRET_KEY

Do not surround the URI with quotes

SECRET_KEY=ThisIsMySpecialSecretKey
MFLIX_DB_URI=mongodb+srv://m220student:m220password@cluster0-cniqc.mongodb.net/test
MFLIX_NS=sample_mflix
PORT=5000

Hi @Alexander_99828,

Please try after adding require("dotenv").config() line in the top of the file:

Kanika

Hi,

i added the given line, but have the same issue. The script outputs: Found 0 documents to update

Enclosed you will find the script. Maybe there is something wrong with my predicate-Variable.

sincerly

Alexander Christof

(Attachment movie-last-updated-migration.js is missing)

Hi,

i added the given line, but have the same issue. The script outputs: Found 0 documents to update

Enclosed you will find the script. Maybe there is something wrong with my predicate-Variable.

sincerly

Alexander Christof

const MongoClient = require(“mongodb”).MongoClient

const ObjectId = require(“mongodb”).ObjectId

const MongoError = require(“mongodb”).MongoError

require(“dotenv”).config()

/**

  • Ticket: Migration

Hi!
I had the same problem and I found a solution here Ticket: Migration test doesn't work

Hi,

Thanks! That worked. Just manually inserted “sample_mflix”.

1 Like