2017-12-24 9 views
1

私はこのエラーで数時間立ち往生していました。重複したキーエラーの原因を突き止めることはできません。MongoDBでこの重複キーエラーを取り除く方法を見つけることができません

重複するキーがnullであり、これが何を参照するのかわかりません。ここでエラーがある:

{ MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: my_db.schools.$username_1 dup key: { : null } 

私は学校のスキーマを持っている:

var mongoose = require('mongoose'); 
 
var passportLocalMongoose = require('passport-local-mongoose'); 
 

 
var schoolSchema = new mongoose.Schema({ 
 
    name: String, 
 
    location: String, 
 
    author: { 
 
     id: { 
 
      type: mongoose.Schema.Types.ObjectId, 
 
      ref: "User" 
 
     }, 
 
     username: String 
 
    }, 
 
    code: Number, 
 
    members: [ 
 
     { 
 
      type: mongoose.Schema.Types.ObjectId, 
 
      ref: "User" 
 
     } 
 
    ], 
 
    cluborgs: [ 
 
     { 
 
      type: mongoose.Schema.Types.ObjectId, 
 
      ref: "Cluborg" 
 
     } 
 
    ], 
 
    date_joined: {type: Date, default: Date.now} 
 
}); 
 

 
schoolSchema.plugin(passportLocalMongoose); 
 

 
module.exports = mongoose.model("School", schoolSchema);

そして、このスキーマに関連するいくつかのルート:

var express = require('express'); 
 
var router = express.Router(); 
 
var School = require('../models/school'); 
 
var middleware = require('../middleware'); 
 

 
//index 
 
router.get("/", function(req, res) { 
 
    School.find({}, function(err, allSchools) { 
 
     if (err) { 
 
      console.log(err); 
 
     } else { 
 
      res.render("schools/index", {schools: allSchools}); 
 
     } 
 
    }); 
 
}); 
 

 
//new 
 
router.get("/new", middleware.isLoggedIn, function(req, res) { 
 
    res.render("schools/new"); 
 
}); 
 

 
//create 
 
router.post("/", middleware.isLoggedIn, function(req, res) { 
 
    var author = { 
 
     id: req.user._id, 
 
     username: req.user.username 
 
    }; 
 
    
 
    var school = { 
 
     name: req.body.school.name, 
 
     location: req.body.school.location, 
 
     author: author, 
 
     code: req.body.school.code 
 
    }; 
 
    School.create(school, function(err, newlyCreated) { 
 
     if (err) { 
 
      console.log(err); 
 
     } else { 
 
      req.flash("success", "Successfully created " + newlyCreated.name); 
 
      res.redirect("/schools"); 
 
     } 
 
    });  
 
}); 
 

 
//show 
 
router.get("/:id", middleware.isLoggedIn, function(req, res) { 
 
    School.findById(req.params.id) 
 
     .populate("cluborgs") 
 
     .exec(function(err, foundSchool) { 
 
      if (err) { 
 
       console.log(err); 
 
      } else { 
 
       res.render("schools/show", {school: foundSchool}); 
 
      } 
 
    }); 
 
}); 
 

 
//edit 
 
router.get("/:id/edit", middleware.checkSchoolOwnership, function(req, res) { 
 
    School.findById(req.params.id, function(err, foundSchool) { 
 
     if (err) { 
 
      console.log(err); 
 
     } else { 
 
      res.render("schools/edit", {schools: foundSchool}); 
 
     } 
 
    }); 
 
}); 
 

 
//update 
 
router.put("/:id", middleware.checkSchoolOwnership, function(req, res) { 
 
    School.findByIdAndUpdate(req.params.id, req.body.school, function(err, updatedSchool) { 
 
     if (err) { 
 
      console.log(err); 
 
     } else { 
 
      req.flash("success", "Successfully updated school."); 
 
      res.redirect("/schools/" + req.params.id); 
 
     } 
 
    }); 
 
}); 
 

 
//destroy 
 
router.delete("/:id", middleware.checkSchoolOwnership, function(req, res) { 
 
    School.findByIdAndRemove(req.params.id, function(err) { 
 
     if (err) { 
 
      res.redirect("/schools"); 
 
     } else { 
 
      res.redirect("/schools"); 
 
     } 
 
    }); 
 
}); 
 

 
module.exports = router; //put this in EVERY single one of your routes file to get rid of router errors

2番目の学校を作成するとエラーが発生します。 私は何が間違っていますか?

答えて

0

Mongooseプロパティからunique: trueを削除しても、自動的にMongoDBからインデックスは削除されません。

さらに、Mongooseスキーマから完全にプロパティを削除しても、自動的にMongoDBから関連するインデックスは削除されません。

あなたの場合のように、MongoDBはスキーマから削除した一意のSchool.username値を期待しているため、すべてのドキュメントでnull値としてデフォルト設定されています。

インデックスにunique: falseが設定されていると思われますが、私は個人的にMongoDB CLIを使用してインデックスを削除しています。

+0

私は一意に追加する必要があります:falseにschool.author.username? – RaspberryPiDudePlusWebDevToo

+0

これが最初に「一意:真」だった場合、はい。 MongooseがMongoDBモデルを更新した後にそれを削除するのは問題ありません。 –

+0

これで修正されませんでした。私にできることはほかにありますか? – RaspberryPiDudePlusWebDevToo

関連する問題