“errmsg”:“E11000重复键错误集合:reduxpress.users索引:address.email_1 dup key:{:null}”

时间:2016-04-23 13:00:00

标签: javascript node.js mongodb express mongoose

{
  "status": "Error",
  "typeof": "object",
  "result": {
    "code": 11000,
    "index": 0,
    "errmsg": "E11000 duplicate key error collection: reduxpress.users index: address.email_1 dup key: { : null }",
    "op": {
      "mobile": "(950) 874-8901 x8729",
      "_id": "571b6e353731775404ac05b9",
      "points": 0,
      "created": "2016-04-23T12:44:37.111Z",
      "roles": "customer",
      "profileImageURL": "default.png",
      "email": "aditya24@gmail.com",
      "password": "VF1WQZ72HEyS0a6",
      "name": "Junior94",
      "__v": 0
    }
  }
}

//用户架构     '使用严格';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    crypto = require('crypto'),
    validator = require('validator');
// generatePassword = require('generate-password'),
// owasp = require('owasp-password-strength-test');

var validateLocalStrategyProperty = function(property) {
    return (property.length);
};

var validateLocalStrategyEmail = function(email) {
    return (validator.isEmail(email));
};

var UserSchema = new Schema({
    name: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty,
            'Please fill in your first name'
        ]
    },
    password: {
        type: String,
        default: ''
    },
    email: {
        type: String,
        unique: true,
        lowercase: true,
        trim: true,
        default: '',
        match: /.+\@.+\..+/,
        validate: [validateLocalStrategyEmail,
            'Please fill a valid email address'
        ]
    },
    mobile: {
        type: String,
        // unique: true,
        required: [true, 'User phone is required'],
        trim: true
    },
    profileImageURL: {
        type: String,
        default: 'default.png'
    },
    roles: {

        type: String,
        enum: ['user', 'admin', 'shop manager', 'customer',
            'operator'
        ],

        default: ['customer'],
        required: 'Please provide at least one role'
    },
    lastLogout: {
        type: Date
    },
    created: {
        type: Date,
        default: Date.now
    },
    resetPasswordToken: {
        type: String
    },
    resetPasswordExpires: {
        type: Date
    },
    points: {
        type: Number,
        default: 0
    }
});

module.exports = mongoose.model('User',UserSchema);

我正在尝试使用faker包保存用户生成数据,但我收到此错误我在database.can中没有任何重复的电子邮件。你告诉我是什么错误。我尝试了不同的东西,但没有得到任何结果

1 个答案:

答案 0 :(得分:1)

您的json文档的email字段的值超过null,并且email字段也是通知索引字段,并且您还指定了此字段unique,此抛出除外

首先删除像这样的电子邮件字段的索引

db.users.dropIndex( { "email": 1 } )

如果您需要提交电子邮件索引,并且提交的电子邮件必须将运动变为空

然后像这样创建sparse index

db.users.createIndex( { "email": 1 }, { sparse: true } )

并且sparse index的知识不仅仅是https://docs.mongodb.org/manual/core/index-sparse/