Skip to main content

MongoDB Tutorial Part One (Introduction)

 



What is MongoDB? 

MongoDB is a cross-platform, document-oriented database with excellent speed, high availability, and automatic scaling. MongoDB is based on the collection and document concepts. It is a NoSQL database. MongoDB is freely accessible under the GNU General Public License, and it is also available from the manufacturer under the Commercial license. MongoDB’s development began in 2007 when the business was developing a platform as a service comparable to Windows Azure. 

 

Benefits of Using MongoDB

  • Full Cloud-Based Application Data Platform 

    MongoDB is not just a database; it is a lot more.  It is a complete application data platform. With MongoDB Atlas, the cloud offered by MongoDB, you have access to a collection of services that all integrate nicely with your database. Amongst other things, you will have: 

    1. The Performance Advisor makes suggestions for improving the performance of your database. 
    2. Atlas Search is a full-text search engine based on the MongoDB Query API. 
    3. MongoDB Charts is a simple tool for creating beautiful dashboards and visualizations. 
    4. Multi-cloud deployment, which is provided out-of-the-box on any major cloud provider. 
  • Flexible Document Schemas 

    One of MongoDB's most appealing advantages is its ability to derive a document-based data model. Because of the way it keeps data in the form of BSON (Binary JSON), ruby hashes, and other formats, it can store a lot of data while also holding arrays and other documents. 

  • Widely Supported and Code Native Access 

    To extract data into Object form for usage in programs, most databases require the use of heavy wrappers such as ORMs (Object-Relational Mappers). MongoDB's decision to store and represent data in a document format implies that you may access it in any language, using native data structures (e.g., dictionaries in Python, objects in JavaScript, Maps in Java, etc.). 

  • Change-Friendly Design 

    If you’re used to having to bring down your site or application to change the structure of your data, you’re in luck: MongoDB is designed for change.  

    We put a lot of work into designing efficient procedures and learning from our failures, but the database is usually a bottleneck. Changing schemas does not need any downtime, and you may start writing new data to MongoDB at any moment without interrupting its operations. 

  • Easy Horizontal Scale-Out with Sharding

    One of its primary features is support for Sharding. Shardingrefers to the technique of storing data across several machines and MongoDB's capacity to handle data as it grows in size. Horizontal scaling is the outcome of this. When Sharding is used, a larger amount of data may be written and read back as the amount of data grows. 

  • High Performance (Speed) 

    MongoDB is a document-oriented database. It is easy to access documents by indexing. Hence, it provides fast query response. The speed of MongoDB is 100 times faster than the relational database. 

  • Simple Installation 

    Creating and configuring a MongoDB cluster is easier than ever using MongoDB Atlas. You may create a new forever-free instance with only a few clicks in the user-friendly interface. You'll be able to connect to your database using the given connection string in minutes. 

    There are a variety of methods to get started with MongoDB on your hardware. The community or enterprise editions can be installed directly on a server. You may also build your own MongoDB container or utilize a pre-built community one. 

  • Cost-Effective 

    MongoDB provides a variety of versatile options. When using MongoDB Atlas in the cloud, you may select the instance size that best suits your needs. You may also configure your cluster to scale automatically as needed. In this manner, you can keep your expenditure low while yet being able to deal with unexpected traffic jams. 

    You may now establish Serverless Databases in addition to the cost flexibility of dedicated clusters. You will only be paid for the actual usage of these databases, making them incredibly flexible and ideal for many low consumptions use cases. 

  • MongoDB has No Joins 

    In RDBMS, connecting two or more tables is challenging as it requires using joins. MongoDB has no complex joins, and changing the document structure in MongoDB is easy, allowing us to connect to other documents with no difficulty.  

  • No Primary Key Set-Up 

    When using RDBMS, setting up a primary key is necessary. In MongoDB, there is no need to explicitly create a primary key. The NoSQL database offers an _id field, created by default, with every document. This created field acts as a primary key. The reserved _id field serves as the primary key in MongoDB, and it must be of unique value. It is important to note that if there is no set up to the _id field, MongoDB fills it with "MongoDB Id Object" automatically.   

  • MongoDB Uses CAP Theorem 

    RDBMS focuses on ACID properties via Atomicity, Consistency, Isolation, and Durability. On the other hand, MongoDB uses CAP theorem Consistency, Availability, and Partition tolerance for database distributions.  

  • Ad-hoc Query Support 

  • Full Technical Support and Documentation 

  • Powerful Querying and Analytics 


Disadvantages of Using MongoDB

  • High Memory Usage 

    MongoDB stores key names for each value pair. Also, due to the lack of functionality of joins, there is data redundancy. This results in increasing unnecessary usage of memory. 

  • Limited Data Size 

    You can have document size, not more than 16MB. 

  • Limited Nesting 

    You cannot perform nesting of documents for more than 100 levels. 

  • Not Support Transaction 

    One of the downsides of MongoDB is that it doesn’t support transactions. Though fewer and fewer applications are requiring transactions, there are still some that need transactions in order to update multiple documents/collections. If that’s a necessary function for your team, MongoDB should not be used. There’s potential for data corruption. 

  • Duplicates 

    Some of these downsides could ultimately lead to duplicate data (as has happened for many users in the past). The relationships in MongoDB are not typically well-defined and the resulting duplicate data sets can be hard to handle. That, along with not being ACID compliant, would lead to corrupted data as well. 

  • Indexing 

    As mentioned earlier, enjoying MongoDB’s quick speeds and high performance is only possible with the right indexes. With shoddily implemented indexes and out of order composite indexes, MongoDB will operate at a shockingly slow speed. 

  • Not support join operation 


