Introduction to MongoDB

mongo-icon

Hello everyone, I want to mentionabout MongoDB which is one document database of NoSQL databases. This post includes what MongoDB is, terminology of MongoDb, Replication and Sharding mechanism and useful information/notes. Hope you enjoy! If you see any wrong or incomplete information, please leave a comment to update it together. I prepared this post such as brief of MongoDb documentation.

I honestly recommend to join MongoDB University according to your development type to learn more deeply with examples and community. There is a weekly program.

What is MongoDB?

MongoDB is an open-source document database. I mentioned about CAP theorem in my NoSQL post. In this theorem, MongoDB is between Consistency and Partition Tolerance but it compromises for Availability. What MongoDB offers us?

  • High Performance
  • Fast Access (It does not mean access every time!)
  • Scalability
mongodb-scability-function
Figure I – Scalability & Functionality

Important Notes for MongoDB;

  • Does NOT support JOIN function between documents. (embedded documents can be)
  • Does NOT support Multi-document transactions. But it is atomic for single-document.
  • There is indexing on documents, It provides a good performance for your data.

Terminology of MongoDB 

There is really cool page includes comparison between SQL databases and MongoDB. If try to summarize the items;

SQL Terms/Concepts MongoDB Terms/Concepts
database database
table collection
row document or BSON document
column field
index index
table joins embedded documents and linking
primary key

Specify any unique column or column combination as primary key.

primary key

In MongoDB, the primary key is automatically set to the_id field.

aggregation (e.g. group by) aggregation pipeline

See the SQL to Aggregation Mapping Chart.

MongoDB store its documents as “Binary JSON” (BSON) format. One document can be maximum 16 mb. It is restricted from MongoDB. (I do not know they will change it or not.)

Example of a document;

{
	"_id": ObjectId("55b23c4800ded419a0c0f819"),
	"Author": "Bahadir Bal",
	"Title": "What is MongoDB?",
	"Content": "Could someone tell me what MongoDb is",
	"Post_Date": ISODate("2017-02-22T10:23:20.480Z"),
	"Tags": [
		"mongodb",
		"nosql"
	],
	"Comments": [
		{
			"Content": "It is a document DB.",
			"Comment_Date": ISODate("2017-02-22T13:23:20.480Z"),
			"Author": "Someone"
		},
		{
			"Content": "Yes, I agree to Someone",
			"Comment_Date": ISODate("2017-02-22T13:29:20.480Z"),
			"Author": "Someone else"
		}
	]
}

Example of Architecture with MongoDB

This is my architecture to give a really basic example;

  • Users send requests from UI via HTTP/HTTPS and Web layer accept that request and apply some validations and transfer it to App layer.
  • App Server connect to DB server via MongoDriver (.Net, Java, Perl..)
  • In DB server, MongoDB server runs. (Cluster, Replication, Scalability or Sharding can be applied.)
mongodb-architecture
Figure II – The example of MongoDB Architecture

Replication

Replica Set means that same data is stored in more than one MongoDB server. Actually we call this system as “Cluster” and it includes;

  • A Single Primary Node (read-write)
  • Multiple Secondary Nodes (only read)
  • Arbiters (decide which nodes are primary or secondary)
    • If the cluster has no arbiters, there are voting process between nodes.

Why we need Replication? (This part not only for Mongo, this is common reasons for systems)

  • Availability (We enable multiple points to access server.)
  • Data Security (We have data in different location.)
  • Recovery (If any failure, we can recover fast from replicas.)

Write Concern is an important key for replication and performance. This concern can be changeable according to your data importance and design of your system.

Sharding / Scalability

Sharding is a term to explain how your data will expand horizontally/partitioning. This example is helpful to understand what you have;

sharded-collection
Figure III – Sharding

Collection needs a shard key to divide data into shards. This key can be primary key for collection but it should NOT be “object_id”.

So, it is going to more design topic if we look into mechanism of Sharding with Replication in MongoDB;

replica-shard-2
Figure IV – Replica & Shard Together II
  • Shards; store the data.
  • Query Routers; allows client requests to be forwarded to the appropriate shards.
    Mongos is a server to be installed and should know shard servers’ IPs.
  • Config Servers; store cluster’s Meta data.
    • In production sharded clusters have exactly 3 config servers.
replica-shard
Figure V – Replica & Shard Together II

Who uses the MongoDB?

Actual question is “Who does not use Mongo?” This page show us so many company already has their solutions with MongoDB. If we look into some of them, we can more understand use cases.

  • The Weather Channel
    Mobile application to show instantaneous weather warning and information systems.
  • City of Chicago
    It is mostly IoT and SmartCity project and processing data flowing from certain points in city to make smart and safety city.
  • Expedia
    They have benefited from MongoDB when a lot of different factors such as airplane, hotel, car rental are offered together.
  • Bosch
    They have improved quality management by providing real-time analysis by providing MongoDb connection with IOT devices.

Pricing?

As I aforementioned above, MongoDB is open-source project and totally free. They have drivers (.Net, Java etc.) to integrate your project easily. This is download center. There is also an option to installation on Azure but this option makes cost for Azure and also Microsoft suggest to you their document database (DocumentDB).

Operational Issues

If you want to build a project and implement with MongoDB, you need to administrator level architecture. There are too many things you have to consider, for example;

I tried to explain what MongoDB is and hope this post is helpful. I will share more technical posts to see what you will encounter inside of MongoDb.

All figures (except figure II) are used from MongoDB documentation and university page.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s