How to create a MongoDB database

So we’ve got a Node.js website ready and waiting for some database data. We’ve also got MongoDB installed. So let’s create a database and put some data into it.

Step 1: don’t create a database

Hang on Simon, are you sure you’ve got that right? Yep! There is no create database command in MongoDB. If you come from a traditional stack, like me, this is quite a weird concept. Surely the first thing to do is create the actual file and define a schema? Not so with MongoDB. With MongoDB you create a database the first time you save data into it.

Let’s try that out.

When we installed MongoDB we tested the installation by displaying a list of the databases on the machine. So let’s drop into the Mongo shell and do that. A couple of terminal commands (don’t type the ‘$’ or the ‘>’ symbols, here ‘$’ indicates a terminal prompt and ‘>’ a Mongo prompt):

$ mongo > show dbs

You should see something like this get returned:
List of MongoDB databases

Step 2: “Use” the database you wish to create

As we mentioned earlier, rather than explicitly creating a database, MongoDB will create a database when it first has data saved to. In order to save data, we need to connect to our new database even though it doesn’t exist yet. Sounds odd, but it’s really easy. Simply enter this command into the Mongo shell:

use euro2012

In this instance we are going to use a database called “euro2012″.

If you use the show dbs command again, you will see that the database hasn’t been created yet.

List of MongoDB databases

Step 3: Insert some data into the MongoDB database

Here’s another difference of MongoDB from traditional relational databases: there are no tables, rows or columns. In their place, MongoDB uses collections and objects. Think of collections as tables, and objects as table rows. The main difference is that we’re not stuck into a rigid column structure.

Here’s how the MongoDB guys explain it themselves (source):

  • database holds a set of collections
  • collection holds a set of documents
  • document is a set of fields
  • field is a key-value pair
  • key is a name (string)
  • value is a - basic type like string, integer, float, timestamp, binary, etc.,
  • a document, or
  • an array of values

You can explicitly create collections, but they can also be created the first time you use them. We’ll do the latter here. In the Mongo shell enter the following:{country:"England",GroupName:"D"})

That will create a new collection called “teams”, and save an object with two fields “country” and “GroupName”. Crucially, this will now also save our database. Check that it has been created by running show dbs again.

List of MongoDB databases

You should now see that our new database has been added to the list.

Now add the rest of the data using the following command in the Mongo shell:{country:"France",GroupName:"D"}){country:"Sweden",GroupName:"D"}){country:"Ukraine",GroupName:"D"})

Step 3: testing by reading the data back out.

Having put some data in, and tested that the database has been saved, we should now verify that the data we entered has been saved as we expected it to be.

Run a simple “find” on the collection to return all objects by entering this into the Mongo shell:


This should return you something like this:
collection.find() command in MongoDB

Perfect! We’ve created a MongoDB database and saved our information into it :)

Next up, we’ll take a look at how to use this data in our site, by connecting MongoDB to Node.js.