最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you remove a model from mongoose? - Stack Overflow

programmeradmin1浏览0评论

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.

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?

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.

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?

Share Improve this question asked Oct 28, 2013 at 19:09 ChevCastChevCast 59.2k66 gold badges221 silver badges325 bronze badges 2
  • 1 Why would you want to change the model while your program is running? – EmptyArsenal Commented Oct 28, 2013 at 19:30
  • 5 @EmptyArsenal Clean unit tests without having to destroy and recreate mongoose between each test. Sorry, sometimes I remove context when asking questions because people like to focus on what I'm doing rather than the specific question I asked and that's not always beneficial to me. – ChevCast Commented Oct 28, 2013 at 20:17
Add a comment  | 

4 Answers 4

Reset to default 19

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:

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

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.

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

You can use the deleteModel method

mongoose.deleteModel(modelName);

This is actually a better way to get rid of models, schemas and collections in 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

发布评论

评论列表(0)

  1. 暂无评论