Blog
0

MongoDB: An Introduction

  • Pradyumn Sharma
  • June 6, 2017

Tags: ,

In my blog post titled “An Introduction to NoSQL“, I had talked about some of the key characteristics of NoSQL databases. In this article, we’ll look at some key concepts and features of MongoDB, one of the most prominent NoSQL database solutions.

MongoDB is a leading document oriented NoSQL database solution that is good at storing very large volumes of data. In fact, its name is derived from the word “huMONGOus”. You can configure a MongoDB cluster with hundreds or thousands of servers, providing linear scalability.

Documents and Collections

When you want to store some data in MongoDB, you provide it as a JSON document. Here are examples of such documents:

1
2
3
4
{ _id:1, name: 'Ahmad', gender: 'M', dept: 'Fin'}
{ _id:2, name: 'Bajrang', gender: 'M', dept: 'Sales'}
{ _id:3, name: 'Catherine', gender: 'F', dept: 'HR'}
{ _id:4, name: 'Dostoyevski', gender: 'M', dept: 'Prod'}

A document in MongoDB is conceptually similar to a record in a typical relational database like MySQL or Oracle. Documents in MongoDB are stored in a collection (equivalent to a table in a relational database).

Documents can have sub-documents, as well as collections of values or sub-documents. For example, the following is a valid document to store in a MongoDB collection (and a pretty natural thing to do so):

1
2
3
4
5
6
7
8
9
10
11
12
{
name: {first: 'Harish', last: 'Chandra'},
gender: 'M',
yearOfBirth: 1962,
livesIn: 'Mumbai',
countriesVisited: ['India', 'Singapore', 'Thailand', 'United Kingdom', 'Spain', 'Denmark',
'United States of America'],
languages: [
{name: 'Hindi', proficiency: 'Fluent'},
{name: 'English', proficiency: 'Fluent'},
{name: 'Sanskrit', proficiency: 'Intermediate'} ]
}

You don’t define a structure for a MongoDB collection. This means that different documents in a collection may have different fields and MongoDB will ingest them all as a part of the same collection! For example, you can insert the following two documents in the same collection:

1
2
3
4
5
6
7
8
9
10
11
12
{
name: 'Narayan Subramanian',
gender: 'M',
currentCity: 'Jaipur'
}
{
name: 'Pushpa Maheshwari',
gender: 'F',
email: 'pushpa@example.com',
worksAt: 'Indian Railways'
}

Rich Query Language

MongoDB provides a rich query language for database operations. It’s query language is built around the JSON format and is different from traditional SQL in terms of syntax. Here are some examples of the CRUD (Create, Read, Update, Delete) operations:

Inserting a document in a collection:

1
2
3
4
5
6
db.books.insert (
{
title: 'To Kill a Mockingbird',
author: 'Harper Lee'
}
)

In “persons” collection, find all males born before 1970 and all females born before 1980, sort by first name and last name of such persons:

1
2
3
4
5
6
db.persons.find (
{$or: [
{gender: 'M', yearOfBirth: {$lt: 1970} },
{gender: 'F', yearOfBirth: {$lt: 1980} }
] }
).sort ( {'name.first': 1, 'name.last': 1} )

MongoDB’s search operations are quite powerful. You can perform searches inside sub-documents as well as collections within documents.

In “persons” collection, update the document for ‘Merilyn Holmes’, setting gender to ‘F’, ‘yearOfBirth’ to ‘1997’, and ‘married’ to ‘N’:

1
2
3
4
db.persons.update (
{name: {first: 'Merilyn', last: 'Holmes'} },
{$set: {gender: 'F', yearOfBirth: 1997, married: 'N'} }
)

You can also update individual elements within collections embedded within documents.

Delete all males from the “persons” collection:

1
db.persons.remove ( {gender: 'M'} )

Some Other Features of MongoDB

  • Indexes are supported for fast retrieval of documents, just like relational databases.
  • Text indexing is supported to perform full text queries on documents.
  • You can have geospatial information in a collection, and perform powerful search operations (such as “find all restaurants within 1000 meters radius from a given position”)
  • Read-only views are a recent addition to MongoDB.
  • You can write Javascript code, both for client-side and server-side operations.
  • An aggregation framework provides more flexible and powerful query operations on collections. The framework works like a pipeline of various operations such as filter, transform, sort, grouping operation, left outer join with another collection, etc.
  • Drivers are available for all the mainstream programming languages.
  • For high availability, you can configure automatic replication with up to 49 servers automatically backing up the data from a primary server.
  • For high performance, you can partition your data across multiple “shards” (each shard being a single server or a replica set), so that workload is distributed across multiple partitions.

Closing Thoughts

All in all, MongoDB reduces some of the restrictions that traditional relational databases impose enabling us to design a database and write queries that better serve the demands of today’s applications (such as mobile services, querying large volume of data, data pipelines, etc).

MongoDB is not a replacement to relational databases. Instead it is a welcome addition to the field of database technology, providing solutions to problems that were hard or inefficient to solve using the traditional approach. That it provides great horizontal scalability and performance is an added benefit.

25 responses to “MongoDB: An Introduction”

  1. help me says:

    flXiVl We could have a hyperlink alternate contract among us

  2. Nice blog here! Also your web site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my website loaded up as fast as yours lol

  3. this paragraph, in my view its actually amazing in support of me.

  4. The leading source for trustworthy and timely health and medical news and information.

  5. There as certainly a lot to know about this topic. I love all the points you ave made.

  6. This text is worth everyone as attention. Where can I find out more?|

  7. Thanks so much for the article.Thanks Again. Want more.

  8. Way cool! Some very valid points! I appreciate you writing this write-up and also the rest of the site is extremely good.

  9. Yeah bookmaking this wasn at a high risk decision outstanding post!.

  10. Say, you got a nice blog article. Keep writing.

  11. Im no expert, but I believe you just crafted the best point. You obviously know what youre talking about, and I can truly get behind that. Thanks for being so upfront and so honest.

  12. name says:

    Major thanks for the post.Much thanks again. Really Great.

  13. This site has got some extremely helpful stuff on it! Cheers for helping me!

  14. Saved as a favorite, I like your web site!

  15. Wonderful site. Lots of helpful info here. I am sending it to a few

  16. sex toys says:

    There is noticeably a bunch to know about this. I consider you made certain nice points in features also.

  17. sex toys says:

    Thorn of Girl Great info may be uncovered on this world wide web blog site.

  18. lingerie says:

    Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is magnificent, let alone the content!

  19. lingerie nyc says:

    Usually I don at read post on blogs, however I wish to say that this write-up very pressured me to try and do it! Your writing style has been surprised me. Thanks, quite nice post.

  20. nyc lingerie says:

    It as really a nice and helpful piece of info. I am glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

  21. nyc sex toys says:

    visit this website What is the best blogging platform for a podcast or a video blog?

  22. There is noticeably a bunch to know about this. I consider you made certain nice points in features also.

  23. Im grateful for the blog.Thanks Again. Much obliged.

  24. Wow, incredible blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is fantastic, let alone the content!

  25. Dispensary says:

    Thanks a lot for the blog post.Thanks Again.

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2017 Pragati Software Pvt. Ltd. All Rights Reserved.

Enquiry

Pragatisoftware