MongoDB is one of the most sought-after NoSQL databases. The open-source cloud platform has helped thousands of developers and data scientists manage large volumes of data with JSON documents and flexible schemas.
And according to VentureBeat’s cloud revenue growth report, MongoDB is a significant contributor to overall cloud revenue.
So if you haven’t learned how to build a database on MongoDB yet, you’re missing out. The company recorded 39,100 customers in its latest quarterly report, as covered by CNBC.
Fortunately, you can soon be one of them once you are done with this tutorial. Read on to find out how to create a database in MongoDB. For clarity purposes, this guide will focus on MongoDB Atlas UI.
Create an Account
First, you’ll need to register an account on MongoDB Atlas with either your email address, Google account, or GitHub account. You’ll also be prompted to read the terms and service when doing so. Don’t worry; signing up is free!
Deploy a Cluster
After logging in, you will be prompted to build a cluster. In MongoDB’s terms, a cluster is a replica set. Atlas offers free clusters (M0) as a small-scale development environment to host your data. They’re free and provide access to basic Atlas features.
If you choose to deploy M0 clusters, you’ll have to pick your cloud provider and region. M0 clusters are supported on Amazon Web Services, Microsoft Azure, and Google Cloud Platform.
Enter a name for your cluster and then click “Create Cluster”.
Create a Database User
To access your cluster, you’ll need to create a database user. Go to Connect > Databases > Databases Deployment and click “Connect” on the cluster. This will prompt you to add your IP address.
Once IP address is added, you can set a name and password. Fill these in, then click “Create Database User.”
Connect to Your Cluster
You can connect to your cluster in various ways. For this tutorial, we will cover how to connect via MongoDB Shell (mongosh). You will need to have it installed on your machine before you start.
Once installed, navigate to Connect > Databases > Database Deployments and then click “Connect.” Then click “Choose a connection method” and “Connect with the MongoDB Shell.” Select your version of MongoDB Shell from the dropdown menu, then copy and pase the provided connection string into your terminal.
Insert and View a Document
Run the command below on mongosh. This will create a database called “gettingStarted” and will point your environment to it.
use gettingStarted
Next, insert a document to your database using the following command. This will create a new collection called “People” and insert a document there.
db.people.insertOne({
name: { first: 'Alan', last: 'Turing' },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ 'Turing machine', 'Turing test', 'Turingery' ],
views : Long(1250000)
})
You will receive confirmation once you’ve successfully inserted a document into the collection.
Load Sample Data
For this step, we can use the Atlas UI again. Just navigate to Database Deployments > Database. Then open the “Load Sample Dataset” dialog.
Go to your database and click the (…) button. Then click “Load Sample Dataset.” You can view your sample data by clicking the database deployment’s “Browse Collections” button.
Conclusion
That’s it! You successfully created a database in MongoDB. The steps will vary depending on the drivers you use.
If you’re interested, MongoDB University launched a new curriculum to help close the tech skills gap. It includes new courses and certifications, plus multiple Atlas labs that provide hands-on access to the product.
For more database-related tutorials, check out how to log WordPress database queries to file.
Leave a Reply