MongoDB: Introduction

Part 2: MERN STACK Series

Deep Shah
TheLeanProgrammer
4 min readApr 19, 2021

--

Welcome back to the MERN STACK series.

In the first part, we learnt about some terms of MERN STACK.

First part link:🔗https://deepshah1309.medium.com/part1-mern-stack-series-deep-shah-6cb9fa248bc6

Now in this blog, I am going to explain MongoDb in detail.

What is MongoDB?😎

  1. Open Source
  2. Document oriented Database
  3. No SQL
  4. Does not require RDBMS(relational database management system)

5.Stores the data in JSON format(JSON-javascript object notation)

MongoDB VS MYSQL

MYSQL: It uses structured query language to access stored data .in this format schemas are used for DB structure and also utilizing tables a way for standardizing datatypes. It uses RDBMS.these relations help to structure the data and also organize the relations between tables. It is being used by many database-driven applications.

MongoDB: It’s the less-restrictive format and higher performance make it a better choice, particularly when availability and speed are primary concerns.

MongoDB COMPASS: It is a GUI(Graphical user interface) for MONGODB.it is also maintained by MongoDB itself.it allows you for smart decisions based on the document structures with great visualization.

download MongoDB compass:

🔗https://www.mongodb.com/try/download/compass

MongoDB ATLAS:

It is a fully managed cloud database developed by MongoDB.it handles complexity, management,and deployment on the Cloud service providers(Azure,GCP, AWS)

SignIn to MongoDb atlas:

🔗:https://www.mongodb.com/cloud/atlas

Fig 1 below shows the clear conceptual way to understand the structure

Fig 1

What is Mongoose?

Mongoose is an object modeling tool to work in an asynchronous environment.it provides schema-based solutions.it includes query building, validation, etc. It supports both promises and callbacks.

In our Project structure, we will be having two folders (Client, Server)

On the client-side, we will be designing awesome, flexible UI components by ReactJs.

On the server-side, we will be managing requests, database models, Database CRUD operations, middlewares etc.(Will be covered in detail in the project structure part blog)

let’s say we have a server directory in which we have package.json,index.js and node_modules(directory)

in this directory run the following command

it installs the package mongoose.

Let’s connect the database😎.

in the index.js file

mongoose model() function:

this function of the mongoose module is used to create a collection of a particular database.

Example:

Mongoose find() function:

It is used to find particular documents in the collection.

it takes 3 args(arguments)=>

  1. Condition
  2. Query Projection
  3. general query options like=>(limit,skip …)

let’s understand it with an example

Let’s see an example with all arguments:

mongoose Update() function:

It is used to update one document in the database without returning it.

mongoose updateOne() function:

4 parameters

(filter,update,options,callback)

mongoose updateMany() function:

it will update all the documents which matches the filter argument

We have now reached the end of the blog !

In this blog we learnt mongodb intro,mongodb vs mysql,mongodb compass,mongodb atlas,mongoose package,mongoose connection,mongoose schema creation,mongoose model creation(collection+schema),mongoose save method to add the document to collection,update method,updateOne,updateMany methods etc.

Stay tuned for upcoming Blogs😎

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

Deep Shah
TheLeanProgrammer

Student of Computer science(B.E) from MSU,vadodara.Enthusiast about FULL stack development|Web development|Java enthusiast|problem solving