MongoDB : Open Source, NoSQL Database System

With the advancement of Technology, the demand for data management is keep increasing. Various Business Organizations and Companies try to utilize this data to reach their business goals. The traditional relational database systems will not be much compatible to cater to the rising needs of the modern data architecture. As a solution to this, NoSQL databases such as MongoDB has emerged. In this post, I will discuss the basics of MongoDB and some of its main advantages.

What is MongoDB…?

MongoDB is an open-source, document-oriented database. This uses JSON-like data structures called Documents to store data in Collections (something similar to tables). MongoDB offers flexibility through enabling different data structures among the various documents. This document model maps the objects in your application code thus making easier to deal with data.

History…

MongoDB came to into life thanks to a company called 10gen (currently known as MongoDB Inc.). They began working on MongoDB in 2007 as a component of a planned Platform As A Service product. In 2009 the company started offering commercial support and other services while shifting to open source development model.

Main Technologies behind MongoDB…

MongoDB has been written by using C++, JavaScript, Python and Go languages.

Why is MongoDB popular…?

There are different reasons behind MongoDB’s success as one of the best NoSQL databases available at the moment. Let’s go through some of those features.

  • Scalability : This is considered as one of the main benefits of MongoDB. When you have large amount of data accessed through a server , it clearly affects performance of the application. But MongoDB servers could be scaled out depending on the amount of data handled through the applications, which could improve the performance drastically.
  • Schema-less : Unlike in Relational database models, you do not need to define rigid schema to create tables, but instead uses free data model where there are no restrictions in defining data types. This suits Agile development as well.
  • Flexible data model :  MongoDB models data in JSON-Like documents, which makes managing data much easier.
  • No SQL Injection :  Use of NoSQL databases has eliminated the risk of SQL Injections.
  • Cloud Availability : MongoDB Atlas is a cloud-based database, which can store data of large amounts in cloud servers.

Who are using MongoDB…?

  • City of Chicago
  • Codeacademy
  • Google Search
  • Foursquare
  • Uber
Some other users of MongoDB

Products Offered By MongoDB…

  • MongoDB Atlas :  This delivers the world’s largest cloud-based storage system (Database As A Service) which could run on most famous cloud platforms available (eg : – Amazon Web services, Microsoft Azure, Google Cloud Platform).
  • MongoDB Stitch :  MongoDB stitch is a serverless platform to develop applications.
  • MongoDB Server : It is a Database Server which is a software that could be deployed in a computer. It comes with 2 types.
    • MongoDB Community Server : This is available free and works across Microsoft, IOS and Linux environments.
    • MongoDB Enterprise Server : This comes as a enterprise edition which is much more advanced than the community edition.
  • MongoDB Compass : Serves a GUI to handle MongoDB. This allows to visually explore your data and interact with them in full CRUD functionality.

Terminology in MongoDB…

  • Documents :  It is a structure which stores data in the form JSON. This the corresponding component for a record in a table in RDBMS paradigm.
An example for a document.
  • Collection : Collection represents a similar concept to a table in RDBMS. This contains several documents which is quite similar to a table having several records.
Several documents forms a Collection

Working with MongoDB using Node.js…

There are several ways you can create MongoDB Database and utilize it for data storage, such as using mongo shell. But Node.js comes up with special packages to integrate MongoDB into Node applications. In this section I will show you how to work with MongoDB using Node.js.

Step 1 : Installing MongoDB into your Machine

First you have to visit https://www.mongodb.com/download-center/community to download the community edition of MongoDB.

Downloading MongoDB Community edition

Step 2 : Installing MongoDB package in your Node.js Project

In order to work with MongoDB in your Node.js Application, you have to install the relevant mongodb package into your project. For that, open your Node.js project in Webstorm and go to the terminal and type the following.

Installing mongodb package into your Node.js Project

Step 3 : Creating a Database

Creating a database

In this case, we need MongoClient object from the mongodb to create the database. In the url variable, the number next to localhost: is the port number allocated for MongoDB Server and mydb is the new database name. Then using connect method which accepts the url and a callback function as the parameters, we are going to create a new database.

Step 4 : Creating a Collection

creating a collection

Here, we do not specify the database name in the url. But instead inside the callback function, we retrieve the database as a JavaScript object through db method. Then by using createCollection we pass the collection name and a callback function to create a collection in our database.

Step 5 : Inserting documents to the Collection

Inserting multiple documents into a Collection

Each record (document) in the collection takes form of JSON. Since we are entering multiple documents in one go, we are using insertMany method which accepts an Array of Documents and a callback function.

Note : Even if the collection Inventory1 has not been created previously, during the execution, MongoDB will automatically create that particular collection.

Step 6 : Querying Data from the Collection

Querying data from a collection

Here, we are trying to query a particular document where an item name starts with ‘p’. find method accepts an object which defines the selection criteria and additionally another object which defines the specific fields which needs to be displayed ((_id : 0 and size : 0) means that the primary key of the document and property called size should not be displayed, we have use something called projection to achieve this) and return s a cursor object. This cursor object could be converted into an Array using toArray method.

Note :  $regex is an example for something called query operators which does a particular task in querying data. Few examples for query operators are…

  • $eq :  Matches values that are equal to a specified value.
  • $gt : Matches values that are greater than a specified value.
  • $lt : Matches values that are lesser than a specified value.
  • $in : Matches any of the values specified in an array.

Step 7 : Updating documents in a Collection

Updating documents in a Collection

UpdateMany method is used could be used to update multiple documents. In that method, first argument is taken as the criteria which needs to be selected from the set of documents. Second argument accepts the new values that should be entered into the set of documents after satisfying the given condition ($set operator is used to set new values into the specified fields). In this case, qty and status properties of the documents which has the item ‘notebook’ has to be updated with values 150 and ‘N’ respectively.

Similarly, UpdateOne method could be used to update a single document

Those are some of the basic steps you need to be aware of while working with MongoDB in Node.js environment.

Coming Up Next…

So far we have discussed few interesting topics about MongoDB. In my next article, I will walk you through some of the basic concepts of JavaScript.

Leave a comment