How to use MongoDB
5th Oct 2016
This article is the first in a 2-part blog series about MongoDBPart 1: [you are here] How to use MongoDBPart 2: Why NOT to use MongoDB Since 2009 there is a new kid on the Database block: MongoDB[caption id="attachment_3464" align="alignnone" width="432"] More and more people are interested in MongoDB[/caption]According to Wikipedia, MongoBD is the number 4 database system at the moment.Want to know what the experts say about MongoDB? Download these answers to the most asked questions about MongoDB
MongoDB (from humongous) is a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favour of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster. Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open-source software. https://en.wikipedia.org/wiki/MongoDBWhen you look at the blue trend line above it certainly seems that MongoDB is rapidly becoming popular as a Database system. However, looks can deceive sometimes (and it also shows how easy it is to skew data). Check out the search volume of MongoDB (blue) against MySQL (red) in the graph below.[caption id="attachment_3463" align="alignnone" width="299"] Trendline for Google search (blue = MongoDB red=MySQL)[/caption]My SQL is definitely still more popular than MongoDB, but the trendline is dropping... while MongoDB's trendline is climbing. On average there are 201,000 searches for MongoDB - a stark contrast with only 74,000 searches for NoSQL (according to Google's Adwords keyword planning tool) Monthly searches globally for MongoDB: Check out what others are discovering about MongoDBMonthly searches globally for Nosql: So MongoDB is definitely nothing to be sneezed at... But we still have the graph on the top with the comparison between MYSQL and MongoDB. How does that translate in monthly searches? You guessed it.... With MongoDB being a serious contender in the Database space, the question is: how to use MongoDB? Looking at Quora, it seems that the largest users of MongoDB are companies like Foursquare and SourceForge. It's all about storing documents
Foursquare is using MongoDB. Harry Heymann says in the comments on Jul 8, 2010: "Foursquare currently has 6 MongoDB instances in production (3 masters, and 3 slaves). We expect this to increase significantly over time. I can say however that we're very pleased with MongoDB thusfar." According to their presentation from MongoSF [9], Bit.ly are using MongoDB to store and serve user history. Bit.ly currently see "~1.25B shortens per month ( 1K/sec peak )", which are logged in MongoDB. SourceForge.net is using MongoDB to store all of their data [10], and it has allowed them to "serve all of the project pages on SourceForge.net without any page level caching."And I really like Steve's answer to the question "What are the main benefits of MongoDB for WebApplications" --> after all Mongo DB is a tool, like there are many more. Understand what the tool can do and choose wisely.
That said, there's things about Mongo that are nice, but have more to do with the contrast between relational databases and document stores than with Mongo itself:* Document modeling fits certain use-cases really well. I'm still exploring particular ones, but having embedded comments on some sort of domain model is one that's been spectacular for me.* Document modeling can drastically reduce your queries per page. I have an app that only needs two queries on most pages, because of the 'denormalization'.* Due to the schema-less nature of document stores, it's much faster to get up and running, and to change the way you store your data.There are also some drawbacks:* Don't even bother using 32 bit. You have a 2G database size limit there.* It's fast because by default it only pages stuff out to disk once per second, so you could theoretically lose data.* It's also fast because with certain settings it actually returns before it knows if the write fails or not, this may or may not be what you want.Bottom line: Know your tools. use the best one.
Tomorrow we'll look at reasons why you shouldn't use MongoDB as your Database solution....