Sim*_*emi 3 mongoose node.js mongoose-schema
在下面的代码中,注释的电子邮件架构是以前的,我不再希望电子邮件是必需的,并且是唯一的,它应该是可选的。
我收到一条错误,说重复,为空“E11000重复键错误集合:mydb.lists索引:email_1 dup键:(电子邮件:null)”
const mongoose = require("mongoose");
const { Schema } = mongoose;
const alliedSchema = new Schema(
{
name: String,
phone: String,
email: {
unique: false,
type: String
},
// email:{
// type: String,
// unique: true,
// required: true,
// lowercase: true,
// validate(value){
// if(!validator.isEmail(value)){
// throw new Error('Email is invalid')
// }
// }
// },
picture: {
type: String,
default: "https://www.dialadocdirect.org/app/storage/images/noimage.jpg",
},
location: String,
type: String,
verified: {
type: Boolean,
default: false,
},
},
{
timestamps: true,
}
);
module.exports = mongoose.model("allied", alliedSchema);
Run Code Online (Sandbox Code Playgroud)
索引不会自动删除,因此,即使您注释掉架构,您的索引也将保留。至少有几种可能的解决方案:
db.getCollection('allied').dropIndexes()并重新启动应用程序。Mongoose 将自动创建集合中不存在的新索引(在这种情况下,它将重新创建除唯一之外的所有索引)。await alliedModel.syncIndexes();实际上取决于您如何创建新模型,但alliedModel应该是与 关联的模型alliedSchema)。这将简单地根据 Mongoose Schema 同步所有索引,并且只会删除unique属性,但保持所有其他索引不变。| 归档时间: |
|
| 查看次数: |
2025 次 |
| 最近记录: |