你如何从猫鼬中删除模型? [英] How do you remove a model from mongoose?
本文介绍了你如何从猫鼬中删除模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我并不是指删除文件或文件。我的意思是完全删除模型,以便猫鼬不再意识到它。在声明模型之后,我无法弄清楚如何让mongoose忘记该模型以便可以重新创建它。
I don't mean remove a document or documents. I mean remove the model entirely so that mongoose is no longer aware of it. After declaring a model I can't figure out how to make mongoose forget that model so that it could be recreated.
mongoose.model('Book', bookSchema);
mongoose.model('Book', bookSchema);
目前上述情况引发异常。
Currently the above throws an exception.
OverwriteModelError:编译后无法覆盖'Book'模型。
OverwriteModelError: Cannot overwrite 'Book' model once compiled.
我希望能做到像这样......
I'd like to be able do something like this...
mongoose.model('Book', bookSchema);
mongoose.removeModel('Book');
mongoose.model('Book', bookSchema);
...并且不会收到任何错误。任何想法?
...and not get any errors. Any ideas?
推荐答案
试试这个
delete mongoose.connection.models['Book'];
然后重新注册/重新初始化它。它会正常工作
and then re-register/re-initialize it . it will work fine
这篇关于你如何从猫鼬中删除模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文