Table of Contents
Table of Contents
MongoDB is an open-source, document-oriented database designed to efficiently handle large-scale data. Unlike traditional relational databases that use tables for storage and retrieval, MongoDB stores data in JSON-like documents, making it a NoSQL (Not only SQL) database. Developed and managed by MongoDB, Inc. under the Server Side Public License (SSPL), MongoDB was first released in February 2009.
MongoDB supports official drivers for many popular programming languages, including C, C++, C#, .NET, Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift, and Mongoid. This allows developers to build applications in their preferred language. Many prominent companies, such as Facebook, Nokia, eBay, Adobe, and Google, use MongoDB to manage their large data volumes.
Overall, MongoDB is known for its ability to create fast, scalable applications by storing data in documents formatted in key-value pairs within JSON. This approach offers a flexible and efficient way to manage vast amounts of data across various platforms.
Let’s explore what happens behind the scenes in MongoDB. As a database server, MongoDB allows you to create and manage multiple databases. Within each database, data is stored in collections, which are similar to tables in a MySQL database. Each collection contains multiple documents, which are the actual units of data storage in MongoDB. Unlike relational databases, MongoDB is schema-less, meaning that documents within a collection don’t need to have the same structure.
Documents in MongoDB are composed of fields, which are key-value pairs akin to columns in a relational database. The values in these fields can be of various BSON data types, such as double, string, or boolean. BSON, which stands for Binary JSON, is the binary representation of JSON documents used by MongoDB for more efficient storage and querying.
One of the key advantages of MongoDB is its ability to store nested data within documents. This nesting allows for the creation of complex data relationships within a single document, making data retrieval more efficient compared to SQL databases, which often require complex joins to combine data from multiple tables. The maximum size for a BSON document is 16MB.
For instance, consider a database named GeeksforGeeks. Within this database, there could be multiple collections, and each collection can contain multiple documents. Each document stores data in the form of fields, creating a flexible and efficient structure for data storage and retrieval. This approach simplifies data management and enhances performance, especially for applications requiring rapid access to large volumes of data.
Before doing anything, update your system packages with the help of the following command:
sudo apt update
Install the required utility packages by entering the following command:
sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release
First, you need to include the MongoDB proprietary package repository in APT resources. To do this, enter the public key of GPG MongoDB with reference to the latest release version of its stable version running the following command:
curl -fsSL https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
MongoDB’s APT queries in the sources.list.d directory to download and install the sources of the target packages, and now you need to configure it. Before doing so, create a file called mongodb-org-5.0.list:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Next, you need to update your Ubuntu system again. This allows the APT package manager to know where to fetch the MongoDB package for download and installation. To do this run the following command:
sudo apt update
Now it’s time to install MongoDB on your system. You can do this by entering the following command:
sudo apt install mongodb-org -y
You can install a specific version of MongoDB using the following command:
sudo apt-get install -y mongodb-org= mongodb-org-database= mongodb-org-server= mongodb-org-shell= mongodb-org-mongos= mongodb-org-tools=
Once the download is complete, start the MongoDB service using the following command:
sudo systemctl start mongod
You can check MongoDB status with the following command:
sudo systemctl status mongod
Now enable the MongoDB database with the following command. This will keep the MongoDB database running successfully even after the system reboots:
sudo systemctl enable mongod
You can test the connection to the MongoDB database by entering the following command:
mongo --eval 'db.runCommand({connectionStatus: 1})'
Enter the following command to access the MongoDB shell:
mongo
Finally, you can access any database-related objective.
MongoDB provides a flexible, scalable, and high-performance solution for modern data management needs. Its document-oriented approach and extensive feature set make it an excellent choice for both new and existing applications. With advanced capabilities and straightforward setup, MongoDB is well-equipped to handle a wide range of data management scenarios..