MongoDB - Getting Started

What is MongoDB?

MongoDB is a database that was found in 2007 that is based on the concept of having a non-structured query language. It is powerful, open source and uses a document-oriented data model that presents many benefits such as being easy to scale, faster access of data(even when working with large data sets) and not the mention being heavily supported. These attributes keep MongoDB ahead of its competition and are the reasons why it is still being used in many tech companies today.

Benefits of MongoDB

  1. Flexibility - If you are a developer that despises the concept of rows, columns and complex joins, then you will definitely feel at ease working with MongoDB. MongoDB uses collections(which are tables in Relational Database Management Systems) which can contain a set of documents. These documents can then vary between one another which allows them to be more dynamic since they do not have to abide to the defined structure. To take the whole dynamic concept even further, fields that are shared between documents can even have different types of data.
  2. Security - With MongoDB, you can expect a decrease in database downtime, if any, with its ability to duplicate data seamlessly over multiple servers. This reduces the risk and impact of failures ranging from machine related to data center related failures. Also, being a NoSQL database system, it cannot be vulnerable to attacks such as SQL injection. Because of these risk-reducing advantages, MongoDB can be appropriate for applications that are vital to their organization and opens up a field of possibilities for applications that do not make sense or not even possible with traditional relational databases. 

MongoDB for Windows

With over 110 million downloads in more than 100 countries, it is safe to say that MongoDB will be a worthy recognizable addition your resume. Lets begin!


  • Select Complete as the setup type
  • Based on personal preference, you can choose whether or not to download MongoDB Compass(not necessary in terms of this tutorial)
To check if the install was successful...

  • In your file manager, check for the MongoDB folder under Local Disk(C:) -> Program Files.
  • From the MongoDB folder, go to Server -> [version number] -> bin.
  • Open up the MongoDB shell by double clicking mongo.



MongoDB CRUD Commands
A great thing about MongoDB is that it automatically creates databases and collections once an entry is inserted, which speeds up and simplifies the data input process. Let's get to know more about MongoDB starting off with these simple commands.
  • show dbs - displays the complete list of databases that are currently on the server.
  • use <db-name> - switches to a database with the specified name. If the database does not exist, MongoDB will create one. Ex. use test
  • db.<collection-name>.insert({<document>}) - inserts a document under the specified collection name in the currently used database. If the collection does not exist, then MongoDB will create the collection to be inserted with the document. Ex. db.users.insert({isFirst: true})
  • show collections - displays the complete list of collections in the currently used database.
  • db.<collection-name>.find() - displays the complete list of entries under the specified collection name. Inside the parameters, we can even input arguments to refine our search. The arguments follow a similar syntax when inserting an document. Ex. db.users.find({isFirst:true})

  • db.<collection-name>.update({_id: <id-value>},{$set :{<field>:<value>}}) - updates the document that has an _id that matches the specified id-value with the new field and value under the specified collection name.  Ex. db.users.update({_id : ObjectId("5ef127fd0dd82c092700ebad")},{$set :{"isFirst":true}})


  • db.<collection-name>.drop() - drops all the documents under the specified collection before dropping the collection itself. This automatically drops the database afterwards if there are no longer any existing collections.
  • db.dropDatabase() - drops the current database and all of its entries. Be sure to run use <database-name> before the command to ensure you are as a precaution.
Conclusion
MongoDB is one of the great open source tools you can implement in your stack. From its benefits such as flexibility, security, speed combined with a simple setup procedure, you can expect a great performance increase compared to traditional relational databases. Hence why it is still prevalent in the modern tech industry to this day!

Comments

Popular posts from this blog

Code Quality Assurance - The Best Practices

An Introduction to Flex Layout and Angular Material

NgRx Store - The Basics