简体   繁体   中英

MongoError: Invalid Operation, No operations in bulk

I'm unable to do bulk insert to mongo using mongoose.

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

// Define our results schema
var webSchema = new Schema({"abc" : String},{
  collection: 'web_v2'
});

MyApi.prototype.Webs= mongoose.model('Webs', webSchema);

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

MyApi.prototype.Webs.collection.insert(resultData, function (err, myDocuments) {
  if (err) {
    console.log(err);
  } else {
    console.log("web inserted : " + myDocuments.result.n);
  }
});

I'm getting the following error

MongoError: Invalid Operation, No operations in bulk
at Function.MongoError.create (/pathtoapp/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
at toError (/pathtoapp/node_modules/mongoose/node_modules/mongodb/lib/utils.js:114:22)
at OrderedBulkOperation.execute (/pathtoapp/node_modules/mongoose/node_modules/mongodb/lib/bulk/ordered.js:500:11)
at bulkWrite (/pathtoapp/node_modules/mongoose/node_modules/mongodb/lib/collection.js:582:8)
at Collection.insertMany (/pathtoapp/node_modules/mongoose/node_modules/mongodb/lib/collection.js:477:44)
at Collection.insert (/pathtoapp/node_modules/mongoose/node_modules/mongodb/lib/collection.js:753:15)
at NativeCollection.(anonymous function) as insert (/pathtoapp/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:136:28)
at /pathtoapp/index.js:481:57
at /pathtoapp/node_modules/async/lib/async.js:721:13
at /pathtoapp/node_modules/async/lib/async.js:52:16
at async.forEachOf.async.eachOf (/pathtoapp/node_modules/async/lib/async.js:236:30)
at _parallel (/pathtoapp/node_modules/async/lib/async.js:712:9)
at Object.async.parallel (/pathtoapp/node_modules/async/lib/async.js:726:9)
at /pathtoapp/index.js:479:43
at /pathtoapp/node_modules/async/lib/async.js:721:13
at /pathtoapp/node_modules/async/lib/async.js:52:16

anyone please let me know what am i doing wrong.?

Most likely you are trying to insert an empty array.

Here is the MongoDB Jira site reference to this error

This is probably caused by your last insertMany attempting to insert an empty array [ ] . I suggest you insert a guard before insertMany.

Just check if the bulk instance has operations before calling execute with this function:

const BulkHasOperations = (b) => b && b.s && b.s.currentBatch && b.s.currentBatch.operations && b.s.currentBatch.operations.length > 0;
...
const bulk = db.collection('something').initializeUnorderedBulkOp();
...
BulkHasOperations(bulk) && bulk.execute();

Here is my check in a basic if block if you were using the raw MongoDB driver for Node js, it might help:

let col = dbContext.collection("collectionName");
let bulk = col.initializeUnorderedBulkOp();

if(bulk && bulk.s && bulk.s.currentBatch 
&& bulk.s.currentBatch.operations 
&& bulk.s.currentBatch.operations.length > 0){
//execute operations
}

Check the array that you're passing to bulkWrite([...elements]). The elements array should not be empty, all of its elements need to be an object, and the objects need to contain properties of operation type, filter and update field. They too CAN NOT be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM