1

I am using nodejs to connect and insert records in compose for mongodb.I am getting the below error when I try to use insertMany and save records.

(node:10140) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Writes to conf ig servers must have batch size of 1, found 23

Below is my code snippet:

MongoClient.connect(url, function(err, db) {

    if(err){
    console.log('Error is ' + err);
    }
    else{
     console.log("Connected correctly to server");
    try {
    var col = db.collection('offshift');
        col.insertMany(result).then(function(result){
         res.json({message:'Data saved succesfully!',error_code:0, data: result});
       });

    } catch (e) {
       console.log(e);
    }
     db.close();
    }
});
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Monesh
  • 21
  • 4

2 Answers2

1

I was trying to insert records in Admin. Then I changed DB name and created user with read/write permission for the new DB . Changed my connection string with correct DB name and newly created user for access. This resolved the issue.

You can refer to below link for more details: https://www.compose.com/articles/avoid-storing-data-inside-admin-when-using-mongodb/

Monesh
  • 21
  • 4
0

If you do not use promise unable to use .then(). So instead of .then() you can use callback function. can try bellow code

var col = db.collection('offshift');
  col.insertMany(result, function(error, result){
       if(error){
          console.log('Error is on insert', error);
        } else
           res.json({message:'Data saved succesfully!',error_code:0, data: result});
});

OR

you can use .exec() that return promise and then can use .then()

var col = db.collection('offshift');
        col.insertMany(result).exec().then(.....)
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68