猫鼬批量插入错误
Posted
技术标签:
【中文标题】猫鼬批量插入错误【英文标题】:mongoose bulk insert error 【发布时间】:2017-04-09 12:51:42 【问题描述】:我正在使用node.js
并使用mongoose
批量插入方法,即model.collection.insert
在MongoDB 中插入数据。当此方法执行时,会出现以下错误:
但是数据在数据库中插入成功。有人能告诉我为什么会出现这个错误吗?
[MongoError: write operation failed]
name: 'MongoError',
message: 'write operation failed',
driver: true,
code: 11000,
writeErrors:
[ code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ,
code: [Getter],
index: [Getter],
errmsg: [Getter],
getOperation: [Function],
toJSON: [Function],
toString: [Function] ]
代码如下:
mOfferCake.collection.insert(jsonArr, 继续错误:真, 继续前进:是的 , onInsert);
1) mOfferCake 是模型,下面是代码:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var offerCakeSchema = new Schema(
offer_id:
type: Number,
unique: true
);
var datetime = new Date();
var mm = datetime.getMonth() + 1;
if (mm < 10)
mm = '0' + mm;
var dd = datetime.getDate();
if (dd < 10)
dd = '0' + dd;
var yyyy = datetime.getFullYear();
var collectionName = 'MX_' + yyyy + '_' + mm + '_' + dd;
module.exports = mongoose.model(collectionName, offerCakeSchema);
2) jsonArr 是 JSON 对象数组
3) onInsert 是回调函数
function onInsert(err, docs)
if (err)
console.log(err);
else
console.error("Cron bulk insert Successfully");
节点版本:v4.5.0 猫鼬版本:4.6.5
【问题讨论】:
【参考方案1】:代码:11000,
E11000 表示重复键错误索引。
您可能插入了集合中已存在_id
的文档。
【讨论】:
每次我插入新数据集并自动生成_id以上是关于猫鼬批量插入错误的主要内容,如果未能解决你的问题,请参考以下文章