How to get an object of all Mongoose instance models

I know what the mongoose.model("myModel")model returns myModel. But how can I get Mongoose to give me an array / object / list of all the models that are currently registered?

I think this is a bit of a design flaw.

+5
source share
2 answers

At the moment, there is an opposite means: Mongoose#modelNames().


You can access the object of all your models with mongoose.models. It looks like this:

models:
  { myModel:
    { [Function: model]
      modelName: 'myModel',
      auth: [Function],
      model: [Function: model],
      options: undefined,
      db: [Object],
      schema: [Object],
      collection: [Object],
      base: [Circular] } },

In my opinion, the poor use of private APIs is bad, so this is a bit of a Mongoose design flaw (see LearnBoost / mongoose # 1362 ).

+6
source

, :

var mongoose = require('mongoose');
var models = mongoose.modelNames()
+3

All Articles