Getting Started with MongoDB and dbKoda.

Learning MongoDB the easy way, with a little help.

Michael Harrison
dbKoda
7 min readDec 28, 2017

--

If you’ve stumbled across this blog, then you probably have a pretty good idea what MongoDB is already, but no introductory tutorial would be complete without a synopsis you may have read a hundred times before. The most important things to know about MongoDB are:

  • MongoDB is a document database, meaning each record and it’s associated data are contained as a document, often with nested subdocuments. In MongoDB these documents are self-contained, independent units of semi-structured data in JSON form.
  • MongoDB is a schema-free database, meaning that the schemas do not follow the formal relational rules of third normal form. Indeed there is no fixed schema of any type (though you can apply a JSON schema rule)
  • MongoDB is designed to be a distributed database, but that doesn’t mean it has to be.
  • MongoDB is completely free and open source.
  • All the cool kids are doing Mongo.

In this blog, we’ll set up MongoDB (the easy way), put some data into our instance, then search through that data in the most basic way possible. If you want to learn more about exactly what MongoDB is, you can read a lot more in the official documentation.

Setting up MongoDB

For the sake of keeping this tutorial (relatively) short, I will be providing examples for macOS. The steps on Linux will be very similar, however some parts of the process on Windows are slightly different. The full steps for installing MongoDB on Windows can be found here.

Because no getting started article is complete without a shameless plug, I’ll be using dbKoda as my Mongo Development Environment, like MongoDB, dbKoda is a completely free and open source IDE designed from the ground up to help with MongoDB development. You can download dbKoda here, or don’t, but it might hurt my feelings.

Open dbKoda, and trigger a local terminal window by right clicking in the Profiles Panel (Example in the gif below). Otherwise you can use your terminal application of choice for this part.

Embedded Terminals in IDEs are all the rage.

Once you have a terminal open, run the following commands to install MongoDB via Brew:

brew update
brew install mongodb

Once that is complete you will want to create the directory that MongoDB will store it’s data files, you can specify this when starting the server, but it’s easier to use the default location while you’re still learning.

mkdir -p /data/db
// Make sure you have the right permissions on this directory.
sudo chown -R `id -un` /data/db

Now that all the boring stuff is done, we can finally get to the good stuff, for now we’ll be running MongoDB in the simplest way, and that’s using the Mongo Daemon or mongod.

// There are heaps of parameters for this, but for now we'll run it //  on all defaults, in part two of this tutorial we'll explore the //  various configurations you can enable.
mongod

The mongod process will spit out a lot of pretty complicated looking logs, but at the end you should see something like this:

2017-12-04T21:58:08.873+1100 I NETWORK  [thread1] waiting for connections on port 27017

Now we’re ready to connect! Create a new profile using the new profile wizard in the top left of the application.

Opening a new Connection in dbKoda

Putting something into MongoDB

Now that we have a connection established, let’s get some sample data in there. Luckily for us dbKoda has a wizard for importing data in just a few clicks, you can get the sample data and an example of an import in this blog post but for now we’ll do it the old-fashioned way.

First we will select what Database we want to use, you may be thinking “But I haven’t created one yet!”, that’s okay, the use command will create the database if it doesn’t already exist. Here’s a gif of adding and executing some code in dbKoda, you can use the hotkey Ctrl + Space to open the code completion menu as in the example, we’ll step through this gif one part at a time.

Executing some simple commands

Put the following commands into the dbKoda editor window.

// Set our database.
use myNewDatabase;
// This will change the value of the db variable.
db; // Outputs "myNewDatabase"

Press the execute all button or use the hotkey (F8). Now that we’ve selected our database, we’ll want to create a collection, a collection in MongoDB is like a Table in a traditional Database system, it contains our documents. Just like the Database, if we try to insert into a Collection that doesn’t yet exist it will be created for us.

db.myNewCollection.insertOne({Name: 'Michael', Profession:'Code Monkey', Age: 24});

You can see in the above code snippet that inserting a document is similar to inserting a row using SQL, the main difference being that we are passing JSON documents into the function. You may also have noticed at this point that we have not specifically created a Database, a Collection, or a Schema, these things are created on demand. We can see this by adding a new document that doesn’t match our previous document at all.

