我正在嘗試從 nodejs 插入 mongodb
我的模型
const UserSchema = new mongoose.Schema({
surname:{
type: String,
required: true,
},
firstName:{
type: String,
required: true,
},
password:{
type: String,
required: true,
},
}, {timestamps: true})
當我插入第一個用戶時,它運行良好,但如果我嘗試創建另一個用戶,我將收到以下錯誤
E11000 duplicate key error collection: xxxxxxx.users index: name_1 dup key: { name: null }
但我的模型上沒有 name_1 索引。
我該如何解決這個問題?
uj5u.com熱心網友回復:
好像在你過去的某個時刻,你有
{
name: String,
required: true,
unique: true
}
在您的模型上,并已將其洗掉。通過添加
db.collection.dropIndex('name_1')應該可以解決問題,或者如果您有像 Mongo Compass 這樣的 UI,您也可以將其從 UI 中洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/528581.html
