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忘记该模型以便可以重新创建它。
mongoose.model('Book', bookSchema);
mongoose.model('Book', bookSchema);
Currently the above throws an exception.
目前上面抛出一个例外。
OverwriteModelError: Cannot overwrite 'Book' model once compiled.
OverwriteModelError:编译后无法覆盖'Book'模型。
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?
......而且没有任何错误。有任何想法吗?
2 个解决方案
#1
13
try this
尝试这个
delete mongoose.connection.models['Book'];
and then re-register/re-initialize it . it will work fine
然后重新注册/重新初始化它。它会工作正常
#2
11
It appears that you'd have to overwrite some of the source code in order to be able to remove a model an add a new one since Mongoose makes sure that a model doesn't exist before it's willing to create a new one, which may or may not be more than you care to do:
看来你必须覆盖一些源代码,以便能够删除模型并添加新的模型,因为Mongoose确保模型在它愿意创建新模型之前不存在,这可能是或者可能不是你想做的事:
if (this.models[name] && !collection) {
// model exists but we are not subclassing with custom collection
if (schema instanceof Schema && schema != this.models[name].schema) {
throw new MongooseError.OverwriteModelError(name);
}
return this.models[name];
}
Line 587 https://github.com/LearnBoost/mongoose/blob/master/lib/connection.js
第587行https://github.com/LearnBoost/mongoose/blob/master/lib/connection.js
Question Author's Update:
Thanks to this answer I discovered that you can access the models defined on the connection through connection.models
. In my scenario I was testing a mongoose plugin with Mocha and and I wanted to clear the models between each unit test.
由于这个答案,我发现您可以通过connection.models访问连接上定义的模型。在我的场景中,我正在使用Mocha测试mongoose插件,并且我想在每个单元测试之间清除模型。
afterEach(function () {
mongoose.connection.models = {};
});
#1
13
try this
尝试这个
delete mongoose.connection.models['Book'];
and then re-register/re-initialize it . it will work fine
然后重新注册/重新初始化它。它会工作正常
#2
11
It appears that you'd have to overwrite some of the source code in order to be able to remove a model an add a new one since Mongoose makes sure that a model doesn't exist before it's willing to create a new one, which may or may not be more than you care to do:
看来你必须覆盖一些源代码,以便能够删除模型并添加新的模型,因为Mongoose确保模型在它愿意创建新模型之前不存在,这可能是或者可能不是你想做的事:
if (this.models[name] && !collection) {
// model exists but we are not subclassing with custom collection
if (schema instanceof Schema && schema != this.models[name].schema) {
throw new MongooseError.OverwriteModelError(name);
}
return this.models[name];
}
Line 587 https://github.com/LearnBoost/mongoose/blob/master/lib/connection.js
第587行https://github.com/LearnBoost/mongoose/blob/master/lib/connection.js
Question Author's Update:
Thanks to this answer I discovered that you can access the models defined on the connection through connection.models
. In my scenario I was testing a mongoose plugin with Mocha and and I wanted to clear the models between each unit test.
由于这个答案,我发现您可以通过connection.models访问连接上定义的模型。在我的场景中,我正在使用Mocha测试mongoose插件,并且我想在每个单元测试之间清除模型。
afterEach(function () {
mongoose.connection.models = {};
});