How To Create A Document Database In MongoDB

MongoDB is a popular NoSQL document-oriented database that allows developers to store and retrieve data in a flexible and scalable manner.

Document databases are designed to handle unstructured and semi-structured data, making them a great choice for applications that require flexibility in data modeling.

That’s why it’s no surprise that MongoDB’s revenue increased by 36% in the last quarter, according to Futurum Research.

What’s more, MongoDB offers its databases in the cloud, sparing users the responsibility of managing and maintaining them. This lets developers focus on more important tasks.

In this article, we’ll walk you through the process of creating a document database in MongoDB.

We’ll cover the essentials, from creating an account on MongoDB to inserting and querying documents in your database.

By the end, you’ll have a solid understanding of how to create and manage a document database in MongoDB.

1. Register An Account On MongoDB Atlas

MongoDB Atlas is a fully-managed cloud database service offered by MongoDB. With this, you can easily deploy, manage, and scale MongoDB databases in the cloud.

pexels christina morillo 1181354

The first step is to create an account with MongoDB Atlas. You can register for free, with no credit card required. Once registered, you can easily set up your document database.

2. Deploy A Cluster

If you’re logging in for the first time, you will be shown options for creating a cluster. Click the M0 option to deploy a free cluster.

Related:  Breaking Barriers: Mastering Effective Communication In Software Outsourcing

You will be prompted to select your provider and region. Once this is taken care of, type in a name for your cluster. Lastly, click “Create.”

3. Add Your IP Address

For security reasons, Atlas requires users to connect to clusters with only whitelisted IP addresses.

To add your IP address, click on “Database” on the Atlas dashboard (top-left corner), then select “Database Deployments.”

From here, click “Connect,” and Atlas will pull up the Setup connection security step. Lastly, add your current IP address to complete this step.

4. Create A Database User

As part of its security measures, Atlas will also only allow verified users to access your databases. They are separate from Atlas users.

To add a verified user, visit “Database Deployments” from the “Database” menu. Click “Connect,” go through the security setup, then set the new user’s username and password.

5. Connect To Your Cluster

MongoDB Atlas allows you to connect to a cluster using a variety of drivers: mongosh, Node.js, PyMongo, or Compass.

The steps may vary per driver, but the general process to follow is to download your preferred driver and open your terminal.

For this demonstration, let’s use the PyMongo driver. After installing the PyMongo driver, connect to your database again and select “Choose a connection method.”

Choose your driver (and version) from the dropdown menu. A connection string will be displayed for you to copy.

Replace <password> with the password you set for your database user, then replace <myFirstDatabase> with the name you typed for your database.

hosting gcc650f2d2 1280

You’ll need to Import MongoClient from PyMongo to connect to a running MongoDB instance. Run this command in the Python shell on your terminal to do this:

Related:  How Fonts Used On A Website Leave Impression On Visitors
from pymongo import MongoClient

Finally, you can connect to your cluster. In your Python shell, paste your connection string into this command:

client = MongoClient(‘<your updated connection string>’)

6. Insert A Document

To create a database on your new cluster with PyMongo, run the following command:

db = client.gettingStarted

You can replace “gettingStarted” with whatever name you want for your new database. Next, create a collection by running this command:

people = db.people

This will create a collection called “people”. Now, you can insert a document into this collection. Here’s how to create a document:

import datetimepersonDocument = {  “name”: { “first”: “Alan”, “last”: “Turing” },  “birth”: datetime.datetime(1912, 6, 23),  “death”: datetime.datetime(1954, 6, 7),  “contribs”: [ “Turing machine”, “Turing test”, “Turingery” ],  “views”: 1250000}

Now that a document is created, you can insert it into the collection. Run this command to do this:

people.insert_one(personDocument)

Congratulations! You’ve successfully created a document database.

Conclusion

Because of the flexibility and scalability of document databases, they are used in projects where data structures and volumes are dynamic and require a flexible schema.

Some popular use cases include content management, real-time analytics, and mobile and IoT applications. They are also used in e-commerce sites like eBay.

MongoDB is just one of the cloud examples companies can explore to streamline their processes.

We’ve got more articles discussing the advantages of the cloud and cloud computing services, so don’t forget to check them out and discover what they can do for your business.

The following two tabs change content below.
Jonathon Spire

Jonathon Spire

Tech Blogger at Jonathon Spire

My diverse background started with my computer science degree, and later progressed to building laptops and accessories. And now, for the last 7 years, I have been a social media marketing specialist and business growth consultant.

Leave a Comment

Jonathon Spire

I blog about a range of tech topics.

For the last 7 years I have been a social media marketing specialist and business growth consultant, so I write about those the most.

Full transparency: I do review a lot of services and I try to do it as objectively as possible; I give honest feedback and only promote services I believe truly work (for which I may or may not receive a commission) – if you are a service owner and you think I have made a mistake then please let me know in the comments section.

– Jon