MongoDB
What is MongoDB?
MongoDB is an open-source,document-oriented,cross-platform ,no sequel (No SQL) database management system(DBMS).We are all familiar with the SQL databases that contain tables, rows, and columns. But in MongoDB architecture is based on collections and documents. MongoDB stores data in JSON-like documents with optional schemas.
Why MongoDB?
As I mentioned in the early section MongoDB stores data in flexible, JSON-like documents, which means fields can vary from document to document. That means in SQL when we store data in tables all rows need to have the same data fields(columns).
But in MongoDB, we can have different fields for different documents. The document model in MongoDB is mapped to the objects in your application code, so it makes a very easy way to access the documents in the database.
MongoDB is free to use.
MongoDB architecture.
The MongoDB architecture is built on collections and documents. The basic unit of data in MongoDB architecture is consist of a set of key-value pairs.MongoDB database uses a document storage format called BSON which is a binary style of JSON documents.
This is a sample document in the MongoDB database,
{id:s33fg323323fghf2g3fhg,
name:”kavindu”,
age:23,
city:”padukka”}
MongoDB vs RDBMS
Terminology conversion.
In the below table, there are some terminology conversions
Characteristics
RDBMS stands for the relational database management system(SQL).and it stores the data in related tables. But MongoDB does not follow the idea of the relationship.
That means there are no foreign keys in MongoDB.
In the below table, there are some characteristic comparisons between RDBMS and MongoDB.
Getting Started with MongoDB
There are two methods that we can use MongoDB they are,
1. Download and install MongoDB locally
2. Use MongoDB as a service (MongoDB Atlas)
If we want to download MongoDB to our local machine we can download it from the MongoDB community version, or if you want to use the online version you can visit the MongoDB Atlas website.
There is some IDE (integrated development environment)s that you can use for MongoDB.These are some common IDEs,
1. MongoDB compass
2. Robo 3T
3. Studio 3T
4. NoSQL Booster
5. Humongous.io
6. NoSql Manager
7. DronaHQ
The easiest way to get started with MongoDB is using MongoDB Atlas(MongoDB as a service), so let’s look at how to create a database in MongoDB Atlas online.
MongoDB Atlas
As the first step Open a browser and log in to https://cloud.mongodb.com after that you can select any package that you want from the below packages.
then you can create a cluster by selecting the cloud service provider and region from the below page.
Then by clicking add my own data you button on the below page we can create a database in MongoDB by entering the necessary details.
Then you can create a collection from the created database and the database will have appeared like the picture given below.(DB name is Mydatabase)
Database Queries
Let's look at what is the difference between SQL basic queries and MongoDB queries.
Insert in SQL
INSERT INTO STUDENTS(“Kavindu”, 23, “colombo”)
Insert in MongoDB
STUDENTS.insertOne({ name: “kavindu”, age: 23, city: “colombo” })
Update in SQL
UPDATE STUDENTS SET city = “padukka” WHERE name = “Kavindu”
Update in MongoDB
STUDENTS.updateOne({ _id = ObjectId(“”) }, { $set: { city: “padukka” } })
Delete in SQL
DELETE STUDENTS WHERE name = “Kavindu”
Delete in MongoDB
STUDENTS.remove({ name: “Kavindu” })
I hope you found this useful. I would like to hear from you so feel free to drop a comment or connect with me via LinkedIn