【发布时间】:2020-05-17 13:43:32
【问题描述】:
我已将 firstname 和 lastname MongoDB 字段指定为 不 是唯一的,但我仍然遇到一个问题,我不能说非唯一的名字和姓氏。
如何向 MongoDB 指定 firstname 和 lastname 的重复项是可以的?
extend type Mutation {
signUp(
firstname: String!
lastname: String!
email: String!
password: String!
): Token!
...
const userSchema = new mongoose.Schema(
{
email: {
type: String,
unique: true,
required: true,
validate: [isEmail, 'Not a valid email'],
},
firstname: {
type: String,
unique: false
},
lastname: {
type: String,
unique: false
},
password: {
type: String,
required: true,
minlength: 7,
maxlength: 42,
},
},
{
timestamps: true,
},
);
错误:
E11000 duplicate key error collection: testDb.users index: firstname_1 dup key: { firstname: "Test" }
【问题讨论】:
-
您需要删除
users集合并运行您的应用程序。似乎您已经创建了唯一索引,现在您正在尝试撤消它 -
我不确定您是如何发现的(或者我会如何发现),但这确实有效。我放弃了我的 testDB 并且一切正常..奇怪