Migration Ticket: Found 0 documents to update

I keep getting the error “Found 0 documents to update
No documents to update” when I run the “node movie-last-updated-migration.js”

Here is my movie-last-updated-migration.js with connection string censored out:

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 =
“mongodb+srv://username:password@[CENSORED].mongodb.net”
const client = await MongoClient.connect(host, { useNewUrlParser: true })
// const mflix = client.db(process.env.MFLIX_NS)
const mflix = client.db(“sample_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!
const predicate = { lastUpdated: { $exists: true, $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: Date.parse(lastupdated) },
    },
  },
}))
console.log(
  "\x1b[32m",
  `Found ${moviesToMigrate.length} documents to update`,
)
// TODO: Complete the BulkWrite statement below
const { modifiedCount } = await mflix
  .collection("movies")
  .bulkWrite(moviesToMigrate)

// "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)
}
})()

Any idea what I’m doing wrong?

I figured out that I spelled “lastupdated” incorrectly, using “lastedUpdated” with a capital U in my predicateby accident. I also read on another post to remove the “new Date()” part of the update to make it work, so when I first ran this code, I ran the update just with “$set: { lastupdated: Date.parse(lastupdated)}”, which turned all the dates into type number/double. I just dropped all my databases to make space to reload the sample data again and try it with “new Date()” surrounding the “Date.parse(lastupdated)” although now that I think of it I probably could’ve changed the predicate to specify $type being double. Now I’m waiting for the sample data to load to try running this again.

i dropped the databases inside the cluster but mongodb says the cluster is still over 300mb and therefore cant load the sample data. im going to try building a new cluster and using that.

also the original cluster actually was able to load in the sample data, it just takes a minute for the databases to be dropped or for it to be recognized that they were dropped

works now, happy labor day

if anyone comes across this who has NOT misspelt the field name lastupdated and is still getting this error after CORRECTLY inserting their MongoDB connection string into

const host = "mongodb+srv://<your atlas connection uri from your .env file>"

changing

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

to

const mflix = client.db("sample_mflix")

worked for me

2 Likes