Hello, database!

It’s time for some real data instead of mocks hardcoded in Angular services. I chose MongoDB in order to play with it a bit more than just by watching tutorials, especially because it represents a concept that is new to me. MongoDB is a document database, which compared to a relational model means more or less that records become JSON objects (called documents) and tables become collections of those objects. There is only a little trick: no foreign keys. In fact, we can put an id of one item in another item, but there are no constraints preventing from using an id of a non-existent item, nor any other violations. Actually the idea of a foreign key is rather irrelevant in a document database because its goal is to store related data in a single document. But there are still scenarios where a reference is more beneficial than nesting.

In my case, there should be data describing categories (name, parent category – so that there is a tree structure), advertisements (category, title, content, author, some parameters) and users (displayed name, credentials, contact data). My first thought was to embed advertisements in categories, but on the other hand, users should be able to see only their advertisements. When a user is deleted, his/her advertisements should be as well. But embedding advertisements in users would harm viewing all advertisements by category… So I’m afraid that I end up with a “relational document model” anyway 🙂 which seems to be a design smell, whoops… However, I’d like to compare performance for the first and last option one day, just out of curiosity.

By the way – MongoDB queries can be executed in command line by default, but for larger structures it’s painful to match all the brackets, braces and qoutation marks. One way is to write a query in Notepad++ and then use a JSTool plugin to minify it, a bit tedious as well. There are also quite a few GUI tools listed on the official page, so I gave a chance to Compass. It works the opposite way – instead of typing, operations are rather performed by clicking. Now it’s annoying when I want to insert a complex document and have to click a tiny “+” to add another field in it… And filter’s structure is checked too strictly – it’s not allowed to filter by a substring or a regular expression in general. On the other hand, update is really quick and convenient, like renaming a file. And there’s also an option to copy a document. And to view some other details, like all indexes at once. And the GUI is clear and pretty. I think this application is going to stay with me for a while.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s