MongoDB: remove unique constraint

て烟熏妆下的殇ゞ 提交于 2019-11-30 22:39:43

问题


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:


"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.



来源:https://stackoverflow.com/questions/12337388/mongodb-remove-unique-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!