Getting Started With MongoDB
In this brief getting-started tutorial, learn about MongoDB, one of the most powerful NoSQL databases.
Join the DZone community and get the full member experience.
Join For FreeHi, everyone! In this article, I will try to explain what MongoDB is and why to use MongoDB.
What Is MongoDB?
MongoDB is one of the most powerful NoSQL databases. It does not use the usual rows and columns that we are so much used to with relational database management. It is an architecture built on collections and documents.
This database uses a document storage format called BSON, which is a binary style of JSON-style documents.
- It is a document-oriented NoSQL database.
- It is a schema-free database and based on a binary JSON format called BSON.
- It stores the data in the collection, which is simply a group of documents.
- It has an auto-sharding feature in order to scale horizontally.
Let’s explore and see how it is different from an RDBMS:
RDBMS | MongoDB |
---|---|
Database | Database |
Table | Collection |
Row | Document |
Colum | Field |
From the above table, it is very clear how MongoDB is organized in document format compared to RDBMS.
MongoDB basically stores the data in a key-value pair, which is JSON format, which is specially designed for transferring data over a network. You can have fields of the different data types in one document; it does not force you to have the same data type for all the fields in the document. With MongoDB, the advantage is that you can insert any kind of data, but this data should be in JSON format.
Now, let’s understand the architecture of MongoDB.
Database, Collection, Document
Database
- A database is a container of your collections.
- A single MongoDB server typically has multiple databases.
Collection
- A collection is basically a group of documents.
- It is similar to a table in an RDBMS.
Document
- The document is basically a set of key-value pairs.
- In one document, you can have a different kind of data.
- You can insert the same field with a different data type.
How Primary Keys Play a Different Role
In an RDBMS, as we are very well aware, the primary key is basically used to uniquely identify the data and we explicitly set the column as the primary key.
In RDBMS, there is no limitation — any column can be part of the primary key.
But if we talk about the primary key in MongoDB, the field _id is reserved for the primary key. When we insert the data without the field _id, MongoDB sets the value of it as the unique number by itself to uniquely identify the data.
If you want to set the primary key for your document, then you need to set the value of the field _id as the primary key. So, basically, in MongoDB, the field _id plays the role of the primary key.
let’s take an example of a MongoDB document:
{
_id:"12345asdfghj7890666",
name:"shubham agrawal",
email:"shubham@gmail.com",
courses:{
course_name:"scala",
fees:"5000",
duration:"3"
}
}
This is a simple example of a MongoDB document, Here you can see the field _id, which is a primary key of this document.
Why Use MongoDB?
- Data is stored in the form of JSON-style documents.
- Auto-sharding.
- Replication and high availability.
- Rich queries.
- Index on any field.
I hope now you have some basic idea of MongoDB. In my next article, I will explain operations in MongoDB.
Originally posted on the Knoldus blog.
If you enjoyed this article and want to learn more about MongoDB, check out this collection of tutorials and articles on all things MongoDB.
Published at DZone with permission of Shubham Agarwal, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments