简体   繁体   English

如何从猫鼬中删除模型?

[英]How do you remove a model from 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: 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?有任何想法吗?

try this尝试这个

delete mongoose.connection.models['Book'];

and then re-register/re-initialize it .然后重新注册/重新初始化它。 it will work fine它会正常工作

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 .感谢这个答案,我发现您可以通过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.在我的场景中,我正在使用Mocha测试 mongoose 插件,并且我想清除每个单元测试之间的模型。

afterEach(function () {
    mongoose.connection.models = {};
});

This is actually a better way to get rid of models, schemas and collections in mongoose这实际上是摆脱 mongoose 中的模型、模式和集合的更好方法

mongoose.connections.forEach(connection => {
  const modelNames = Object.keys(connection.models)

  modelNames.forEach(modelName => {
    delete connection.models[modelName]
  })

  const collectionNames = Object.keys(connection.collections)
  collectionNames.forEach(collectionName => {
    delete connection.collections[collectionName]
  })
})

const modelSchemaNames = Object.keys(mongoose.modelSchemas)
modelSchemaNames.forEach(modelSchemaName => {
  delete mongoose.modelSchemas[modelSchemaName]
})

Reference: https://github.com/Automattic/mongoose/issues/2874#issuecomment-388588452参考: https : //github.com/Automattic/mongoose/issues/2874#issuecomment-388588452

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM