2017-01-11 43 views
0

當過我嘗試插入新記錄我得到的forrlowing錯誤,實際上用戶名本身是不是我的模型的一部分,但我不知道爲什麼我我得到這個錯誤,任何人都可以猜錯。MongoError:insertDocument ::引起:: 11000 E11000重複鍵錯誤索引

我的錯誤,

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

我的收藏,

var StudentSchema = new Schema({ 
    title: { type: String, default: '' }, 
    first_name: { type: String, default: '' }, 
    last_name: { type: String, default: '' }, 
    email: { type: String, default: '' }, 
    display_name: { 
    type: String, 
    trim: true 
    }, 
    username: { 
    type: String, 
    validate: [validateUsername, 'Please enter a valid username: 3+ characters long, non restricted word, characters "_-.", no consecutive dots, does not begin or end with dots, letters a-z and numbers 0-9.'], 
    lowercase: true, 
    trim: true 
    }, 
}); 

我的指標,

[ 
    { 
     "v" : 1, 
     "key" : { 
      "_id" : 1 
     }, 
     "name" : "_id_", 
     "ns" : "project1.students" 
    }, 
    { 
     "v" : 1, 
     "unique" : true, 
     "key" : { 
      "username" : 1 
     }, 
     "name" : "username_1", 
     "ns" : "project1.students", 
     "background" : true 
    } 
] 

回答

1

我覺得您的用戶名進行索引,因此每當你嘗試插入一個文檔這個錯誤來自相同的用戶名。索引應該是唯一的。如果用戶名不是你的模型的一部分,你可以告訴你的模型模式。

+0

在我的模型我沒有任何的用戶名外地 – MMR

+0

你能告訴我到底什麼是您的最愛和你有什麼送? –

+0

編輯我的代碼。 – MMR

相關問題