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.