db.myNewCollection.insertOne({Company: 'dbKoda', Year_Founded: 1993});

Now we have two documents with different schemas in the same collection, something you couldn’t do in other traditional Databases. After running one of these commands you’ll see a message from MongoDB letting you know it has been added successfully. Let’s remove these two documents to keep the Collection nice and clean.

db.myNewCollection.remove({Name: 'Michael'}); // Removes any documents where the key Name equals Michael.
db.myNewCollection.remove({Company: 'dbKoda'}); // Removes any documents where the key Company equals dbKoda
db.myNewCollection.remove({}); // Removes all documents.

Adding a meaningful amount of data this way would take far too long, so instead we’ll use Javascript. In the following code we’re using .insert([{}]) instead of .insertOne({}), there is also .insertMany([{}]) the main difference between these commands is the number of documents that can be inserted and the output you receive back from the database.

var myDocumentArrayOne = [];
for(var count=0; count < 10000; count++) {
myDocumentArrayOne.push({Name: 'Michael_'+count, Job: 'Code_Monkey', Count: count});
}
db.myNewCollection.insert(myDocumentArrayOne);
var myDocumentArrayTwo = [];
for(var count=0; count < 10000; count++) {
myDocumentArrayTwo.push({Name: 'Guy'+count, Job: 'Fearless Leader', Count: count});
}
db.myNewCollection.insert(myDocumentArrayTwo);

Getting something out of MongoDB

There’s not much use having all this data in our Collection if we can’t have a look at it, so let’s start looking! First let’s do it the easy way using dbKoda, then we’ll try it the traditional way in the shell, using our Explorer Panel in the bottom left, refresh our tree to see the new database, then view our collection as a table using the right click menu (example below):

Viewing a simple Table with the Table View.

From here you can also right click individual documents and inspect them, this is particularly useful if you have very large, complex and nested documents like the ones in the example below:

Now I’ll demonstrate the traditional way to get data out of MongoDB using the shell, execute this query:

db.myNewCollection.find({});

This will return the first twenty documents that match the query, you can return more by;

  1. Executing an it (iterate) command.
  2. Pressing the show more button in the dbKoda GUI or using the hot key Ctrl + M
  3. Returning the documents using the toArray() function: db.myNewCollection.find({...}).toArray();

Clearly, it’s not much use to always return the first X documents, instead we often want to search and filter our results. One way to do this is to write Aggregate functions in MongoDB, but that’s a little too advanced for the scope of this tutorial, instead we will use some of the built in functionality in the find command to filter our results.

db.myNewCollection.find({Count: { $gte: 500}}, {"_id": 0, "Count": 0}).limit(10).sort({Name: 1});

The syntax may look a bit intimidating to start with, but if we break it down it becomes a lot clearer.

You can format code like this in dbKoda by right clicking and selecting “Format All”

With the query formatted it already appears a lot simpler.

  1. The first line db.myNewCollection selects our target collection.
  2. The second linefind({ Count: { $gte: 500} }, { _id: 0, Count: 0}) is the most dense but also the most important part of the query. We pass two JSON objects into the find command;
  3. Firstly we pass the search parameters { Count: { $gte: 500} , in this case we’re looking for any documents where Count is greater than or equal to 500, using the $gte operator. There are many, many of these operators (all documented in the official documentation).
  4. The second document we pass into the find command is our projections. The projection { _id: 0, Count: 0} tells Mongo which attributes to output at the end of the query, in this case we’re telling Mongo to hide the _id and Count attributes, by default it will output all others.
  5. We then append the functionlimit(10) , this will ensure we only return 10 documents.
  6. Finally we append the function sort({Name: 1}) , this will sort the returned documents by the Name field in ascending order (use -1 for descending).

As you can imagine, writing these sorts of simple queries can be quite cumbersome when working directly with the shell, and it only gets more complex when moving into Aggregate Queries and more advanced Database Administration tasks.

This post is already way too long, but hopefully by the end of this you gained the skills needed to manipulate some data in MongoDB and the knowledge required to continue mastering MongoDB with the help of dbKoda.

Thanks for reading!

--

--