3

Below is my code for bulk insertion.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    "xyz": String
}, {
    collection: 'user'
});

Api.prototype.Users = mongoose.model('Users', UserSchema);

resultData = [{
    "xyz": "abc"
}, {
    "xyz": "44545"
}, {
    "xyz": "545"
}]

Api.prototype.Users.collection.insert(resultData, function(err, data) {
    if(err) {
        console.log(err);
    } else {
        console.log("User inserted : " + data.result.n);
    }
});

But i'm getting this error: MongoError: Invalid Operation, No operations in bulk

MongoError: Invalid Operation, No operations in bulk at Function.MongoError.create (/myapp/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11) at toError (/myapp/node_modules/mongoose/node_modules/mongodb/lib/utils.js:114:22)

Mûhámmàd Yäsår K
  • 1,492
  • 11
  • 24
williams
  • 31
  • 1
  • 5

1 Answers1

0

I have had this error when trying to bulk insert an empty array using insertMany. You should check for an empty array before attempting a bulk insert.

if (resultData.length > 0) {
  // do bulk insert...
}
cbaigorri
  • 2,467
  • 25
  • 26