Ticket: Migration

Hi all,
please help me in this ticket, why am i getting this error.
error : 0 documents found!
0 document updated.

i have tried like this:

his 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-jncib.mongodb.net/test?retryWrites=true&w=majority”
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, $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`,
)
// 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)

Hi @pradeep_61272,

Thanks for sharing the information. Can you check the following:

  • If not included, include this line on the top:

     require("dotenv").config()
    

Thanks,
Kanika

Hi Kanika,

Thank you for help, however it seems showing same problem.
Another things I point out here is when I am trying to retrieve data from my mongodb cluster using following code then also I am getting error. I don’t no what to do

const MongoClient = require(‘mongodb’).MongoClient;

const uri = “mongodb+srv://m220student:m220password@cluster0-jncib.mongodb.net/test?retryWrites=true&w=majority”;

const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

client.connect(err => {

const collection = client.db(‘sample_mflix’).collection(“movies”);

console.log(‘connected’)

collection.find().toArray(function(err, res){

if(err){

  console.log(err)

}else{

console.log(res)

client.close();

}

});

// perform actions on the collection object

// client.close();

});

//and result is coming as:
connected
MongoError: server is closed
at Server.getMore (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\sdam\server.js:332:16)
at Cursor._getMore (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:424:17)
at nextFunction (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:784:10)
at Cursor._next (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:202:5)
at fetchDocs (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\operations\to_array.js:27:14)
at D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\operations\to_array.js:56:9
at handleCallback (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:32:5)
at nextFunction (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:879:5)
at D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:754:7
at done (D:\xampp\htdocs\program\node js\workspace\node_modules\mongodb\lib\core\cursor.js:463:7) {
name: ‘MongoError’,
[Symbol(mongoErrorContextSymbol)]: { isGetMore: true }
}

Try replacing this with below one:

const host = process.env.MFLIX_DB_URI
    const client = await MongoClient.connect(
      host,
      { useNewUrlParser: true },
    )
    const mflix = client.db(process.env.MFLIX_NS)

Making sure that you have cluster string in .env file.

Kanika

Hi Kanika,
I followed your guideline, but i got below error:
D:\xampp\htdocs\program\node js\workspace\mflix-js\src\migrations>node movie-last-updated-migration.js
Error during migration, MongoParseError: URI malformed, cannot be parsed


//new info
//if try with limit upto limit(100) it is retrieving but i entire 23539 document, how can i achieve that please guide if possible.

const MongoClient = require(‘mongodb’).MongoClient;

const uri = “mongodb+srv://m220student:m220password@cluster0-jncib.mongodb.net/test?retryWrites=true&w=majority”;

// const uri = ‘mongodb://localhost:27017’

const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

client.connect(err => {

const collection = client.db(‘sample_mflix’).collection(“movies”);

console.log(‘connected’)

collection.find({},{ projection: {lastupdated: 1}}).limit(100).toArray(function(err, res){

console.log(res)

})

// perform actions on the collection object

client.close();

});

Regards,
Pradeep Swain

Check for any special characters, sometimes double quotes get converted to special character while copy/paste.