1

Complicated issue, and one that I doubt many people have to run into... but I'm trying to manage schemas dynamically post-runtime, as in update/replace schemas that have already been loaded. Basically an app that lets you create, register, and later update schemas (to then populate). Of course, trying to redefine a schema that's already been registered results in this error:

OverwriteModelError: Cannot overwrite [myModelName] model once compiled.

Is there any way to destroy a mongoose Model/Schema once it's been created? I've checked the API docs and searched for answers to no avail. Creating new schemas on the fly works like a champ - but updating them throws me against the brick wall.

Would rather not fork mongoose to add this functionality if I can at all help it :)

Thanks for any help!

kwhitley
  • 745
  • 5
  • 8
  • That is a fair enough question. It may also help to show some (even failing) code. Also give example usage, as there may be a different approach to solve this. – Neil Lunn Jun 30 '14 at 07:15

1 Answers1

1

try this

delete mongoose.connection.models[myModelName];

and then re-register/re-initialize it . it will work fine

Niraj Kashyap
  • 232
  • 2
  • 8