如何解决 Mongo Error: E11000 重复键错误集合

时间:2021-07-23 19:03:33

标签: javascript node.js mongodb mongoose

我希望我的用户拥有一部电话电子邮件(至少其中一个),这就是我在架构中为电话和电子邮件使用这些选项的原因:

const schema = new Schema({

    phone: { type: String, unique: true, required: false },
    email: { type: String, unique: true, required: false },
    passwordHash: { type: String, required: true },
    role: { type: String, required: true },
    verificationToken: String

});

这是将新文档保存到集合的代码:

async function register(params, origin) {

    // create account object
    console.log('params', params); 
    const account = new db.Account(params);

    // first registered account is an admin
    const isFirstAccount = (await db.Account.countDocuments({})) === 0;
    account.role = isFirstAccount ? Role.Admin : Role.User;
    account.verificationToken = randomTokenString();

    // hash password
    account.passwordHash = hash(params.password);

    console.log('account reached 1');

    // save account

    try {
        await account.save();
    } catch(err) {
        console.log(err) // this gets the error
    }
    

    console.log('account reached 2');



    console.log('account reached 3');

    return { success: true, msg: 'Registration successful' }

}

我向注册函数发送了两种不同类型的 params,一种是电话属性,另一种是电子邮件。当我使用带有电子邮件属性的 params 类型时一切正常并且我将一个新用户保存到集合中时,这是此处的控制台:

enter image description here

但是当我使用电话而不是电子邮件时,我收到此错误并且没有任何内容保存到集合中: enter image description here

我该如何解决这个错误?我是 mongoDB 的新手请跟我一起轻松...

编辑 1: 我使用了这个,但发生了同样的错误(这是整个架构):

const schema = new Schema({

    phone: { type: String, required: false },
    email: { type: String, required: false },
    passwordHash: { type: String, required: true },
    title: { type: String, required: false },
    firstName: { type: String, required: false },
    lastName: { type: String, required: false },
    acceptTerms: Boolean,
    role: { type: String, required: true },
    verificationToken: String,
    verified: Date,
    resetToken: {
        token: String,
        expires: Date
    },
    passwordReset: Date,
    created: { type: Date, default: Date.now },
    updated: Date,
    userLevel: { type: String, default: 'starter' },
    cards: {
        starter : [unitSchema], 
        intermediate : [unitSchema], 
        advanced : [unitSchema],   
    }

});

schema.index({'phone': 1, 'email': 1}, {unique: true});

编辑 2:这也不起作用:

const schema = new Schema({

    phone: { type: String, unique: true, required: false, sparse: true },
    email: { type: String, unique: true, required: false, sparse: true },
    ...

编辑 3: 使用了这个但它不起作用(发生了一些变化或者这根本不是解决方案!!)

const schema = new Schema({

    phone: {
        type: String, 
        trim: true, 
        index: {
            unique: true,
            partialFilterExpression: {phone: {$type: "string"}}
        }
    }, 

    email: {
        type: String, 
        trim: true, 
        index: {
            unique: true,
            partialFilterExpression: {email: {$type: "string"}}
        }
    },

    // phone: { type: String, unique: true, required: false, sparse: true },
    // email: { type: String, unique: true, required: false, sparse: true },
    ...

0 个答案:

没有答案