Working with MongoDB

Working with MongoDB

04/03/19   7 minutes read     844 Naren Allam

This post is designed for Software Professionals who are willing to learn MongoDB Database in simple and easy steps. It will help understand some basic MongoDB commands.

What is MongoDB?

MongoDB is a rich document-oriented NoSQL database. It is an open source platform written in C++ and has a very easy setup environment. MongoDB provides high performance, high availability, and auto-scaling. It has flexibility with querying and indexing. MongoDB has very rich query language resulting in high performance.

The data objects are stored as separate documents inside a collection — instead of storing the data into the columns and rows of a traditional relational database.

The motivation of the MongoDB language is to implement a data store that provides high performance, high availability, and automatic scaling.

MongoDB uses JSON or BSON documents to store data.

Some of the differences between sql and mongodb terms are shown below.

MongoDB Features

Here, in this part of the MongoDB Tutorial, we discuss some key features of MongoDB:

i. Ad-hoc Queries

    MongoDB supports ad-hoc queries by indexing.

ii. Schema-Less Database

    It is very flexible than structured databases. There is no need to type mapping.

iii Document Oriented

    It is document oriented, JSON like a database.

iv. Indexing

    Any document can index with primary and secondary indices.

v. Replication

    It has this powerful tool. Every document has one primary node which further has two or more secondary replications.

vi. Aggregation

    For efficient usability, MongoDB has aggregation framework for batch processing.

vii. GridFS

    It has grid file system, so it can use to store files in multiple machines.

viii. Sharding

    For the larger data sets sharding is the best feature. It distributes larger data to multiple machines.

ix. High Performance

    Indexes support faster queries leading to high performance.

Basic Installation

1. In Ubuntu, first we install MongoDB by running the below command in the console

BASH  Copy
                      $ sudo apt-get install mongodb 

    To install in Mac, use command

BASH  Copy
                      $ brew install mongodb 

2. After installation, to check the status use command

BASH  Copy
                      $ sudo systemctl status mongodb

3. If the service is not up, to start the service use commands listed below and check status again

BASH  Copy
                      $ sudo systemctl start mongodb

$ sudo systemctl enable mongodb

4. To check that MongoDB has been started on port 27017 with the netstat command.

BASH  Copy
                      $ sudo apt install net-tools

$ netstat -plntu

5. To stop Mongodb service, use command

BASH  Copy
                      $ sudo systemctl stop mongodb

6. Now to open mongo shell, use command

7. This will connect you to running MongoDB instance. To come out of mongo, run the following command given below (or) press "ctrl+c"

BASH  Copy
                      $ exit

Basic Commands related to Mongo Shell

First open mongo shell, using the command "mongo" in the console. Then follow the steps given below.

1. To see all available databases

BASH  Copy
                       > show dbs


    Hi sir


    Hi sir