It is recommended to study according to the mongoose documentation. (Or search mongoose related technology blogs and learn to use google)
Let me give you a brief answer:
Why do we need to define data types
A major advantage of mongodb is that it is schemaless, that is, there is no need to preset data structures. And why does mongoose need to define data types? Mongodb's schemaless has great advantages in today's projects with rapidly changing requirements, and mongoose's schema-based model is not a step back, it is just for the convenience of development. You can modify the schema at any time, and in most cases you can run it directly without reorganizing the database. With a schema that describes data types, you can operate the mongodb database more easily. (Also including the advantages of oop) mongoose has built-in schema-based advantageous functions such as type conversion, default value, etc.
mongoose.model('Book', BookSchema)What is it for
Register BookSchema with mongoose on the Book model. So you can use mongoose.model('Book') to take out this model later. mongoose has the concepts of schema, model, and document.
Whydb.book.find({})can’t find data
Please use db.books.find({})db.books.find({}) mongoose会自己搞复数形式,显得比较专业,毕竟是collection嘛。 而且story还会复数成stories呢。 你在use blog之后,可以通过show collectionsmongoose can make plural forms by itself, which looks more professional. After all, it is a collection.
And story will also be pluralized into stories.
After use blog, you can know which collections exist through show collections.
Off topic
It is recommended that the questioner study the documentation (blog tutorial) before asking questions. 🎜mongodb, mongoose. 🎜
It is recommended to study according to the mongoose documentation.
(Or search mongoose related technology blogs and learn to use google)
Let me give you a brief answer:
Why do we need to define data types
A major advantage of mongodb is that it is schemaless, that is, there is no need to preset data structures. And why does mongoose need to define data types?
Mongodb's schemaless has great advantages in today's projects with rapidly changing requirements, and mongoose's schema-based model is not a step back, it is just for the convenience of development.
You can modify the schema at any time, and in most cases you can run it directly without reorganizing the database.
With a schema that describes data types, you can operate the mongodb database more easily. (Also including the advantages of oop)
mongoose has built-in schema-based advantageous functions such as type conversion, default value, etc.
mongoose.model('Book', BookSchema)
What is it forRegister BookSchema with mongoose on the Book model.
So you can use
mongoose.model('Book')
to take out this model later.mongoose has the concepts of schema, model, and document.
Why
db.book.find({})
can’t find dataPlease use
And story will also be pluralized into stories.db.books.find({})
db.books.find({})
mongoose会自己搞复数形式,显得比较专业,毕竟是collection嘛。
而且story还会复数成stories呢。
你在
use blog
之后,可以通过show collections
mongoose can make plural forms by itself, which looks more professional. After all, it is a collection.After
use blog
, you can know which collections exist throughshow collections
.Off topic
It is recommended that the questioner study the documentation (blog tutorial) before asking questions. 🎜mongodb, mongoose. 🎜