# MongoDB - Overview

In this blog, we are going to discuss about  **NoSQL** Fundamentals, MongoDB basics and Advanced concepts

<iframe src="https://giphy.com/embed/MYCyhNfiO4VVbv2jqN" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>




### Intro - NoSQL

NoSQL can be defined as a database that is employed for managing the massive collection of unstructured data when your data is not piled up in a tabular format or relations like that of relational databases. The term NoSQL came from the word non-SQL or nonrelational. There are a wide variety of existing Relational Databases that have been unsuccessful in solving several complex modern problems such as:

- A dynamic change in the nature of data - i.e., nowadays data are in structured, semi-structured, nonstructured as well as polymorphic in type.

- The variety of applications and the type of data feed into them for analysis has now become more diverse and distributed and is approaching cloud-oriented.

- Also, modern applications and services are serving tens of thousands of users in diverse geo-locations, having various time zones. So data integrity needs to be there at all the times.

<iframe src="https://giphy.com/embed/KGr9wMij9rZt7nHwMx" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>


### Types of NoSQL Database

- Document-type databases
- Key-Value stores
- Graph stores
- Wide-column stores

### Benefits of NoSQL

- High elastic Scalability
- Valuable and reliable for big data
- Reduced dependence/trust in in-house DBAs
- It's Easy to understand
- Agile data models

## MongoDB - Fundamentals

Mongo DB can be defined as a document-oriented database system that uses the concept of NoSQL. It also provides high availability, and high performance, along with automatic scaling. 

** Database  : **  In MongoDB, a database can be defined as a physical container for data collection.

** Collection  : ** Collections can be defined as a cluster of MongoDB documents that exist within a single database.

** Document  : ** A document can be defined as a collection of key-value pairs that contain dynamic schema.


<iframe src="https://giphy.com/embed/RLo8tJquqMKVUSW1Wi" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>


Okay. The theory part is over. Let's get into hands-on part


<iframe src="https://giphy.com/embed/HMpcgxAZs5YYHsbe8P" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>



## MongoDB Installation

