Categories
How to MongoDB

Databases: Difference between Relational Database, NoSQL database, and Columnar database

You might have heard about these terms, but you might be wondering what is the difference between these 3 types of databases. I’ll try to explain my understanding of each and when to use them.

Relational Database

Relational database is such database that stores related data. For example, you own a show and keep a record about your clients and items they bought. We can draw two tables one that stores Users and second one stores Items. So, to store transactions we logically draw relationship between these 2 unrelated data. That’s why RDBMS is called relational database, because there is certain relationship between separate pieces of information. Example of relational databases are: MySQL, PostgreSQL, Oracle Database and so on. When you put data into this kind of database, you will need to plan relationship between the data before using database.

Ex of Relational data:

Relational Database Design

NoSQL database

NoSQL database is complete opposite of Relational Database. This kind of database allows you to simply put the data that you want to store. This is quite useful in this cases:

  1. You are rapidly developing your app. The beauty of NoSQL is that you don’t need to spend time about all the relationships between data that you want to store. You just simply store your data.
  2. When you primarily want to store data. For example, you have some sensor data and you want to store it.

Above two are the ones comes to my mind. There should be other uses too. Examples of NoSQL database is MongoDB and Cassandra. The difference between the two is that MongoDB is document based, meaning it stored objects as documents. Document here is some sort of logical representation of data. MongoDB uses JSON to store data. Casandra however, just stores key and value pair.

Ex of NoSQL data:

Columnar Database

Columnar database stores all the data as a sequence of data points. This is quite useful to perform aggregation operations such as summing. Since the data is stored sequentially, it’s easier for db to perform operations, because it doesn’t have to scan the rows of data like in a relational database, it just needs to read data by knowing it’s offset. Some of the example of columnar database is AWS Redshift

Ex of columnar data:

So here we go, the only difference between these 3 database types is the way how they store data. Of course, each of them have their own usages, so using one over another gives some benefits.

Categories
How to

How to delete nested object from MongoDB collection

Sometimes, we write data into our mongodb collection by accident.

I can share interesting case that happened not so long ago.

My colleague passed id that has .(dot) in it to my API. Then on the backend mongodb saved the id. But when it saved the id, because of the dot, it got saved in the nested way. Ex: GET /api/bar?fooId=xyz.abc.123

So, this is what I can see in the database:

xyz

abc

123

Meanwhile other records are just stored like 456, or 555 or 123. Means as a single entry.

And this is an issue, because when we try to retrieve the data, nested object cannot be properly mapped back to model that I pass to fronted. The whole controller stops working, because of one invalid record that fails to be mapped properly.

That’s why you should:

  1. Validate the id
  2. Or clean up the id

So, in order to make the API work again, I needed to delete the invalid record from DB. This is how it can be done from mongodb terminal

db.getCollection('a.bcd').update({foo:"bar",_id:ObjectId("XXX")},{$unset:{"xyz.abc":""}},{ multi: true })

Note that your collection can be sharded, so you need to specify, your shard key in the first part of the update. For example, in my case shard key is foo.

The code above will delete the xyz

Hope, this saves time for someone and will be useful.

So, the lesson is, always validate the inputs )

Categories
Git GitHub How to

Git Rebase

Rebase is useful when you want to get the latest changes from the master branch into your branch. The benefit is that you will save time when merge into the master branch and you can use latest code added by your team mate.

To do rebase:

git checkout master
git pull

git checkout feature_branch
git rebase master
git push -f

Categories
GitHub How to Java EE

New app

checkout my new app under development:

http://arman.ninja:8080

Categories
Git GitHub How to

Create local repository and push it to GitHub

First of all, you will need to create local repository.

Requirement: you should have git already installed. You can find more info here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Go to the folder where your project is, then in terminal if you use Linux (0r Git Bash if you use Windows)

then type:

See if you have .gitignore file. If you have it then add folders which you want to exclude from appearing in your repository. If you don’t have it, then create it and add folders to ignore.

When you enter above command you might see message this message *** Please tell me who you are.

That means git doesn’t know particulars of the committer.

Then run this commands:

After that try committing again:

Now that git knows who we are, head to GitHub and create new repository(also skip adding README file, it’s too early too do so)

Then:

When you do this, you might face this error: hint: Updates were rejected because the remote contains work that you do
hint: not have locally.

That means remote repository has files that you don’t have locally. For example LICENSE file.

Then type:

This will allow you to get the remote files

Now:

Type your username and password

Now you if check your GitHub repository you can see that your files are there.

That’s it.

So whenever you do changes locally and want to commit them, just run these commands:

You might notice that every time you push your files, you need to enter your username and credentials. You can use below command to store your username and password. However, it will store your password somewhere on computer in plain text 🙂

then you will be asked to enter your username and password. After completing this you won’t be asked to do it again.

 

Sources:

http://kbroman.org/github_tutorial/pages/init.html

https://stackoverflow.com/questions/11403407/git-asks-for-username-every-time-i-push

 

 

Categories
How to SQL

How to convert the UUID to bin

Sometimes the UUIDs are in human unreadable format(Bin) and you want to find the string version of it. This is the query to do so:

select uuid_to_bin("ec52b511-d139-4b2a-a5d1-f903598055a7");  \\ 0xE552B5AAD1394B8AA4D1F903598033A3

then you can search the table using the result:

select XXX where uuid = 0xE552B5AAD1394B8AA4D1F903598033A3

Categories
How to

How to enable minify on your local maven build

Sometimes you want to minify your js code on your local.

Normally, minify will be enabled on production, but not on local environment. So if you want to update or add new js library, you can test the minify on your local.

Go to pom.xml(the main one, the one in the project folder)

navigate to local profile(the name might be different)  <environment>local</environment>

then change this line <skipMinify>true</skipMinify>

to <skipMinify>false</skipMinify>

Categories
Good programming practice How to JavaScript

JS: IIFE(Immediately-invoked function expression ) and safe code

Sometimes we need to declare the function and call it immediately. Below code does that, it can also accept the parameter:

So we will see “hello John”. What happens here is that we wrapped the anonymous function inside the (); brackets. This allows to declare the anonymous function. Because JS compiler treats everything inside () as declaration. Then we add function and call it

second () actually invokes the function. The rest is easy we just pass the variable that we need. You can see the first example.

My understanding is that this approach is used in many frameworks.  It also possible to put the execute brackets () outside the function wrapper. Both are ok:

The beauty of the above code is that it’s safe. By that I mean that this code will run inside it’s own execution context and not global one.

So, variable “greetings” will not clash with variables with same name, but from other places in code.

If, however, we want to overwrite some existing global variable we can do it this way:

File 1, has this code:

File 2, has this code:

 

Categories
Good programming practice How to

JS: good practices

  1. Be careful when returning some values

Ex:

function test(){

return

{

cacheable: true;

}

}

console.log(test()) ;

 

 

// console.log will return undefined, all of this is happening because the js compiler puts ; (semicolon) after the return statement. Therefore, it’s good practice to have curly braces directly after the return statment

ex: return {

cacheable: true;

}

2. Writing comments

The JS compiler will ignore the white spaces and comments so we can do something like this:

var

// this is the name of the user

firstName,

// this is the surname of the user

surname,

 

// this should be always more than 0

balance;

Categories
How to

How to: Missing commit (error: unpack failed: error Missing commit) on Smargit

Go to the folder where your project is stored, then open the Git bash and run below commands:

  1. git gc
  2. git pull –rebase
  3. git push

In case you have this error:

error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

Then, just commit the files first. You can do it in the smart git.