All you need to know about Mongoose
Tags: node, mongodb, beginners, mongoose
Published: 2021-06-15 10:29:12

* MnongoDB provides it's native driver to work with our project at server side but it may be large amount of code or repetition in code but it's not the case with **mongooses.** In this article I am going to tell you how to get started with `mongoose`
<br>

* Mongoose is a **Node.js** package that gives you an interface to play with mongo database. It is very **light weight** `npm` package to use in our application. Mongoose has all sets of methods that help you to connect and access data stored in Mongo database.
<br>

* **Mongoose** is an Object Data Modeling `(ODM)` library for MongoDB and Node.js. Mongoose.js provides an abstraction layer on top of MongoDB that eliminates the need to use named collections in native MongoDB driver. Mongoose makes MongoDB `easier to work` with MongoDB. Mongoose allow developers to enforce a specific **schema** of desired object at the application layer.


### What Mongoose Do ?
* provides schema validation
* It manages relationships between data
* make MongoDB easy to use 

## Content of this blog
[1. Installing and Requiring Mongoose] (#1)
[2. Connecting To MongoDB database] (#2)
[3. Defining Schema] (#3)
[4. Creating Model] (#4)
[5. Creating and Saving Instance] (#5)
[6. Reading from DB] (#6)
[7. Updating] (#7)
[8. Deleting] (#8)

## (1). Installing andRequiring Mongoose <a name='1'></a>

* install Mongoose npm package in your project directory as dependency using below shown command in terminal.

`npm install mongoose --save`

* Require mongoose in your application using this syntax 
![require](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hoaw3p58eik9abyul93v.png)<hr>

## (2). Connecting To MongoDB database <a name='2'></a>

* Syntex
`mongoose.connect(<Database URI>);`
Using connection URI of database we cane connect to database.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1aypz2ya2z75gspzs3q6.png)

* here test is **Database name**
* 
* You can get connection Instance by `const db = mongoose.connect;`
db object is used to listen the events attached to it.
![connection methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nr70mbpmsx2dpnxdl0a.png)
We can use any function method. so don't get confused😕 by seeing it.<hr>

## (3). Defining Schema <a name='3'></a>
* schema is a way to describe **structure of documents ** in database.

#### Schema accepts only the following Datatype.
* String
* Number
* Date
* Boolean
* Buffer
* ObjectId
* Mixed
* Array
![Schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ziiwvgom2n3jhjl35bkv.png)
In above example some Schema validator are used.<hr>

## (4). Creating Model <a name='4'></a>
*  Mongoose model provides an interface to the database for creating, querying, updating, deleting etc...
* Model is one type of definition of database collection using `collection name` and `schema name`.
* Collection Name must be in **singular** form
![model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hylpzymyxybjw17wtun3.png)
<hr>

## (5). Creating and Saving Instance <a name='5'></a>
* We can save object in database using `save()` method on model.
![save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btoff0f0eeu0ckjvgqkv.png)<hr>

## (6). Reading from DB <a name='6'></a>
* We can reed data from database using `find()` on model and also find document by it's id using `findById()` 
![fInd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjurcdash93nb93e4xkw.png)<hr>

## (7). Updating <a name='7'></a>

![update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gua5luvqx0t9nsezkqt.png)<hr>

## (8). Deleting <a name='8'></a>
![delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqm94f652w9qtsyunu0y.png)<hr>

### For more knowledge checkout my `Github 👁`
{% github bhavinvirani/bhavinvirani%}