25

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?

Stefan Ernst
  • 2,781
  • 4
  • 25
  • 34

3 Answers3

15

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

  • 12
    That worked. For the reference: > use ecomm_database > db.products.dropIndexes(); { "nIndexesWas" : 2, "msg" : "non-_id indexes dropped for collection", "ok" : 1 } – Stefan Ernst Sep 09 '12 at 08:16
  • 1
    "MongoDB is *schema-less*" ... why, then, is there a whole section in their documentation on schema design? http://www.mongodb.org/display/DOCS/Schema+Design – Zaid Masud Sep 10 '12 at 15:55
  • @ZaidMasud: a process description with fewer connotations than "schema design" might be "data modelling" :). Data has to take some nominal shape (i.e. you still need to consider *schema design* to plan your data, queries, and indexes) .. but there is no rigid *schema* enforced on a document or collection level. This is a distinct contrast from the SQL approach of a [fixed schema](http://en.wikipedia.org/wiki/Information_schema) per table, where each field has a specific type and size. – Stennie Sep 11 '12 at 11:22
  • @Stennie yes the term has potential for misuse... but the way I like to think of it is that the *data is stored* in a schema-less fashion i.e. JSON and its corresponding BSON is by definition schema-less http://blog.mongodb.org/post/119945109/why-schemaless. However the database considered as an entity is *not* schema-less, as collections give it a database schema. In other words, while the data is represented in a schema-less format, the database is not. – Zaid Masud Sep 11 '12 at 11:37
  • 2
    three hours of my life is gone, but thank you so much for this discovery. db.users.dropIndexes(); def worked for me. – JZ. Nov 05 '12 at 08:56
  • It worked fine with db.collections.dropIndexes() but now i ran into another problem, if i drop these indexes and later on when i try to set unique attribute again, mongoose is not creating indexes again automatically. can u help with that ?? – Daga Arihant Nov 14 '16 at 06:37
13

I hope this will be helpful.

db.collection_name.getIndexes()

Returns an array that holds a list of documents that identify and describe the existing indexes on the collection. Now find your index name from list and then remove that index as below:

db.users.dropIndex("your_index_name_here")

Here is an example:

db.product.dropIndex({ style: 1 })
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
Manoj Rana
  • 3,068
  • 1
  • 24
  • 34
8

When you add a unique constrains on to mongoose schema than an index will be created on the same key. For eg: if you have a schema key called style then an index will be created as style_1. Even when you modify the schema the index which is already created will be still there. You need to delete this index in order to reset the unique constrains.

If you have mongoDB Compass installed then you can go to collection->indexes and delete the same. The same interface is available on the Atlas Web interface as well. In my case I had an index on the key url, which I have deleted.

enter image description here

Anees Hameed
  • 5,916
  • 1
  • 39
  • 43