Skip to main content

MongoDB quick thoughts

This time I'm writing of my experiences and random thoughts about MongoDB. Just a quick overview and nothing too profound.

My MongoDB experiences


I've been a part of a development team in two projects that used MongoDB as a database and in addition earlier this year I attended and completed MongoDB for Java developers online course by 10gen, the company behind MongoDB. 

Moving from relational databases to document databases isn't easy. I don't have any real experience with functional programming but I'd imagine moving from object oriented programming to functional programming is some what similar experience as moving from relational databases to document databases. Some of the rules are still the same but there are a lot of differences.

Flexible schemas


MongoDB has flexible schemas meaning that the data model in the collection can be changed per document a any time. I really like this as it gives the opportunity to store only and all the data that is needed per document. 

When the schema is flexible it means that there's no need to store null values for anything. In the example below we have two documents in a collection named "contacts".

{ _id: 1, name: "Joe", email: "joe@foo.com", phone: "1234567"}
{ _id: 2, name: "Andy", phone: "7654321" }

Were storing a contact list in the database where Joe has a email address and that's stored under key "email" and Andy has only phone number. If we were storing this same information in a relational database we'd have to store a null or empty string as Andy's email address because in the schema there's a column for email address.

Having a flexible schema still means that schema and the data model of the application has to be thought well. With the flexibility comes a cost, it means that much of the logic has to be in the application.

Indexes and searching


Another great thing in MongoDB are indexes. They work pretty much the same way as indexes work in relational databases.

Querying is a basic functionality in MongoDB it can be done against any key-value pair in a document collection. Querying is always more efficient if it's done against indexed values.

These are two things that separates document databases from key-value stores where indexing is done only on the key and querying can be done only against the key. There are some separate solutions for key-value store indexing and searching but their not part of the database itself.

Aggregating


Aggregating is a more sophisticated way of searching it gives nice opportunities to modify the search result documents before the answer is returned. This is also a nice tool for querying for statistical data based on the documents.

Replicating and sharding


Replicating is where the data is copied to multiple databases and sharding is where the data is spread between multiple database instances. These two can also be combined where data is sharded and the individual shards are replicated.

Replication is a good way to have to the data backuped and for fault tolerance and it can be used to spread reads against multiple databases. Replication also gives a opportunity to confirm writes to multiple replicas before the write is considered successful.

Sharding is a way to spread reads and writes against multiple databases.

Final thoughts


Since I've used and learned more about MongoDB and document databases I've started to think differently about applications. 

In the past a relational database was the only choice as a database for me but now MongoDB is one of the alternative solutions, more on the other solutions on a later post. This new knowledge that I've gained has had me thinking of how some of the past solutions would have probably worked better if MongoDB had been the choice for a database instead of a relational database.

Comments

Popular posts from this blog

DIY home automation v2

Previously I set up a simple home automation system for controlling outlets at home. So far I've used the system for controlling various lights at our home. I've been satisfied with oulet controlling via browser but I wanted something more. Objective I had a idea of temperature and humidity monitoring via Raspberry Pi. I was already browsing for electronics components needed for building a system with sensors but buying single components seemed a bit expensive and I would also have to actually connect all the components to RPi and write the code to read the sensor data. Mainly because of the price the components I started looking for a pre-built system with the preferred sensors. Almost immediately I came across Proove's sensor that has indoor and outdoor temperature sensors and a indoor humidity sensor. Best part of the pre-built system is that it's already compatible with TellStick system that I used for controlling outlets. I decided to buy the pre-built s

DIY home automation, new generation

I've had my DIY home automation system for controlling outlets and reading sensor data running for about two years now. The system has been working fine and I haven't had any need to touch the code since I added the sensor reading to it, until a few months back. Need for new functionality Few months ago I got a new IoT toy for a lend from a friend until I'd get my own toys, a ruuvitag sensor beacon. Ever since I found the ruuvitag for the first time from kickstarter I had the idea of getting a bunch of ruuvitags and adding their weather station sensor readings as part of my home automation system. The original home automation backend included only tellstick compatible devices and was written in Python, and in my mind it was kind of a hack. The ruuvitag beacons communicate via BLE i.e. Bluetooth Low Energy and that meant that I needed to add functionality to read the beacon data via bluetooth. I found a ruuvitag Python library and initially thought that I'd just

Automated browser testing in 2018

Every now and then I do some research on browser testing. More specifically I try to find and evaluate what kind of ready to use solutions or libraries have emerged since my previous research or if some of the previously tested solutions has come up with something new. Back in September 2017 I had some extra time on my hands and I used a couple of days to see what are the new and interesting solutions at that time. I came across some tools that I have used in the past and found two new interesting options in which one stood out. Say hello to TestCafe The most interesting solution that I found was called TestCafe . It's a open source Node.js library from a company called Developer Express Inc. They also have a commercial product called TestCafe ( confusing ) which I haven't tried as the open source library provided everything I was interested in and it seemed to be under active development . The thing that caught my interest at first was that this library doesn't depen