Getting Started with MongoDB

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@superoo7·
0.000 HBD
Getting Started with MongoDB
#### What Will I Learn?

- You will learn what is NoSQL and MongoDB.
- You will learn what are the difference between MongoDB and SQL.
- You will learn basic API for Creating Table & Collections, Find Data in Table, and Dropping Tables & Collections

#### Requirements

- Basic Understanding on how database works.
- Basic JavaScript
- Installed MongoDB

#### Difficulty
Intermediate


#### Tutorial Contents

In this tutorial, I will explain how NoSQL different from SQL and all you need to get started with MongoDB.

# NoSQL
NoSQL is a non-relational database management system which different from normal Relational Database Management System (RDBMS).

## Feature
- Store large volumes of structured, semi-structure and unstructured data.
- NoSQL has high performance, horizontal scaling, and high availability.
- Dynamic Schemas
- Easy to maintain and Fail-Prove

## Type of NoSQL
- Key-value Stores
	- Redis
	- Dynamo
	- Riak
- Column Oriented
	- Cassandra
- Graph
	- Neo4j
- Document Oriented
	- MongoDB
	- CouchDB

## Why NoSQL?

Big Data are (Unstructured Data) semi-structure and unpredictable in nature. Since RDBMS are useful for structure data, These data are not effective to be handle by typical RDBMS

# MongoDB
MongoDB is one of the most popular Document Oriented NoSQL. MongoDB is free and open-source and is available in most of the operating system (Cross Platform for Linux, OS X, Windows & Solaris) \[[source](https://db-engines.com/en/system/Couchbase%3BMongoDB)\]

<center>
![mongodb.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515916447/zujtzzilu5lhmaom9xyp.png)
[Source](https://blog.serverdensity.com/mongodb/)
</center>

MongoDB is high performance, high availability, automatic and scalability. The data can be access easily with document-based search which leads to high-performance.

You can imagine MongoDB as a "big" version of Javascript Object Notation (JSON), because they use Binary JSON (BSON), a binary version of JSON which provides a better performance than JSON.

## Comparison MongoDB and SQL
SQL|MongoDB
|:----:|:----:|
Tables|Collections
Rows|Documents*

\*__Documents__ can be easily correspond to native data types in many programming languages.


e.g.

Language|Data Type
|:---:|:---:|
Ruby|Hash Map
Javascript|Object
Python|Dictionary

## Why MongoDB than SQL?

- Embedded documents and arrays reduced the need of expensive join in SQL

- Dynamic query can be done with MongoDB using document based query language

- Conversion of objects to DB objects is no needed.

- High performance result of using internal memory to store working set in order for quick data access.

## Install MongoDB
Always check the [doc for latest installation guide](https://docs.mongodb.com/manual/installation/)


# Basic Query in MongoDB
[MongoDB Manual](https://docs.mongodb.com/manual/contents/)

## Create Database
- Start MongoDB with `mongod` depends on Windows or OS x.

- Connect to DB with command `mongo`

- Switch to database with `use test`

- Then create a collection with `db.<collection>.insert({<data>})`

e.g.
```
 db.users.insert({ 
 	name: "superoo7",
 	age: 22,
 	location: "Malaysia",
 	skills: ["javascript", "sql", "electronics"]
 })
```

## Search for data
- Search data with `.find()`

```
db.users.find()
```

> By default, if no value pass in, it will query out all the data in the collection

> add `.pretty()` to beautify the output. (e.g. `db.users.find().pretty()
`)

## Create Documents and save to Mongo
Creating a variable in mongo the syntax are really like javascript

```
var userInfo = {};
userInfo.name = "sueproo7";
userInfo.age = 22;
userInfo.location = "Malaysia";
userInfo.skills = ["javascript", "sql", "electronics"];
userInfo.metaData = {};
userInfo.metaData.createdOn = new Date();
db.users.save(userInfo);
```

> The data in the table can be found via `db.users.find().pretty()`


## Drop Database Tables
- use `show dbs` command to see all the listed database
- In order to drop a database, we need to `use` that database with the command `use <database_name>`
- Run `db.dropDatabase()` to drop the database.
- To check, simply just run `show dbs` again.


## Create Collections
- Run `db.createCollection('users');` to create a collection
- To check the result, simply just run `show collection;`

## Drop Collection
- Check all collections with `show collection;`
- To drop a collection, simply just run `db.users.drop();`
- To check the result, simply just run `show collection;`


You can read more about MongoDB API at [mongodb.com](https://docs.mongodb.com/manual/contents/)
    
This is the end of the tutorial. In the next tutorial, I will explain how to use mongoose with Node.js, Mongoose.js is a schema based solution to model your data in mongoDB, which helps to connect Node,js with MongoDB with ease.

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@superoo7/getting-started-with-mongodb">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , ,