Why .(dot) is a bad idea for key name in mongodb

I find that I like mongodb. In my student years, I had trouble understanding why RDBMS are structured the way they are structured. And why you can’t just save whatever the data we want to save? In RDBMS we always had to design everything before hand. While designing databases is good, sometimes it’s difficult to move forward because you are tied to specific design. However, mongodb is not like that. You can always just save the document the way it is. Actually, what happens in reality it really accelerates development, because db is just on less worry now, when using mongodb. I’ve decided that I will be sharing some interesting things that I find about mongodb.

The first thing that I’d like to share about mongodb is that don’t use .(dot) as your key name!

The reason is that .(dot) will create subdocuments!

So, imagine that you want to save this id: apac.id.123

What happens when you pass that id, mongodb will save it apac being it’s root, id it’s child and 123 child of id!

Same goes for the $ sign.

https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names

What are the advantages of constructor autowiring over normal autowiring or property autowiring?

Constructor autowiring has advantages in that you can make the wired in fields final (particularly useful if your bean will be called from multiple threads as threadsafety is easier to analyse with finals). And you can ensure that the bean is always constructed in a valid way (although you can always implement InitializingBean and use the afterPropertiesSet method to achieve the same if you’re wiring in properties).

One of them is that when testing, doing a mock over a constructor is easy, but over properties it’s a little bit hard

ule of thumb: stick to constructor injection. Why? A constructor specifies the required dependencies of the class being instantiated. With setter/field injection on the other hand, you always have to look them up (by looking at the source code). You may run into troubles (eg NPE’s) because an instance misses some required dependencies. Have a look at olivergierke.de/2013/11/why-field-injection-is-evil

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 )

added ssl cert to my blog

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

 

 

Initial CSS

It’s a good idea to custom set styles for the elements. However, elements are pre-set to certain puddings and margins . So it’s better to reset them. In your css file:

What are the options we used inside, you can see in my previous post.