Have to change schema in production
In MongoDB, we don't have to change schema, right?


MongoDB does not require you to predeclare schema, but there is still some onus for an administrator to remove unneeded indexes or data constraints. The Mongoose Node.js framework you are using includes a declarative client-side schema, but the MongoDB server is unaware of any changes to field or index usage. 

By default Mongoose creates new indexes on startup (depending on the config.autoIndex option of your schema), but does not have any tracking to remove or alter index definitions once created. Since you had previously created a unique index on username, the index will need to be manually dropped if you no longer require that constraint.

The duplicate key error message indicates the index name and offending key value:


  E11000 duplicate key error index: news.users.$username_1  dup key: { : null }


Any documents missing the field in a unique index will have a value of null indexed. You cannot have two documents in the same collection missing a field in a unique index unless that index is configured as a  sparse or partial index.

You can manually drop the problematic index using the mongo shell or an admin tool. For example:
db.getSiblingDB('news').users.dropIndex({username:1})


  Additionally, I backed up the previous database of production, and copied to development, and restored it. Oddly, unlike production, creating two new users did not give error in development.


The difference in outcome will depend on whether the target database had the unique index previously created.