Installing MongoDB: A Step-by-Step Guide
Learn how to install MongoDB, a powerful document database, on your local machine or in the cloud. Discover the key differences between SQL and document databases, and decide whether a local or cloud-based installation is best suited for your needs. This guide will provide you with the necessary instructions and insights to get started with MongoDB.
MongoDB installation
MongoDB is a document database that can be installed locally or hosted in the cloud.
SQL vs Document Databases
SQL databases are relational, storing data in tables and using joins to retrieve related data. MongoDB, a non-relational (document) database, stores data in flexible documents, allowing related data to be stored together. This approach can improve read performance and simplifies data management.
Local vs Cloud Database
MongoDB can be installed locally on your hardware or used through a cloud service. The local installation requires managing server maintenance yourself. Alternatively, MongoDB Atlas offers a cloud-based solution that simplifies database management.
For this course, we'll use MongoDB Atlas. Sign up for a free account to access a MongoDB database.
Creating a Cluster
- After creating your account, set up a free "Shared Cluster". Choose your preferred cloud provider and region.
- Under "Database Access", create a new user and note the username and password.
- Under "Network Access", add your IP address to allow access from your computer.
Install MongoDB Shell (mongosh)
To connect to your MongoDB database, start by installing the MongoDB Shell, mongosh
. Follow the official installation instructions for your operating system.
Verify the installation by running:
mongosh --version
The version used in this tutorial is v1.3.1.
Connect to the Database
To connect to your database:
- In the MongoDB Atlas dashboard, under "Databases", click the "Connect" button for your Cluster.
- Choose "Connect with the MongoDB Shell".
- Copy your connection string. It should look similar to:
- Paste the connection string into your terminal and press enter.
- Enter the database user password you created earlier when prompted.
mongosh "mongodb+srv://cluster0.ex4ht.mongodb.net/myFirstDatabase" --apiVersion 1 --username YOUR_USER_NAME
You are now connected to your MongoDB database!