Features of MongoDB

  • Ad-hoc Queries for Optimized, Real-Time Analytics 

    It is difficult to predict all of the queries that will be run by end-users when creating a database structure. An ad hoc query is a one-time command that depends on the value of a variable. Depending on the variables in question, the outcome of an ad hoc query may alter each time it is run. 

    When hundreds to millions of factors need to be evaluated, optimizing how ad-hoc queries are handled may make a big impact. MongoDB, a document-oriented, flexible schema database, is the cloud database platform of choice for corporate applications that demand real-time analytics for this reason. The performance gained from ad-hoc query functionality, which allows developers to update ad-hoc queries in real-time, can be game-changing. 

    Field queries, range queries, and regular expression searches are all supported by MongoDB. User-defined functions can be accounted for in queries that return specified fields. Because MongoDB indexes BSON documents and employs the MongoDB Query Language (MQL). 

  • Load Balancing 

    At the end of the day, proper load balancing for developing corporate applications remains one of the holy grails of large-scale database administration. A considerable (and much-appreciated) change in performance may be achieved by properly spreading millions of client requests among hundreds or thousands of servers. MongoDB, fortunately, provides large-scale load balancing via horizontal scaling capabilities like replication and Sharding. With best-in-class concurrency management and locking mechanisms that maintain data consistency, the platform can handle several concurrent read and write requests for the same data. There's no need to employ an external load balancer because MongoDB ensures that every user sees the same data and has a good experience with it. 

  • Sharding

  • Replication for Better Data Availability and Stability 

  • Indexing appropriately for better query executions 

 

Installation

How to Install MongoDB on Windows

Prerequisites

  • The users of Windows must know that their windows desktop has got one of two versions. i.e., 32-bit & 64-bit. 

  • This information could be found in the properties of one’s “My Computer” or “This PC” on their device i.e., either their windows are 32-bit or 64-bit. 


Download the binaries from the MongoDB Download Center. 

  1. Open Windows Explorer/File Explorer. 
  2. Change the directory path to where you downloaded the MongoDB .msi file. By default, this is %HOMEPATH%\Downloads. 
  3. Double-click the .msi file. 
  4. The Windows Installer guides you through the installation process. 
  5. If you choose the Custom installation option, you may specify an installation directory. 
  6. MongoDB does not have any other system dependencies. You can install and run MongoDB from any folder you choose. 

NOTE:
This tutorial assumes that installed MongoDB in C:\Program Files\MongoDB\Server\4.2\.

How to RUN MongoDB on Windows

Step 1: Set up the MongoDB Environment

  • MongoDB requires a data directory to store all data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB. Create this folder by running the following command in a Command Prompt: 

md \data\db

  • You can specify an alternate path for data files using the --dbpath option to mongod.exe, for example: 

    "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath d:\test\mongodb\data

  • If your path includes spaces, enclose the entire path in double quotes, for example: 

    "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath "d:\test\mongo db data"

Step 2: Start MongoDB

  • To start MongoDB, run mongod.exe. For example, from the Command Prompt: 

"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" 

  • This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully. 

  • Depending on the security level of your system, Windows may pop up a Security Alert dialog box about blocking “some features” of  C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe  from communicating on networks. All users should select Private Netwroks, such as my home or work network, and click Allow Access.

Step 3: Verify that MongoDb has started successfully

  • Verify that MongoDB has started successfully by checking the process output for the following line: 

[initandlisten] waiting for connections on port 27017

  • The output should be visible in the terminal or shell window. 

  • You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB. 

Step 4: Connect to MongoDB

  • To connect to MongoDB through the ~bin.mongo.exe shell, open another Command Prompt. 

"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"


That concludes part one of this MongoDB tutorial. We've looked at MongoDB briefly in this blog. If you want to learn more about MongoDB, visit its website, find it on Github or check out this repository of examples.

In the next part of this tutorial, we'll learn more about MongoDB and how to build a CRUD application with MongoDB. Leave a comment below if you have any questions or comments.

Comments

Popular posts from this blog

Spring Boot Tutorial Part One (Introduction)

  What is Spring Boot?  Spring Boot is a fantastic platform for Java developers to create a stand-alone, production-grade spring application that can be started straight away. You may get started with only a few settings rather than a full Spring configuration. The Java Spring Framework (Spring Framework) is a popular open-source enterprise framework for developing standalone, production-grade Java Virtual Machine applications (JVM). Through three essential functionalities, Java Spring Boot (Spring Boot) helps develop a web application and microservices using Spring Framework faster and easier : Autoconfiguration An opinionated approach to configuration The ability to create standalone applications   Benefits of Using Spring Boot Reduce the Development Time Reduce the Time Spent on Development and Increase the Overall Efficiency of the Development Team Help to Autoconfiguration All Components for a Production-grade Spring App Facilitate the Creation and Testing ...

NodeJS Tutorial Part One (Introduction)

  What is NodeJS?  NodeJS is a scalable network application builder that uses an asynchronous event-driven JavaScript engine. NodeJS is a free programming language. It is an open-source Chrome's JavaScript runtime environment, allows you to develop scalable online apps with ease. This environment is built on top of Google Chrome’s JavaScript Engine V8. It uses an event-driven, non-blocking I/O model, which makes it lightweight, efficient, and ideal for data-intensive real-time applications that operate across several devices. NodeJS runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).  Before we go any further, there are two things you should be aware of,  NodeJS is a programming environment, not a programming language or framework.  Contrary to common belief, NodeJS may be used on both the front and backend of a web application.   Benefits of Using NodeJS The ability to scale up quickly In NodeJS, each node is based around an "event." For inst...