After reading this tutorial you'll have a basic knowledge about MongoDB and will know how to create a free MongoDB instance using MDB GO.
If you have even little experience with backend technologies you may have noticed that there is a lot of options when it comes to data storage. There is a whole variety of database systems and each of them has its strong sides. It's not possible to explain all of them in a single article - it would probably take a few books to do so. Instead, we will focus on one specific database here - MongoDB. Let's dive in!
What's so special about MongoDB? Well, it's one of the most popular NoSQL databases out there. In case you are wondering what NoSQL is, here's a short explanation for you:
NoSQL database is a type of database that provides non-tabular way of storing data. It refers to non-relational databases which means that you can store data in other way than tables with relations (typical for MySQL database for instance).
MongoDB is document-oriented. The data is stored in JSON-like documents. A document is a key-value structure capable of storing any data scheme. It's a very convenient concept as it allows you to represent real data very accurately.
Documents are stored in collections. They fulfill a similar role as tables in relational databases but with a very important difference. Collections do not enforce any common scheme for the documents stored in a given collection. It means that you can have documents with different keys and values (including number of keys and the document structure in general) stored in the same collection. Quite convenient.
Let's see how to create an example MongoDB instance using MDB GO.
Before you create the MongoDB database you need to have MDB CLI installed. If you don't have it yet go to the installation guide and do so. It is a very useful tool that will allow you to create the database with a single command.
If you already have our CLI tool you can create your first MDB GO hosted MongoDB instance. First, make sure you are logged in:
$ mdb login
Next, run the following command:
$ mdb database init -db mongodb
You'll be informed that before you create your database you need to create a database user. Hit Enter.
Now you need to provide your user data and then the database details. Please provide the values of your choice. For example:
? Enter username peter
? Enter password Parker_1
? Repeat password Parker_1
? Enter database name spider_shop
? Enter description Test database
Note: the password must contain at least one uppercase letter, one lowercase letter, one number, one special symbol and have minimum length of 7.
Hit Enter and that's it. You just created your own database. Simple, isn't it?
If you provided correct data you should now see something similar to this:
Warning! Write down the password to your database as we will never show it again.
Info: To connect to this database you need to download Robo3T or another MongoDB client
┌─────────┬─────────────────┬────────────┬───────────────────────┬────────────────────────────────────────────────────────────────────────────────┐
│ (index) │ Username │ Password │ Database Name │ Connection String │
├─────────┼─────────────────┼────────────┼───────────────────────┼────────────────────────────────────────────────────────────────────────────────┤
│ 0 │ 'peter68c6924a' │ 'Parker_1' │ 'spider_shopf60cfe72' │ 'mongodb://peter68c6924a:Parker_1@mongo.db.mdbgo.com:8604/spider_shopf60cfe72' │
└─────────┴─────────────────┴────────────┴───────────────────────┴────────────────────────────────────────────────────────────────────────────────┘
These are your credentials that you will need to use to connect to your newly created database. It's described in the next section.
Before you jump there however, please notice that the username and database name slightly differs from what you provided earlier. Don't worry - it's OK. A few random characters have been added to these values to randomize them and improve security of your database credentials.
Important: Do not close your terminal window until you save your credentials somewhere. This is the only time we will show you your database password. If you don't save it you'll lose it.
Now let's see how to connect to your database.
In order to connect to your MongoDB instance we will use the client named NoSQLBooster for MongoDB. You can download and install it from the official website.
After you downloaded and installed NoSQLBooster let's open the client and click Connect (1) | Create (2) in the top-left corner of the application window.
Now find the From URI (3) button in the bottom-left part of the window and click it.
In the text input that popped up paste your Connection String that's been displayed
by MDB CLI after your database has been created. In my case it's this one:
mongodb://peter68c6924a:Parker_1@mongo.db.mdbgo.com:8604/spider_shopf60cfe72
Click the Test Connection button in the bottom-left corner of the window. If you followed all the steps you should see a bunch of text and a green OK at the top of the window.
Congratulations! You have configured your connection successfully. You can click Close | OK and then Save and you will be automatically connected to the database.
You can now start using your MongoDB instance. Let's see a few examples.
If you connected to your database you should be able to see available collections in the
left-hand-side tree view. In our case, you should see the
hello
collection - double-click it. In the tab on the right there is a single
document with a key message
and the value "Hello, world!"
. Let's
see what else you can do with your test collection.
Let's run some queries. We will be working in the query editor now. Please note the green arrow button at the top bar of the app window (marked with the red border) - we will be using it to run queries:
In order to read documents you can use the .find()
method. It accepts an
argument where you can provide query options. In order to find all the documents that have
the key message
with value "Hello, world!"
you can use the
following query:
db.getCollection("hello").find({ message: "Hello, world!" })
After you run it with the green arrow button you'll see that it shows only one document - the one that you saw earlier. It's hard to tell if the query works if the result is the same as earlier. So let's create more documents to be able to play with querying.
Inserting a document requires using the .insert()
method. It accepts an
argument which is a JSON-like document that you wan't to insert. For example to add a few
more documents to the hello
collection you can run the following queries:
db.getCollection("hello").insert({ message: "I love spiders!" })
db.getCollection("hello").insert({ message: "I don't feel so good :(" })
db.getCollection("hello").insert({ suitType: "Iron-Spider" })
db.getCollection("hello").insert({ suitType: "standard" })
Note that you can insert different documents to the same collection. The
suitType
key does not match with the message
key but MongoDB
allows you to do that.
Now if you run the .find()
method with different options you'll see how it
works.
If you want to update a document you can use the .update()
method. You need
to provide at least two arguments: first the query which will retrieve the documents that
needs to be updated (the same as in .find()
) and second argument is a
JSON-like object that will provide new values to the document. For example:
db.getCollection("hello").update({ message: "I love spiders!" }, { message: "I hate spiders." })
This query will change the message
value to "I hate spiders."
.
Note that you can add new keys to the document using
.update()
method. Simply pass the new keys in the second argument.
In order to delete documents you can use one of the two methods:
.deleteOne()
or .deleteMany()
. Both of them accept an argument
with query options the same as in .find()
. The difference is that the former
will delete only one document even if more will match the query while the latter will
delete all matching documents. Here's an example:
db.getCollection("hello").deleteOne({ suitType: "Iron-Spider" })
Using this query you will delete the first document that matches given criteria.