dedalux
dedalux

Reputation: 59

Correct procedure for restarting local MongoDB when Mongoose schema is changed

Hi all I'm new to learning how to develop with Node.js using Mongoose and Express. I have been having some issues with Mongodb not correctly reflecting my schema changes.

For example, I have written a model that supposedly auto-increment _id by 1 starting from 0 when each new document is inserted. I'm testing a few ways to do this model and so I'm constantly inserting different data.

I drop the collection each time to try to reinsert the data. However, _id instead of starting at 0, it starts at the previous largest _id.

This is just one of the issues that I have encountered. Some other similar Schema changes that I did on the fly also were not reflected.

I tried the following:

Is there something else that I need to shut down completely then restart before mongoose reflects the newest form of the schema? Thanks!

Upvotes: 1

Views: 1728

Answers (1)

robertklep
robertklep

Reputation: 203359

To start fresh with a new schema, it's best (if possible) to drop the old database entirely (db.dropDatabase() from the MongoDB shell), otherwise you may run into various issues (that can also be solved separately, if dropping the database isn't an option):

  • left-over collections, like the one used by the autoincrement plugin that you're using (I think it'll use a collection called identitycounters);
  • left-over indexes, which happens when you define field names in your schema that should be indexed, and you subsequently rename or delete one of those fields;

Restarting the database server won't solve these, they do require some manual administration.

Upvotes: 1

Related Questions