Skip to main content

How to get started with MongoDB

In my last post I gave you all a brief introduction about NoSQL databases and its uses. As I mentioned in my last post , in this blogpost I'll give you all an overview of MongoDB and how to get started using MongoDB.


Basically MongoDB is a document-oriented, NoSQL database and it can be rated as the most popular and widely used NoSQL database. MongoDB is free and open source , therefore anyone can simply download it from their site and start using it in their applications. It stores data as JSON(JavaScript Object Notation) like documents with schemas. In fact, data is actually stored in MongoDB as BSON objects. BSON is very much similar to JSON except that it is the binary representation of JSON. Unlike JSON, BSON is optimized for performance and navigational abilities. Typically data will be stored in the format shown on the following picture.

 Key features that had contributed to the huge popularity of MongoDB are as follows:
  • Support Ad hoc queries together JavaScript functions
  • Allows indexing with secondary and primary indices
  • Provide high availability with replica sets
  • Load balancing - MongoDB can run over multiple servers balancing the load or duplicating data to keep the system up and running in case of any failure
  • Has an in-built file system called Grid File System.
In the previous blog I told you all that currently developers are working on improving the transaction support feature of NoSQL databases. What  about MongoDB ? Does it support transactions? The happy news is MongoDB Inc. have announced recently that the latest version of MongoDB (MongoDB 4.0)  which is yet to be released will support multi document transactions and will guarantee ACID(Atomicity, Consistency, Integrity, Durability) data integrity. A stable release of this version will be soon available , probably by this summer. 

Now lets start looking at how to work with MongoDB. (Note that in the following steps I'll be using the currently available stable version of MongoDB, which is MongoDB 3.6.4).

Step 1 : Download MongoDB .(Click here)
This will display the following window. Choose the correct version according to the OS you are using.


Step 2 : Install MongoDB to your local machine.
Run the downloaded file and install MongoDB to your PC. By default it will be installed in the path C:\Program Files\MongoDB\Server\3.6 , however you can change this path if  select "Custom" installation option. During the installation process , it will ask you to install 'MongoDB Compass' , in addition to MongoDB server. MongoDB Compass provides a GUI for manipulating data sets in MongoDB.

Step 3 : Set up the MongoDB environment 
MongoDB requires a data directory to store all its data. The default path of this directory will be /data/db in the drive you are starting the MongoDB instance. For an example , if you are starting the DB instance from a project folder inside D drive , then there should be a /data/db directory inside the D drive. Without this data directory MongoDB will not start successfully. 

Step 4 : Start MongoDB
You can simply start MongoDB by double clicking on the mongod.exe file inside the bin directory of folder in which MongoDB is installed. In my case, I have installed MongoDB to my D drive so the path to the mongod.exe file is : "D:\MongoDB\Server\3.6\bin". Double clicking on mongod.exe file will open up a command prompt as follows;

  
The last line 'waiting for connections on port 27017' , indicates that your MongoDB has started successfully.

Step 5 : Creating a database and tables
There are various GUI applications to do this easily. But, I'll show you how to create a DB and tables, more accurately in this case a collection using mongo shell. To start mongo shell , move into the bin directory of MongoDB and open a command prompt. Then type 'mongo'.
Here are some useful commands:
show dbs - shows all databases  currently available
use <database_name> - creates a new database
db.createCollection("<collection_name>") - creates a new collection 
show collections - display all collections inside a specific database
There are several more commands to insert, update and delete documents from a collection. The image below shows how I have executed some of the commands.


Above steps are for starting MongoDB instance in your local machine. Alternatively you can create an account in MongoDB Atlas, which a cloud provider that provides MongoDB as a service. You can deploy, operate and scale a MongoDB database in just few clicks using MongoDB Atlas , without installing any software to your PC. You can then use MongoDB Compass to connect to the clusters created in MongoDB Atlas. In fact, this the most trendiest way of using MongoDB as in the future everything tend towards cloud technologies.

Comments

Popular posts from this blog

A comparison between Node and Express

If you are programmer who have worked a long time with JavaScript you might know that , earlier JavaScript was just a scripting language used in the client side. However now the story is different. With many libraries like Node js and Express js being added to the JavaScript family JavaScript have evolved beyond, just being a scripting language in the client side. With these new libraries being added, now it is possible to build entire sites on JavaScript, extending it from front to back-end seamlessly. So what is Node and Express? Let's  clear this out step by step! What is Node js? Node js is an open source JavaScript runtime environment that executes JavaScript code server side. Node enables developers to use JavaScript for server side scripting. Node uses an event driven, non-blocking I/O model that makes it light weight and efficient. Because of this model Node js is particularly useful in web application that have many input/output operations and for real time web appli...

Difference between callback functions and promises in javascript

If you have ever worked with JavaScript you might have the experience that it is a quite confusing language to understand at first. This is because  in JavaScript there are many ways to do the same thing, and also unlike most other programming languages, if you do a small mistake in JavaScript instead of giving any errors, the program would behave in a totally different way than what you expect. Therefore, without understanding the basic concepts of JavaScript, it's quite difficult to work with it. In this post I'll be explaining about callback functions and promises, what they are used for and the difference between the two methods. Actually callback functions and promises are 2 different ways of doing the same thing. By default JavaScript is an asynchronous programming language. Which means that JavaScript does not execute instructions line after the other as in many programming languages. Whenever JavaScript come across an operation that takes relatively a long time, typic...