最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what does module exports = mongoose model do in NodeJs - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question asked Oct 18, 2018 at 7:55 AlwaysblueAlwaysblue 11.9k44 gold badges140 silver badges252 bronze badges 2
  • 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 of bookSchema 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 from bookSchema when you save first document. – Nenad Vracar Commented Oct 18, 2018 at 8:09
Add a ment  | 

1 Answer 1

Reset to default 9

According 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();
发布评论

评论列表(0)

  1. 暂无评论