Why are people shifting to MongoDB?
Shrey Batra
CEO @ Cosmocloud | Ex-LinkedIn | Angel Investor | MongoDB Champion | Book Author | Patent Holder (Distributed Algorithms)
Hey guys, these days you might have seen MongoDB being so much more famous than SQL based databases such as MySQL or PostgreSQL. Even in college courses and interviews, people are switching from asking SQL questions to MongoDB fundamentals. But why is it so? Let's look at some things which make MongoDB a unique, much better and futuristic database in today's time.! P.S I am coming up with a 5 day LIVE course to learn MongoDB and get to understand all important concepts..! Register for it now..!
What's different in MongoDB?
MongoDB is a NoSQL based "document database". This means that it does not follow the Relational RDBMS internals and do not have relationships defined. Instead, it follows a more natural - JSON like structure (BSON data type) as individual records or rows in the database. You can keep all "related" information together in a single document, for example a User document might look like -
{
"_id": ObjectId("123456677"),
"username": "shreybatra",
"name": {
"first": "Shrey",
"middle": null,
"last": "Batra"
},
"age": 25,
"mobileNumber": "999999999",
"socialLinks": {
"github": "some_url",
"linkedin": "some_url"
},
"addresses": [
{
"house": "123",
"country": "..."
},
{
// more addresses
...
}
]
}
Over here, I have tried to add all common data types such as string (name), integer (age), nested/embedded data (socialLinks), arrays of data (addresses). Along with this, MongoDB supports much more types of data such as
What else than data type? How does Document Database help?
Well, it's everything. Coming from a SQL background you may seem confused and find it tough to understand that a Document Database / document world is a different perspective than your SQL world. Where you have normalisation to improve performance and data modelling in SQL world, you have similar data modelling concepts and patterns in MongoDB world but in a quite different perspective.
MongoDB Moto: Data that is accessed together, stays together..!
Just memorise the above thing, that's what the document database world relies on. Instead of breaking and normalising your data, you keep your data together in a single object (preferably) so that whenever you need to read the data, you have exactly 1 place to read, with minimal joins and extra lookups.
Does that mean that you should always club all information together in a Single Document?
No..! Absolutely not...! If you think MongoDB does not support Joins then you are absolutely wrong. It's a very important feature and when we say embed data than normalise, you might wanna read the quote again..!
Data that is accessed together, stays together..!
Now, imagine a system having Inventory, Users, Product Orders. You can't club the Orders data in Inventory, as a single document having all details of object as well as all transactions. You can't club all orders inside a User object saying all orders from the User stays in the User object (or document). That will simply not scale. You will have to break it down into 3 different Collections (or tables in SQL world) and join them as and when needed.
领英推荐
Does this mean we have a performance hit when compared to SQL world?
Absolutely not..! Looking at data from a different perspective does not mean it has a worse performance than SQL database. Instead, in many scenarios it would be even better than a SQL database, specially in terms of Developer Productivity and efforts needed to scale a MongoDB database (shard / partition / etc.)
We have indexing in MongoDB similar to SQL databases, with much more interesting index types such as a MultiKey index which you can apply on Array fields, Geo Spatial indexes and much more..!
What's more that MongoDB gives natively?
This blog post could actually NOT cover everything that MongoDB has, simply because there is SO much to talk about..!
Conclusion
There is just so much about MongoDB, that I can't just stop expressing about..! But if you feel you miss out on so many of these things and wanna learn and explore more, come join me in my 5 Day MongoDB course where I'll be teaching all the basic and important concepts of MongoDB.
Just a warning, seats are limited and I tend to have only small batches to concentrate QnA better. Be sure to book fast..!