MongoDB E11000重复密钥错误 [英] MongoDB E11000 duplicate key error

查看:210
本文介绍了MongoDB E11000重复密钥错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,该模型在第一次POST后一直出错.我正在创建一个调度应用程序,它是X天数,包含房间和房间的时隙.

I have a model that keeps erroring out after the first POST. I'm creating a scheduling application, which is X number of days, with rooms, and time slots for the rooms.

我遇到的问题是在数据库中创建日对象.为了便于阅读,我将只有一个键值对

The issue I'm having is creating Day Objects in the database. For sake of easy reading I'm just going to have a single key value pair

day.model.js

var mongoose = require('mongoose');

// Day Schema
var daySchema = mongoose.Schema({
  name:{
    type: String,
    required: true,
  },
  createdAt:{
    type: Date,
    default: Date.now
  }
});

var Day = module.exports = mongoose.model('Day', daySchema);

// Get all Days
module.exports.getDays = function(callback, limit){
  Day.find(callback).limit();
};

// Add Day
module.exports.addDay = function(day, callback){
  var add = {
    name: day.name,
};
Day.create(add, callback);
};

day.routes.js

var express     = require('express');
var router      = express.Router();
var jwt         = require('jsonwebtoken');
var config      = require('../config/database');

Day = require('../models/day.model.js');

// Get all Days
router.get('/', function(req,res){
  Day.getDays(function(err, days){
    if(err){
      res.send(err);
    }
    res.json(days);
  }); 
});

// Add Day
router.post('/create', function(req,res){
  var day = req.body;
  Day.addDay(day, function(err, day){
    if(err){
      res.send(err);
    }
    res.json(day);
  });
});

module.exports = router;

示例JSON

  1. {"name": "Monday"}-这将很好地反映在数据库中
  2. {"name": "Tuesday"}-这将引发11000错误
  1. {"name": "Monday"}- this will reflect in the Database just fine
  2. {"name": "Tuesday"} - this will throw an 11000 error

错误

{
  "code": 11000,
  "index": 0,
  "errmsg": "E11000 duplicate key error collection: <collection-name>.days index: date_1 dup key: { : null }",
  "op": {
    "name": "Tuesday",
    "_id": "57fd89638039872dccb2230b",
    "createdAt": "2016-10-12T00:52:51.702Z",
    "__v": 0
  }
}

让我感到困惑的是,我为 User 设置了相同的设置,但是当涉及到创建新的 Day 时,会出现此重复的键错误.不知道我在想什么或做错了什么.谢谢

Where I'm confused is I have this same setup for a User but when it comes to making a new Day, this duplicate key error arises. Not sure what I'm missing or doing wrong. Thanks

推荐答案

我认为您具有days集合的模型,该模型具有date属性,该属性具有唯一索引date_1.

I think You had model for days collection with date attribute which had unique index date_1.

现在您已将其删除,但collection仍具有该索引.

Now You've removed it but collection still has that index.

所以这就是为什么:

重复键错误集合:.days索引:date_1 dup键:{:null}

duplicate key error collection: .days index: date_1 dup key: { : null }

这意味着您要插入另一条记录,其中date属性也为空.

it means You're inserting another record where date attribute is also null.

从控制台登录到mongodb并尝试执行以下操作:

log in to mongodb from console and try to do this:

db.collectionNameHere.getIndexes();
db.collectionNameHere.dropIndex('date_1');
db.collectionNameHere.getIndexes();

p.s.随时在您的问题或评论中提供任何其他数据,以帮助我/我们解决您的问题.

p.s. feel free to provide any additional data in Your question or in comments, to help me/us to solve Your issue.

这篇关于MongoDB E11000重复密钥错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