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

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 depend on WebDri…

Studying and developing as software development professional

As everybody in software development knows, or should know, that studying and experimenting is something one must do to stay on top of the game. That said this time I'm writing about my experiences and ideas of studying. In this post I'll be covering different methods of studying and how I feel about them and what other types of resources are available.

Reading a book Reading a book is probably the most traditional way of studying and I do read a few books every year. To me this is a way to learn theory and principles of something but usually little to nothing to do with the actual implementation. This type book I usually read in a week or two and I like these books when their length is reasonable somewhere between 50 and 250 pages.
Reading a book with exercises These are very common type of books in software development. These usually cover some theory and the exercises bring a pragramatic approach with what one can learn a basic implementation. Some of these books are good i…

DIY home automation v1

For years I've been interested in home automation. I've had remote controllable outlets from a few different manufacturers but I've never been quite satisfied with just the remote. What if I could control my outlets within my local network from any device, now that's something I wanted to have.

Controlling outlets from computer A few years back I bought a three pack of remote controllable nexa outlets. A while ago I discovered that another company manufactured a control unit that's plugged in to a USB port and best of all they provided linux software for it.

To make full use of these I'd need a computer that's always on and that's where I could make use of Raspberry Pi.

Setting up outlets
I had already set up my outlets with the remote that came in the retail pack just follow the manufacturer instructions.
It might be possible to configure the outlets completely via the software at least for some brands but I haven't tried it so I can't be sure.…