0

I'm using nodejs to migrate data into mongodb, the json file which i'm trying to migrate is too large, when I try to migrate just some data (about 8000 docs), it works, otherwise I'm getting this issue :

MongooseError: Operation `products.insertOne()` buffering timed out after 10000ms

at Timeout.<anonymous> (\migration- 
module\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:148:23)

at listOnTimeout (internal/timers.js:557:17)

at processTimers (internal/timers.js:500:7)

MongooseError: Operation `products.insertOne()` buffering timed out after 10000ms

 at Timeout.<anonymous> (\migration- 
module\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:148:23)

at listOnTimeout (internal/timers.js:557:17)

at processTimers (internal/timers.js:500:7)

the script :

/** @format */
fs = require('fs')
var mongoose = require('mongoose')
 mongoose.connect('mongodb://host')
 mongoose.Promise = global.Promise
 var schema = new mongoose.Schema({
 //some fields
  })
  var d = fs.readFileSync('./data/file.json', 'utf8', (err, data) => {
   if (err) throw err
   return d
   })
  var e = JSON.parse(d)
  var Product = mongoose.model('product', schema)
  //console.log(e)
  for (var i = 0; i < e.length; i++) {
   // data process
   // another data process
  var product = new Product(e[i])
  product.save(function (err) {
  if (err) return console.log(err)
  })
 }
 e.length === i ? console.log('Migration Done successfully') : ''

any solution to get ride of this problem please ?

7
  • Check this : stackoverflow.com/questions/40585705/… - You need to configure timeouts or turn them off Commented Feb 25, 2022 at 9:06
  • I tried this this, I'm getting the same issue. Commented Feb 25, 2022 at 9:23
  • It's not connection, it's buffering. Turn it off on schema level, or globally mongoose.set('bufferCommands', false); Commented Feb 25, 2022 at 9:30
  • I put this command under mongoose connect but I got this error :MongooseError: Cannot call products.insertOne() before initial connection is complete if bufferCommands = false. Make sure you await mongoose.connect() if you have bufferCommands = false. Commented Feb 25, 2022 at 9:38
  • Yes, please use async/await or .then/.catch promises. Commented Feb 25, 2022 at 10:22

1 Answer 1

0

Disabling buffering and few other fixes:

fs = require('fs')
var mongoose = require('mongoose')

mongoose.set('bufferCommands', false);
var schema = new mongoose.Schema({
 //some fields
})
var Product = mongoose.model('product', schema)

var d = fs.readFileSync('./data/file.json', 'utf8', (err, data) => {
   if (err) throw err
     return data   // <= corrected here
})

var e = JSON.parse(d);
  //console.log(e)

async function main() {
  await mongoose.connect('mongodb://host')   // <= corrected here
  for (var i = 0; i < e.length; i++) {
     // data process
     // another data process
     var product = new Product(e[i]);
     await product.save();     // <= corrected here
  })
}

main().then(()=>{console.log('Migration Done successfully')})
Sign up to request clarification or add additional context in comments.

3 Comments

it worked for a while (35 min) then it gave this error :(node:396) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 1 to @ip:27017 closed. , is it normal ?
No, unhandled rejections are never normal. Please don't just copy-paste the code. It's here to illustrate the answer to the original question. In real-life application you wrap all async calls in try-catch and gracefully shut down on exception. This particular one says the database has gone. You will need to check database logs to understand why it happened.
you are right :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.