I am ing sort of front-end React world and have mostly used statements like import and exports.
I know there are numerous article highlighting How we can use import and export in nodeJS and I also know that this might not have anything to do with import and export.
Either way, I was starting to learn Backend (NodeJs) along with mongoDB.
I am using express framework and package name mongoose.
Inside, models, we were creating a schema as simple as this
const mongoose = require('mongoose')
const bookSchema = new mongoose.Schema({
name: String,
genre: String,
authorID: String
})
module.exports = mongoose.model("Books", bookSchema)
While I understand what we are doing above, creating a schema above and exporting, I am unable to prehend the export statement
module.exports = mongoose.model("Books", bookSchema)
Like, I understand it does export mongoose schema but what does mongoose.model
do/mean? like behind the scenes?
I am ing sort of front-end React world and have mostly used statements like import and exports.
I know there are numerous article highlighting How we can use import and export in nodeJS and I also know that this might not have anything to do with import and export.
Either way, I was starting to learn Backend (NodeJs) along with mongoDB.
I am using express framework and package name mongoose.
Inside, models, we were creating a schema as simple as this
const mongoose = require('mongoose')
const bookSchema = new mongoose.Schema({
name: String,
genre: String,
authorID: String
})
module.exports = mongoose.model("Books", bookSchema)
While I understand what we are doing above, creating a schema above and exporting, I am unable to prehend the export statement
module.exports = mongoose.model("Books", bookSchema)
Like, I understand it does export mongoose schema but what does mongoose.model
do/mean? like behind the scenes?
-
1
It probably means that we are making a model inside our
mongoose Database
which will be named books and would have schema like that ofbookSchema
you are creating. – Alwaysblue Commented Oct 18, 2018 at 7:59 -
3
It will create
books
collection in your database and documents inside that collection will have fields frombookSchema
when you save first document. – Nenad Vracar Commented Oct 18, 2018 at 8:09
1 Answer
Reset to default 9According to the documentation
Models are fancy constructors piled from Schema definitions. An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database.
Schema are the description of the data. Model kind of represent your collection. You can have multiple Model having the same Schema. To create new documents, or to get documents from database, you need to use Model.
To use the model as you described it :
a.js
// ...
module.exports = mongoose.model('Books', bookSchema)
b.js
import Books from 'a.js';
// We create a new document and then save it in database
const book = new Books({
name: 'Harry potter',
genre: 'drama',
authorID: 'JK',
});
// Save is asynchronous and can fail
await book.save();