Introduction about Node.js and MongoDB

NoSQL databases are databases that store and retrieve the data that is present in a non-tabular format. Depending on the format of the data stored, NoSQL databases are split into 4 major types:

  • Key-Value
  • Graph database
  • Document-oriented
  • Column family

These databases are built to address the shortcomings of traditional RDBMS and provide high performance, availability and increased scaling (horizontally), while also handling data which constantly changes it’s schema over time. One of the most popular NoSQL databases which are available today is the MongoDB.
MongoDB is a scalable, high-performance, open-source, document-oriented NoSQL database, which was developed by 10gen in the year 2007. It is written in C++ and it supports a variety of APIs in many programming languages.

Key features of MongoDB:

  • Full index support for high performance
  • Horizontally scalable and fault tolerant (distributed data storage/sharding)
  • Rich document based queries for easy readability
  • Replication and failover for high availability
  • Map/Reduce for aggregation
  • Supports Master-Slave replication
  • No joins nor transactions
  • No rigid schema, which makes it dynamic
  • Data represented in JSON / BSON

When to use MongoDB?

MongoDB can be used in places that require simple queries, easy and fast integration of data and have data whose structure changes constantly with time.

Examples:

  • E-commerce websites
  • Mobile applications
  • Blogs and content management portals
  • Storing geospatial data

At the same time, since it doesn’t support transactions, it can’t be used in highly transactional systems.

SQL vs MongoDB

The components used in MySQL and MongoDB have different terminologies, but similar functions.



SQL MongoDB
Database Database
Table Collection
Row Document
Column Field
Index Index

Installing MongoDB (on windows):

Go to the following link (https://www.mongodb.com/download-center/community) and download the MongoDB Community Server.


From here, download the MSI file, and install MongoDB in your computer.

After the installation is complete, open the MongoDB Compass Community application, and select the connect option.


A local instance of MongoDB will now be running on the local host, with the port number 27017.

NodeJS and MongoDB:

Node.js, the open source JavaScript server environment, has the ability to connect to both SQL and NoSQL databases such as MySQL and MongoDB (respectively). In order to use these databases, the required modules need to be downloaded and installed by using the Node Package Manager (npm).

For using the MongoDB, the Mongoose module needs to be installed.