A quick intro to MongoDB

 


In this blogpost we are going to take a quick look at one of the most popular peaces of modern technology, MongoDB.

What is MongoDB?

MongoDB is an open source, NoSql, non-relational, schema-less, document-oriented data base. In Mongo, data is organised in a Binary Json document format which is called a Collection. The basic differences between Mongo and a relational data base such as PostgreSQL can be summarised as:

Collections INSTEAD OF Tables

Documents INSTEAD OF Rows

Fields INSTEAD OF columns


Installation and Interface.

MongoDB can be downloaded from their official website. If you are on a Mac, you can easily use the Homebrew package manager. 

Once the installation is completed just start the MongoDB service to initiate a local DB server.



Connecting to the DataBase.

To start using my database, I'm going to use a client called RoboMongo (Robo 3T) which can be downloaded from this link

Once the client is installed, we can begin by connecting to our DataBase.



To initiate a new connection:

  • Click on "New connection".
  • Leave the address and port as localhost:default(27017).
  • Optionally name your connection. (I have named mine IceCream).
  • Click on "Test" to test the connect.
  • Click on "Save" to close the dialog and start using the DataBase.



Create a new collection

Once I'm in my client I can right click on collection and choose "New Collection".
In my case I will name it "Products".


Now, I can go ahead and insert a document into my new Collection.




Validating your Json would be a good idea prior to saving :)

I'm going to add some more products to my Database.



As you can see in the client, we currently have 2 objects with 3 fields. It's 3 because Mongo has generated an _id for our objects which is added to the additional properties we defined on setup. You can also view the types of our fields which were dynamically set when we saved our documents.

Moving forward, I now have some data organised in documents, so I can start querying my DataBase.


Find
In the following "Find" command, I'm querying my DB for any item with the price: 10.
As you can see, it rightfully found only 1. 



Projection
As we saw in the above example, the find command retrieved my desired document. But, what if it is a very large document? I might want to filter out some specific desired fields. For that purpose I can use the Projection object in my query command. In the example below, I'm asking to project only the item name for an item with the price of 10.


Comparison
Since the price field is an integer value, we can also query by less than or grater than values.

$gt: means grater than

$lt: means less than.



Count
We can also perform a count command on a collection. In the below example I'm returning the count of all documents in the "Products" collection.



Sort
The sort function, Is a very common function that allows us to orgenize the results in an ascending or descending order.
In the following example you can see that I'm sorting my results by the price filed. 

1 - means ascending. 
-1 - means descending.






For more useful commands you can visit the official MongoDB Documentation.




Comments

Popular posts from this blog

Sharing is caring - Intro to Jenkins shared libraries

Intro to Terraform and how it is related to test automation infrastructure

Test Automation, Security, and other vegetables