Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot specify a custom index on _id #144

Open
burakkilic opened this issue Apr 3, 2019 · 2 comments
Open

Cannot specify a custom index on _id #144

burakkilic opened this issue Apr 3, 2019 · 2 comments

Comments

@burakkilic
Copy link

Hi,

I'm getting

mongoose: Cannot specify a custom index on `_id` for model name "Shop", MongoDB does not allow overwriting the default `_id` index. See http://bit.ly/mongodb-id-index

after upgrading Mongoose to 5.x

How can I solve that? Do you have any idea?

@hom
Copy link

hom commented Apr 17, 2019

@burakkilic If you want to make _id shortly, you can use shortid when you create the model.
such as this:

const shortid = require('shortid');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const UserSchema = new Schema({
    _id: { type: String, default: shortid.generate() },
    username: { type: String  },
    password: { type: String  },
  });

  return mongoose.model('User', UserSchema);
}

or if you want to rewrite _id, you must get the mongodb client instant and save the _id yourself.

@privateOmega
Copy link

@burakkilic If you want to make _id shortly, you can use shortid when you create the model.
such as this:

const shortid = require('shortid');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const UserSchema = new Schema({
    _id: { type: String, default: shortid.generate() },
    username: { type: String  },
    password: { type: String  },
  });

  return mongoose.model('User', UserSchema);
}

or if you want to rewrite _id, you must get the mongodb client instant and save the _id yourself.

Pass shortid.generate instead of shortid.generate()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants