Search code examples
node.jsmongodbcompose-db

Unable to insert records using insertMany using compose for mongodb


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

Solution

  • 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/