Getting Started with MongoDB


Introduction

As you know Relational Databases held the lead in the industry for decades. It’s because we had limited choices and they were, MySQL, Oracle or MS SQL etc. Still, they are serving a great deed for enterprise applications. But with the technological revolution happening every day, the modern applications required more diversity and scalability when it comes to storing data. So that’s when the NoSQL/Non-relational databases came to the spot like MongoDB. MongoDB is known as a leading NoSQL database which helps to create and deploy a highly scalable and performance-oriented database.

What is MongoDB?


As I mentioned;
  • A NoSQL database
In RDBMS you need to map out everything, and you need to know the exact schema to use. But NoSQL you don’t need any predefined structure of schemas. And have a better scalability and high performance.
  • An open source document database
The data is stored as documents.
  • Uses JSON-like documents with schemas
In MongoDB the documents are created and stored in Binary JSON format, so all JS types of data are supported. In that case, MongoDB is often used with Node.js projects.
  • Written in C++, C, and Javascript

Getting Started with MongoDB

Let’s first install MongoDB.

Step 1:
go to the MongoDB official download center site and go to the community section; https://www.mongodb.com/download-center?ct=1000172319#community
The current stable version is 3.6.5 and the commands are a little bit different than the previous versions. So I’ll be doing this for previous versions.
Download the right file and install it to your ’C:\mongodb\’ directory.

Step 2:
after the installation, create a ‘C:\data\db’ directory in your C Drive. And also create a ‘C:\mongodb\log\mongo.log’ files manually.

Step 3:
open up the command line as an administrator and type following commands;
In 3.2 version you can use just ‘dbpath’ and ‘logpath’ commands but in 3.4 version it was ‘dbpath arg’ and ‘logpath arg’. So if you have any troubles with commands, it will automatically load the commands used in that version or use ‘help’. This command will install the services to run MongoDB.

Step 4:
Then enter;

Step 5(optional):
If the previous command didn’t work properly, create a ‘C:\mongodb\mongodb.conf’ file and enter the following command;
>C:\mongodb\bin\mongod.exe --config C:\mongodb\mongodb.conf --install
If this installed properly, you can enter the;
>net start MongoDB

Step 6:
After the service started successfully to shift into the mongo shell enter;


Now let’s work with databases;


  • To show databases you have, use; >show dbs this will list down the databases you currently have.
  • To create a DB, >use database_name this will not only creating but also switching to that database.
  • You can check it by simply typing >db
You can also create a user for the database with the administration properties as above.

Normally in an RDBMS, after creating the database you need to create tables to add data. But in MongoDB we use collections.
To create a collection;
the name of the collection we going to use is; customers

Insert Data

We are sending the data in JSON format. No need of defining a type like in RDBMS. Just add as you go.



In the first_name: John, I have added an integer: age, an object: address, an array: membership. So as you can see adding data is super flexible compared to
Relational databases.

Also, you might have noticed the ‘_id’ ;
this is something that MongoDB is adding automatically. It’s like the Primary key in RDBMS. Also, auto-incrementing as you add.







Update Data

Let’s use some easy data set;

Let’s add a gender to ‘Steven’;
To update we use $set operator along with the attribute and the value, just like in SQL queries.
Remove Data

This will remove the whole data set of ‘steven’. But you can specify which line should be deleted as another parameter.










Selecting Data

Let’s say that we want to select everyone who has a male gender;

Let’s say that we want to find a city in an address object;

Finding an element in an array;





Compass

So as you can see it’s pretty easy to deal with data in MongoDB. But using commands for that is a little bit uncomfortable. Because you need to always remember what commands to use for different purposes.
That’s why MongoDB is introducing the Compass, as a GUI for MongoDB, MongoDB Compass allows you to make smarter decisions about document structure, querying, indexing, document validation, and more.


  • Host: localhost
  • When you start mogo; it gives the port you are connected to.  Use that as the port number.
  • Authentication: use username/password
  • As for the username/password; enter the username and password you gave when creating the user.
  • Click connect











That's all folks. Hope you enjoyed the article.
Happy Coding!!!

Comments