Migration: Found 0 documents to update message

I believe that I’m creating the right predicate and still encountering the error which says:

“Found 0 documents to update”

Pardon me if I’m mentioning the complete solution as I see no other way to seek the right answer. You may hide it.
Here’s the code that I’m adding:

const predicate = { lastUpdated: { $exists: true }, lastUpdated: {$type: 'string'} }

What is it that I’m doing wrong?

Thank you.

1 Like

did you find the answer? I am getting the same thing…

actually I’ve been struggle with found 0 documents for a long time.
At the end, I found change process.env.MFLIX_NS with sample_mflix then everything works.

Why did it work with sample_mflix? Also did you replace process.env.MFLIX_NS with sample_mflix or sample_mflix.env.MFLIX_NS?
Thanks.

Replace the whole variable with sample_mflix like below:

  const mflix = client.db("sample_mflix")

Kanika

1 Like

Hi Kanika, thanks, the answer (given once implementation succeeds) still shows db name as “mflix”. Was that the name for the last class? Should I have used the command line instead of loading my dataset on to my cluster using Atlas? Thanks.

Hi @Jai_33176,

Yes, in previous offerings we were using datasets with database name “mflix” but now we have changed this to sample data and its almost half the size of previous dataset.

Now, we recommend users to import sample data from atlas cluster itself.

Kanika

Hi Kanika,
How do I send private messages that only the recipient can view? Thanks. - jai

Hi @Jai_33176,

Only Staff can initiate a direct message at the moment. I have started a conversation with you, let me know if you have any issues.

Kanika

Hey @kanikasingla!

Been having the above mentioned issue. However, I made the following change to my code:

const host = "mongodb+srv://m220student:m220password@mflix-mvxh1.mongodb.net/sample_mflix"

on my host variable from the previous const host = "mongodb+srv://m220student:m220password@mflix-mvxh1.mongodb .net/test"

The result before the change was:

anthony@KYRA:~/Documents/mflix-js/src/migrations$ node movie-last-updated-migration.js
(node:17366) 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.
 Found 0 documents to update
 No documents to update

This was the result after the change.

anthony@KYRA:~/Documents/mflix-js/src/migrations$ node movie-last-updated-migration.js
    (node:17853) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitorin
    g engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
     Found 23539 documents to update
     23539 documents updated

However, the tests fail. Both with the original code before any change as well as after the changes.
Here is my code:

const MongoClient = require("mongodb").MongoClient
const ObjectId = require("mongodb").ObjectId
const MongoError = require("mongodb").MongoError

  /**
   * Ticket: Migration
   *
   * Update all the documents in the `movies` collection, such that the
   * "lastupdated" field is stored as an ISODate() rather than a string.
   *
   * The Date.parse() method build into Javascript will prove very useful here!
   * Refer to http://mongodb.github.io/node-mongodb-native/3.1/tutorials/crud/#bulkwrite
   */

  // This leading semicolon (;) 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-mvxh1.mongodb.net/test"
      const client = await MongoClient.connect(
        host,
        { useNewUrlParser: true },
      )
      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: true }
      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)

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

This is the error thrown both with and without the changes mentioned above:

anthony@KYRA:~/Documents/mflix-js/src/migrations$ npm test -t migration

> server@1.0.0 test /home/anthony/Documents/mflix-js
> jest --passWithNoTests "migration"

Determining test suites to run...Setup Mongo Connection
(node:19355) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use t
he new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
 FAIL  test/migration.test.js
  Migration
    ✕ migration (402ms)

  ● 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/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
      at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        3.55s, estimated 4s
Ran all test suites matching /migration/i.
Teardown Mongo Connection
npm ERR! Test failed.  See above for more details.

Please help. Chapter labs deadlines are in a few hours from now.

Hi @Anthony_73816,

I apologise we were not available during these days. According to the error message I see here, it shows that database has not reflected those changes.

Did you check the database/collection documents if the type of the field has been updated?

Kanika