MongoDB, founded in 2007 with more than 15 million downloads, excels at supporting real-time analytics for big data applications. Rather than storing data in tables made out of individual rows, MongoDB stores it in collections made out of JSON documents. But, why use MongoDB? How does it work? What issues should you pay attention to? Let’s answer these questions in this post.
MongoDB, a NoSQL database
MongoDB is a NoSQL database, and NoSQL == Not Only SQL. The data structure is combined with keyvalue, like JSON. The data type is very flexible, but flexibility can be a problem if it’s not defined properly. Here are some good reasons you should use MongoDB:
- If you are a front-end developer, MongoDB is much easier to learn than mySQL, because the MongoDB base language is JavaScript and JSON.
- MongoDB works well for big data, because for instance, you can de-normalize and flatten 6 tables into just 2 tables.
- MongoDB is document-based. So it is good to use if you have a lot of single types of documents.
So, now let’s examine how MongoDB works, starting with installing MongoDB:
- Download MongoDB from https://www.mongodb.com/download-center#community.
- De-zip your MongoDB file.
- Create a folder for the database, for example, Data/mydb.
- Open cmd to the MongoDB path, $ mongod –dbpath ../data/mydb.
- $ mongo , to make sure that it works.
- $ show dbs, and you can see two databases: admin and local.
- If you need to shut down the server, use $db.shutdownServer().
MongoDB basic usage
Now that you have MongoDB on your system, let’s examine some basic usage of MongoDB, covering insertion of a document, removal of a document, and how to drop a collection from MongoDB.
To insert a document, use the cmd call. Here we use employee as an example to insert a name, an account, and a country. You will see the data shown in JSON:
To remove the document:
db.collection.remove({ condition }), justOne)
justOne: true | false, set to remove the first data, but if you want to remove them all, use db.employee.remove({}).
To drop a collection (containing multiple documents) from the database, use:
db.collection.drop()
For more commands, please look at the MongoDB documentation.
What to avoid
Let’s examine some points that you should note when using MongoDB:
- Not easy to change to another database: When you choose MongoDB, it isn’t like other RDBMSes. It can be difficult to change, for example, from MongoDB to Couchbase.
- No support for ACID: ACID (Atomicity, Consistency, Isolation, Durability) is the basic item of transactions, but most NoSQL databases don’t guarantee ACID, so you need more technical skills in order to do this.
- No support for JOIN: Since the NoSQL database is non-relational, it does not support JOIN.
- Document limited: MongoDB uses stock data in documents, and these documents are in JSON format. Because of this, MongoDB has a limited data size, and the latest version supports up to 16 MB per document.
- Filter search has to correctly define lowercase/uppercase: For example: db.people.find({name: ‘Russell’}) and db.people.find({name: ‘ russell’}) are different. You can filter by regex, such as db.people.find({name:/Russell/i}), but this will affect performance.
I hope this post has provided you with some important points about MongoDB which will help you decide if you have a big data solution that is a good fit for using this NoSQL database.
About the author
Tess Hsu is a UI design and front-end programmer. He can be found on GitHub.