I know that mongoose.model("myModel")
returns the myModel
model. But how can I get Mongoose to give me an array/object/list of all models, that are currently registered?
I think that somewhat is a design flaw.
The findByIdAndDelete() is a function in Mongoose used to find a document by the _id field and then remove the document from the collection. It returns the document removed or deleted.
By default, Mongoose adds an _id property to your schemas. const schema = new Schema(); schema.path('_id'); // ObjectId { ... } When you create a new document with the automatically added _id property, Mongoose creates a new _id of type ObjectId to your document.
The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ).
Mongoose by default does not create any collection for the model in the database until any documents are created. The createCollection() method is used to create a collection explicitly.
As of now, there is an offcial way: Mongoose#modelNames()
.
You can access an object of all your models via mongoose.models
.
This looks like that:
models:
{ myModel:
{ [Function: model]
modelName: 'myModel',
auth: [Function],
model: [Function: model],
options: undefined,
db: [Object],
schema: [Object],
collection: [Object],
base: [Circular] } },
In my opinion fiddling around with API private stuff is bad, so this is a little bit of a design flaw of Mongoose (See LearnBoost/mongoose#1362).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With