Home > database >  Why mongoose.model() is required to get data from MongoDB?
Why mongoose.model() is required to get data from MongoDB?

Time:06-04

I want to read data from the mongodb with mongoose, but every time it requires creating a model. Why? I thought model are just like templates to insert data to MongoDB. Can anyone describe what exactly mongoose.model() is and how it works?

I tried

const Model = mongoose.model(mongoose.Schima())

Without object in it And it worked as well!!!

How does mongoose.model get data in background?

Thank you...

CodePudding user response:

Mongoose models are much more than just templates on how to store data in the database: they perform type conversion, provide validation, have pre/post hooks, provide easy methods for population, and much more.

You don't need to use a full model to retrieve data from the database (in fact, you don't even need Mongoose at all), but you'll lose all the additional features.

  • Related