0
const UserSchema = new Schema ({
  mobile: {type: String, unique: true, required: [true, 'Mobile Required']},
  verified: {type: Boolean, default: false},
  password: {type: String, required: [true, 'Password Required']},
  salt: {type: String, required: [true, 'Salt Required']},
  type: {type: Number, required: [true, 'User Type Required']},
  storeId: {type: Schema.Types.ObjectId, ref: 'Store', default: null},
  username: {type: String, lowercase: true, unique: true},
  userInfo: {
    name: String,
    email: String,
    address: String,
    postcode: String,
  },
  likes: [{type: Schema.Types.ObjectId, ref: 'Post'}],
}, {
  timestamps: true,
  versionKey: false,
});

This is the data schema, after the first insert, the second one always out put error. The email field is not indexed and the email field of the first one is not null. The second insert is not null either.

E11000 duplicate key error collection: gdaymobile.users index: email_1 dup key: { : null }
qwang07
  • 1,136
  • 2
  • 11
  • 20
  • Which email field? I see a path to `userInfo.email` but the error suggests you actually have a "unique" key looking for a field on the document "root" named just `email`. Thus since your documents do not contain this data then the index considers this value to be `null` on all documents. A "unique" index means only 1 document can have this `null`, so any more than 1 document throws the error. Hop into the shell and remove the index you created earlier in error: `.dropIndex({ "email": 1 })` – Neil Lunn May 31 '17 at 03:27
  • whats does users.stats().indexSizes return? – Atish May 31 '17 at 03:28
  • @Astro `email_1 dup key: { : null }` tells you exactly what is going on. – Neil Lunn May 31 '17 at 03:29
  • this was needed to check if email field is not indexed – Atish May 31 '17 at 03:48
  • @Astro `email_1` means that there **is** an index in ascending order defined for that field. That field is not present in any documents in the collection. Hence the `null`. Hence the duplicate key error. Hence the solution to drop the index. We don't need someone to print out the list of indexes when the error already tells us the index is indeed there. Hence the duplicate question and why this one is closed. – Neil Lunn May 31 '17 at 04:19
  • @NeilLunn I don't email field other than userInfo.email, and userInfo.email is not indexed. Also, there has no null value of userInfo.email document insert into the database. – qwang07 May 31 '17 at 10:36
  • Please read the linked duplicate and even look at the comments above. I know there is an index created on email because it is named in the error message. It does not need to be in your document to have an index created, because you or someone has created it before. What you need to do is stop your application, log into a mongo shell and perform a `.dropIndexes()` command or the specific `.dropIndex({ "email": 1 })` on the collection. And when you start your application again, you will not see the duplicate key error occur. Actually try it. We know its the problem because you are not the first – Neil Lunn May 31 '17 at 10:41
  • It works. Thanks a lot. – qwang07 Jun 01 '17 at 01:31

0 Answers0