Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With MongoDB

DZone's Guide to

Getting Started With MongoDB

In this brief getting-started tutorial, learn about MongoDB, one of the most powerful NoSQL databases.

· Database Zone ·
Free Resource

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

Hi, 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.

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

Topics:
database ,mongodb ,nosql ,bson ,json

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}