To get started with MongoDB, you have to install it in your system. You need to find and download the latest version of MongoDB, which will be compatible with your computer system. 
You can use this
 [http://www.mongodb.org/downloads](http://www.mongodb.org/downloads)





## MongoDB - Basics

### Create Database


```
use cloudnloud_db;

``` 



### Drop Database


```
db.dropDatabase();

``` 


### show Database


```
show dbs;

``` 



### Create Collection


```
db.createCollection("developers");

``` 


### Drop Collection


```
db.developers.drop();

``` 


### Insert Documents

** insert() **


```
db.collection_Name.insert(JSON document)

``` 

** insertOne()**


```
db.developers.insertOne({ _id: 1, name: "Veera" , skills : "blockchain"})
``` 

** insertMany() **

```
db.developers.insertMany(
[
  { _id: 2, name: "Kanna" , skills : "blockchain"},
  { _id: 3, name: "Karthik" , skills : "cybersecurity"},
 { _id: 4, name: "Deepika" , skills : "Node js"},
{ _id: 5, name: "Aadhi" , skills : "python"},
]
)
``` 




### Query Documents


```
db.developers.find()

db.developers.find().pretty()
``` 



** Filtering in Queries **



```
db.developers.find( { skills: "blockchain"} )


db.developers.find({$or: [ { skills: "blockchain" }, { name: "karthik" } ] } )


db.developers.find( { "skills": { $in: [ "blockchain", "python" ] } } )

``` 



### Update Documents


```

db.developers.update(

{_id: 4},
{
  $set: { instrument: 'blockchain' }
}
)
``` 


### Delete Documents


db.collection.deleteOne()


```
db.developers.deleteOne( { name: { $in: [ "Veera", "Kanna"] } } )

``` 


db.collection.remove()


```
db.developers.remove( {} )

``` 


db.collection.deleteMany()


```
db.developers.deleteMany( { name: { $in: [ "Veera", "Kanna" ] } } )

``` 



### Aggregation

MongoDB offers three different ways of performing aggregation:

- The aggregation pipeline.
- The map-reduce function.
- Single purpose aggregation methods



```
db.developers.aggregate([{$group : {_id: "$skills", TotalRecords: {$sum : 1}}}])
``` 



- ** $sum - ** adds up the definite values of every document of a collection.
- ** $avg - **	computes the average values of every document of a collection.
- ** $min	- ** finds and returns the minimum of all values from within a collection.
- ** $max-**	finds and returns the maximum of all values from within a collection.
- ** $push - **	feeds in the values to an array in the associated document.
- ** $first - **	fetches out the first document.
- ** $last -**	fetches out the last document.
- ** $addToSet- **	feeds in the values to an array without duplication.



### Sorting


```
db.techSubjects.find({}, {"topic":1, _id:0}).sort({"topic":1})
``` 


Hope you can understand the basic commands in MongoDB

<iframe src="https://giphy.com/embed/3NUJ4FwSp4MJG" width="342" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>



let's move on Advanced concepts

<iframe src="https://giphy.com/embed/13ZHjidRzoi7n2" width="466" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>


## MongoDB - Advance


### Indexing

- Databases having indexes make queries performance more efficient. 

- When you have a collection with thousands of documents and no indexing is done, your query will keep on finding certain documents sequentially.



** Create an Index in MongoDB **


```
db.collection_name.createIndex( {KEY:1, KEY:2} )

db.Users.createIndex({"PhoneNo":1})

``` 



### Advance Indexing


** Indexing Fields for Sub-Documents **


```
db.users.ensureIndex( {"location.city":1,"location.country":1} )


db.users.find({"location.city":"Los Angeles"})

``` 




### DB Replication



```

mongod --port "PORT" --dbpath "your_db_complete_path" --replSet "instanceName_of_Replica_set"


mongod --port 27017 --dbpath " C:\Program Files\MongoDB\Server\4.0\data" --replSet rs0

``` 


### DB Export


** Export to json ** 


```
mongoexport --db cloudnloud_db --collection developers --out /developers.json
``` 



** Export to CSV **


```
mongoexport --db cloudnloud_db --collection developers -type = csv  --out /developers.csv
``` 






### DB Backup and Restore


** mongodump **

For creating a backup of your database in MongoDB, make use of the command mongodump.


```
mongodump -d <database name> -o <backup-folder>

mongodump -d cloudnloud_db -o /backup
``` 



** mongostore **

For restoring all your backup data in your server, MongoDB provides the command mongorestore. 


```
mongorestore -d <database name> --dir <backup-folder>

mongorestore -d cloudnloud_db --dir /backup
``` 




### Regular EXperession


** Use $regex Operator**


```
db.developers.find( {name : {$regex: "vee" }} )

``` 


** Case Insensitive ** 


```
db.developers.find( {name : {$regex : "vee", $options:"$i"} })

``` 

that's guys. Hope you can get better insights about MongoDB

<iframe src="https://giphy.com/embed/iBdKJ6iIubtRK" width="476" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>



** Keep Learning & Keep Growing **



## 🍯 Contributors:
- [Karthikeyan S](https://www.linkedin.com/in/herbie36/)
- [Karuppasamy PKN](https://www.linkedin.com/in/ksamypkn/)
- [Deepika Karunakaran](https://www.linkedin.com/in/deepi19/)
- [Veera solaiyappan](https://www.linkedin.com/in/veera26/)
- [Aadhityaa SB](https://www.linkedin.com/in/aadhi06/)


## Community and Social Footprints :

- [GitHub](https://github.com/cloudnloud)
- [Twitter](https://twitter.com/cloudnloud)
- [YouTube Cloud DevOps Free Trainings](https://www.youtube.com/c/CloudnLoud)
- [Linkedin Page](https://www.linkedin.com/company/cloudnloud/)
- [Linkedin Group](https://www.linkedin.com/groups/9124202/)
- [Discord Channel](https://discord.com/invite/vbjRQGVhuF)
- [Dev](https://dev.to/cloudnloud)
