Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does module exports = mongoose model do in NodeJs

I am coming 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 comprehend 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?

like image 554
iRohitBhatia Avatar asked Oct 18 '18 07:10

iRohitBhatia


People also ask

What is the purpose of module export in node JS?

The main purpose of module. exports is to achieve modular programming. Modular programming refers to separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.

How module exports work in node JS?

The module. exports is a special object which is included in every JavaScript file in the Node. js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module.

What does Mongoose model do?

A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What does Mongoose do in node JS?

Mongoose is a Node. js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as SQLAlchemy for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.


1 Answers

According to the documentation

Models are fancy constructors compiled 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();
like image 142
Orelsanpls Avatar answered Sep 25 '22 08:09

Orelsanpls