当前位置:  开发笔记 > 编程语言 > 正文

MongoError:无效操作,批量操作无操作

如何解决《MongoError:无效操作,批量操作无操作》经验,为你挑选了1个好方法。

我无法使用mongoose批量插入mongo.

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

我收到以下错误

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

有谁请让我知道我做错了什么.



1> Mercury..:

您很可能正在尝试插入一个空数组.

以下是MongoDB Jira站点对此错误的引用

这可能是由于您的上一次insertMany尝试插入空数组[].我建议你在insertMany之前插入一个警卫.

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有