Weird behavior on mongodb

Hi, I’m currently working on the exercise tracker project. I’m using mongo v3.6.3 Then I’m getting a weird warning on my console which is mongoose: Cannot specify a custom index on_idfor model name "Users", MongoDB does not allow overwriting the default_idindex. See http://bit.ly/mongodb-id-index. It only shows when I set the index field to true in my schema. Which is

const usersSchema = new Schema({
  username: {
    type: String,
    unique: true,
    required: true
  },
  _id: {
    type: String,
    index: true,
    default: shortid.generate
  },
})

Though it is still working as expected but it just bother me when I see it on my console. Where I think there’s something wrong. But when I remove it or set to false it is still working. The problem I have is that the unique validation doesn’t work.

Mongodb creates a default index for the _id field, you can just remove the index: true line.

https://docs.mongodb.com/manual/indexes/#default-id-index

1 Like