Skip to main content
In this guide, we introduce MongoDB, a high-performance, scalable NoSQL document database. You will learn what MongoDB is, how to install and configure a MongoDB server, and how to perform basic operations and troubleshooting. MongoDB stores data in JSON-like documents gathered into collections, and multiple collections form a database. A single MongoDB server can host multiple databases. There are two main editions available: the free Community Edition and the commercial Enterprise Edition. Below are examples of JSON documents demonstrating how data can be structured in MongoDB:
MongoDB is available as a managed cloud service through MongoDB Atlas and as an on-premises server. This article focuses on installing a local (on-premises) MongoDB server.
The image is an informational slide about MongoDB, describing it as a scalable document database available in cloud and server versions.
The Community Edition is free and widely used for development and prototyping. The Enterprise Edition offers additional features and support for commercial deployments.

Installing MongoDB

The first step is configuring your package management system with the MongoDB repository and installing the mongodb-org package. Although best practices for user creation and security are available in the official documentation, this guide uses a simplified approach. For instance, on a system using yum, install MongoDB with:
Create a repository file (e.g., /etc/yum.repos.d/mongodb-org-4.2.repo) with the content below to configure the MongoDB repository:
The image shows a MongoDB installation interface, offering options to download the Community Server, select version, OS, and package, with additional resources like release notes.

Starting the MongoDB Service

After installation, start the MongoDB system service named mongod and verify its status. The main log file is located at /var/log/mongodb/mongod.log. Start MongoDB using:
Then check the service status:
A sample output may appear as follows:
When MongoDB starts, the log file at /var/log/mongodb/mongod.log provides details about the server startup, including the MongoDB version, and indicates that it is listening on port 27017 of the loopback IP address (127.0.0.1). This default setting permits connections only from the local system—a secure default for development that should be adjusted for production environments. A sample log snippet is shown below:

MongoDB Configuration

The MongoDB configuration file (/etc/mongod.conf) allows you to modify settings such as port number, storage location, and network bind addresses. Here’s an example configuration:
By default, MongoDB’s access control is not enabled. For production environments, ensure you enable authentication and implement proper security measures.

Interacting with MongoDB Using the mongo Shell

To interact with your MongoDB server, use the mongo shell, which provides a JavaScript interface. Launch the shell with:
You should see output similar to this:
After connecting to the shell, you can explore and manage your databases:
  • List all databases:
  • Create or switch to a database:
  • Confirm the current database:
  • Create a new collection:
  • Insert a document into the collection:
  • Retrieve data from the collection:
  • Filter documents using query parameters:

Conclusion

This introduction to MongoDB covered the installation of a local MongoDB instance, basic configuration, and common database operations using the mongo shell. Practice these steps to build a solid foundation, then continue exploring advanced MongoDB features and capabilities. Happy coding! For further information and in-depth tutorials, check out the MongoDB Documentation.

Watch Video

Practice Lab