MongoDB: remove unique constraint

2019-02-16 12:35发布

I'm trying the "Develop a RESTful API Using Node.js With Express and Mongoose" example and I ran into a problem with the MongoDB Schema:

POST: 
{ title: 'My Awesome T-shirt 2',
  description: 'All about the details. Of course it\'s black.',
  style: '12345' }
{ [MongoError: E11000 duplicate key error index: ecomm_database.products.$style_1  dup key: { : "12345" }]
  name: 'MongoError',
  err: 'E11000 duplicate key error index: ecomm_database.products.$style_1  dup key: { : "12345" }',

there is a unique contraint in the schema definition:

var Product = new Schema({  
    title: { type: String, required: true },  
    description: { type: String, required: true },  
    style: { type: String, unique: true },  
    modified: { type: Date, default: Date.now } });

how do I get rid off that? When I remove unique: true and restart the app, the schema doesn't get updated.

How does mongodb handle "alters" to the schema?

1条回答
叛逆
2楼-- · 2019-02-16 13:30

"How does mongodb handle "alters" to the schema?"

MongoDB is schema-less

The only thing there uniqueness is enforced is on the indexing level where you can define indexes on a collection with unique criteria. So you may want to remove the related index and re-created it if necessary.

查看更多
登录 后发表回答