- Get Started with MongoDB
- MongoDB For Beginners
- Table of Contents
- Introduction to MongoDB
- Installing MongoDB
- How do I know if MongoDB is running?
- Get Started with MongoDB
- MongoDB Basics
- Creating your first MongoDB collection
- Filtering records in MongoDB
- Common MongoDB commands MongoDB Aggregations
- Best Practice Considerations in MongoDB
Get Started with MongoDB
MongoDB is a general-purpose, feature-rich, and powerful document-based NoSQL database that provides efficient and flexible storage for a variety of different types of data sets.
MongoDB For Beginners
Table of Contents
Introduction to MongoDB
The best way to start understanding MongoDB basics is to grasp the following comparison:
MongoDB | Traditional SQL |
---|---|
Database | Database |
Collection | Table |
Document | Row |
$lookup | Join |
Index | Index |
ACID Transaction | ACID transaction |
Single Machine | Multiple Nodes |
In a traditional relational SQL database (i.e., MySQL, PostgreSQL), tables are used for defining the structure of a data set and rows for the actual data, while in MongoDB, we have collections and text documents containing name-value pairs, respectively. In other words, in a relational database, tables are related by keys, and in MongoDB, collections contain unstructured and often arbitrary data.
Indexes, a fundamental feature for efficient query execution, are also supported in MongoDB on any field or subfield of the documents, and they are defined at the collection level.
A collection contains a number of documents, and a document contains data fields. The data fields can be of an arbitrary number and type, even within the same collection. Effectively, each MongoDB document follows the Javascript Object Notation (JSON) format. An example of a document is:
MongoDB can handle dynamic data models and schema that can easily store unstructured data sets; and in the case of variable data loads, MongoDB can scale horizontally, which is extremely cost-efficient when compared to traditional SQL databases, which can usually only scale vertically.
Installing MongoDB
There are a few ways to install MongoDB. The most effective way to achieve this is to use the MongoDB Database-as-a-Service offering called MongoDB Atlas. This is a service that you can access using your favorite web browser; it can be installed on all three major public cloud providers (Amazon Web Services, Microsoft Azure, Google Cloud Platform), comes with a free tier, and with just a few clicks, you will have provisioned a fully functioning MongoDB database.
To start the setup on MongoDB Atlas, all you need to do is to create an account.
Once you do, you need to create a project:
And then, create the actual MongoDB Cluster:
Select your preferred cloud provider, supply a name for your cluster, and click Create Cluster:
After the cluster is created, let’s configure the security options. The two things we need to configure are the IP Whitelist addresses and a database user.
For the IP whitelist, click on “Add your Current IP Address” and then select a username/password that you will remember. On the following page, choose the “Connect with the mongo shell” option as this is what we’ll use for the rest of our tutorial. If you haven’t installed the mongo shell yet, follow these instructions:
To connect to the MongoDB cluster, you can also use the official GUI client for MongoDB called Compass. You can download MongoDB Compass from the MongoDB downloads page. Once you’ve installed the application, you can connect to the MongoDB Atlas cluster by supplying the hostname from the connection string from the above window in addition to the username and password. Once you open Compass, click on ‘’Fill in connection details individually” and then “Connect”:
Alternatively, you could download and install the MongoDB Community Edition on your workstation or get the Enterprise Edition. If this is your first time trying out MongoDB, then it is highly recommended to use MongoDB Atlas due to its ease of use in quickly provisioning a free cluster.
How do I know if MongoDB is running?
If you have used MongoDB Atlas, you can easily inspect the status of your cluster from the Clusters home page:
If you have installed MongoDB Community Edition on a personal workstation with Windows, MacOS, or Linux, you need to check whether the mongod process is running on localhost, respective to your OS-specific commands.
Get Started with MongoDB
Once you have a MongoDB database provisioned for you from MongoDB Atlas, you are ready to start exploring MongoDB’s capabilities.
Depending on your preferences, you have a few options for how to interact with the database that you just created:
- Through the official cross-platform graphical user interface for MongoDB, Compass.
- Through your web browser and the MongoDB Atlas web user interface. This interface is very similar to MongoDB Compass.
- Through a command line tool — the mongo shell.
- Through a programming language, API, or an IDE (i.e., VS Code extension, Python mongodb driver, node.js mongodb driver, Java mongodb driver).
For explanatory tasks, it is a good idea to work with the MongoDB Atlas web UI, but if your intentions are to automate document loads onto MongoDB, then switching to a programming language is going to be a better option since it will give you greater control.
If you are new to MongoDB, then using MongoDB Atlas is the simplest solution since it doesn’t require you to download any extra drivers or set up connection strings.
MongoDB Basics
Creating your first MongoDB collection
As discussed above, once you pick your preferred interaction option, it is very easy to get started in creating your first MongoDB collection. If you are using MongoDB Atlas, then the GUI will guide you to do so. If, for example, you opted for using the mongo shell, creating a collection is as easy as opening a command line, connecting to mongo, and executing the following command: \
Then, to insert your very first document, create the JSON document on the fly using the insertOne command. From the same command line, run:
MongoDB will automatically store documents in binary format, which greatly speeds up the retrieval and access times; this format is called BSON (Binary JSON).
In addition, for every collection, MongoDB reserves a field called _id , also known as the id field . This field is the primary key for any collection and uniquely identifies a document within any collection. When you insert a document in a collection and don’t specify an _id, MongoDB will fill that in for you with Mongo ObjectId value. Alternatively, you can specify custom primary keys with one or more values of your choice.
Filtering records in MongoDB
It is possible to filter specific records from a MongoDB collection. MongoDB supports all the common filter operators like numerical and string querying comparisons. For example:
In this query, we are filtering records from the inventory collection to return only documents when their qty data field is greater than 20, using the $gt comparison query operator. Similarly:
In this query, we are also filtering records from the same collection to return only documents when their qty data field is equal to 5 or 15, using the $in comparison query operator.
Common MongoDB commands MongoDB Aggregations
There are a plethora of actions that can be performed on a MongoDB collection. MongoDB supports common query and projection operators. For example, in the following query, we are making use of three different operators:
In this query, we are filtering records from the inventory collection. We are using the logical operator $and to only get documents that have a price data field ($exists) and that are equal ($eq) to 2.00.
MongoDB supports update operators as well. As its name suggests, these are useful if you want to perform updates to text documents. For example, you can update a document in the products collection with the $set like this:
The above query assumes that you already have a document in the products collection with an _id: 100 and a quantity field:
< _id: 100, sku: "abc123", quantity: 250, instock: true, reorder: false, details: < model: "14Q2", make: "xyz" >, tags: [ "apparel", "clothing" ], ratings: [ < by: "ijk", rating: 4 >] >
Imagine that you own a department store and want to capture the sales per item per month. This is an aggregation that can be configured to occur automatically upon insertion of new data in a configured MongoDB collection. Moreover, MongoDB Atlas offers you the capability to add explanatory visualizations using MongoDB Charts, including adding bar charts, line charts, or even geospatial graphs like choropleths, scatter plots, or heat maps.
The MongoDB tutorials page and MongoDB University are both very good places to get started and apply all of the above. Plus, you’ll learn a lot more exciting things that can help you move forward for you or your company.
Best Practice Considerations in MongoDB
When you have a MongoDB workload, there are a few recommendations to allow a MongoDB database to work in the most efficient way. As a general rule of thumb, it is always a good idea to configure indexes for key collections. If your queries are using specific fields, then the MongoDB engine won’t have to go through every document to fetch the results. Insteadm it will know where to look based on the defined index and greatly decrease the retrieval time. This can be very useful for collections that are big in size or are expected to grow in the future.
Furthermore, two more considerations to start thinking about from early on in a MongoDB database are replica sets and sharding. If one of these features is enabled, then MongoDB is also commonly called as a MongoDB cluster:
- Replica sets: A replica set is when MongoDB creates copies of the data and stores them in other servers. This feature provides a safety net in case that the main server of a MongoDB database fails or when maintenance takes place; it switches to the other server to serve the data for the end users. This is a seamless operation; by default in the free tier of MongoDB Atlas, a replica set of 3 servers is automatically configured.
- Sharding: This is another feature that benefits from having multiple machines. Sharding a MongoDB database is a synonym for horizontal scaling. Sharding is the distribution of collection documents in multiple instances, or “shards,” and is very useful when data growth is expected.