Mongo DB

Mayank_Agarwal
10 min readMay 10, 2021

What is MongoDB?

As a definition, MongoDB is an open-source database that uses a document-oriented data model and a non-structured query language. It is one of the most powerful NoSQL systems and databases around, today.

Being a NoSQL tool means that it does not use the usual rows and columns that you so much associate with the relational database management. It is an architecture that is built on collections and documents. The basic unit of data in this database consists of a set of key–value pairs.It allows documents to have different fields and structures. This database uses a document storage format called BSON which is a binary style of JSON documents.The data model that MongoDB follows is a highly elastic one that lets you combine and store data of multivariate types without having to compromise on the powerful indexing options, data access, and validation rules. There is no downtime when you want to dynamically modify the schemas. What it means that you can concentrate more on making your data work harder rather than spending more time on preparing the data for the database.

‘Users love MongoDB because it offers the fastest time to value compared to any other DBMS technology.’ — Eliot Horowitz, Developer of MongoDB

Architecture of MongoDB NoSQL Database

Database: In simple words, it can be called the physical container for data. Each of the databases has its own set of files on the file system with multiple databases existing on a single MongoDB server.

Collection: A group of database documents can be called a collection. The RDBMS equivalent to a collection is a table. The entire collection exists within a single database. There are no schemas when it comes to collections. Inside the collection, various documents can have varied fields, but mostly the documents within a collection are meant for the same purpose or for serving the same end goal.

Document: A set of key–value pairs can be designated as a document. Documents are associated with dynamic schemas. The benefit of having dynamic schemas is that a document in a single collection does not have to possess the same structure or fields. Also, the common fields in a collection’s document can have varied types of data.

What makes it different from RDBMS?

You can directly compare the MongoDB NoSQL with the RDBMS and map the varied terminologies in the two systems: The RDBMS table is a MongoDB collection, the column is a field, the tuple/row is a document, and the table join is an embedded document. The typical schema of a relational database shows the number of tables and the relationship between the tables, but MongoDB does not follow the concept of relationship.

Go through the following table to understand how exactly an expert NoSQL database like MongoDB differs from RDBMS. This blog has elucidated nine different comparisons between the two.

MongoDBRDBMSDocument-oriented and non-relational databaseRelational databaseDocument basedRow basedField basedColumn basedCollection based and key–value pairTable basedGives JavaScript client for queryingDoesn’t give JavaScript for queryingRelatively easy to setupComparatively not that easy to setupUnaffected by SQL injectionQuite vulnerable to SQL injectionHas dynamic schema and ideal for hierarchical data storageHas predefined schema and not good for hierarchical data storage100 times faster and horizontally scalable through shardingBy increasing RAM, vertical scaling can happen

Important Features of MongoDB

  • Queries: It supports ad-hoc queries and document-based queries.
  • Index Support: Any field in the document can be indexed.
  • Replication: It supports Master–Slave replication. MongoDB uses native application to maintain multiple copies of data. Preventing database downtime is one of the replica set’s features as it has self-healing shard.
  • Multiple Servers: The database can run over multiple servers. Data is duplicated to foolproof the system in the case of hardware failure.
  • Auto-sharding: This process distributes data across multiple physical partitions called shards. Due to sharding, MongoDB has an automatic load balancing feature.
  • MapReduce: It supports MapReduce and flexible aggregation tools.
  • Failure Handling: In MongoDB, it’s easy to cope with cases of failures. Huge numbers of replicas give out increased protection and data availability against database downtime like rack failures, multiple machine failures, and data center failures, or even network partitions.
  • GridFS: Without complicating your stack, any sizes of files can be stored. GridFS feature divides files into smaller parts and stores them as separate documents.
  • Schema-less Database: It is a schema-less database written in C++.
  • Document-oriented Storage: It uses BSON format which is a JSON-like format.
  • Procedures: MongoDB JavaScript works well as the database uses the language instead of procedures.

Why do you need MongoDB technology?

This technology overcame one of the biggest pitfalls of the traditional database systems, that is, scalability. With the ever evolving needs of businesses, their database systems also needed to be upgraded. MongoDB has exceptional scalability. It makes it easy to fetch the data and provides continuous and automatic integration. Along with these benefits, there are multiple reasons why you need MongoDB:

  • No downtime while the application is being scaled
  • Performs in-memory processing
  • Text search
  • Graph processing
  • Global replication
  • Economical

Moreover, businesses are increasingly finding out that MongoDB is ticking all the right boxes when it comes to meeting the business requirements. Here is how:

  • MongoDB provides the right mix of technology and data for competitive advantage.
  • It is most suited for mission-critical applications since it considerably reduces risks.
  • It increasingly accelerated the time to value (TTV) and lowered the total cost of ownership.
  • It builds applications that are just not possible with traditional relational databases.

Check out this insightful video on ‘How to Learn MongoDB?’:

Advantages of MongoDB

You ought to know why technocrats project MongoDB as one of the best NoSQL databases.

Find out the six aspects through which you can realize the benefits of MongoDB:

Distributed Data Platform: Throughout geographically distributed data centers and cloud regions, MongoDB can be run ensuring new levels of availability and scalability. With no downtime and without changing your application, MongoDB scales elastically in terms of data volume and throughput. The technology gives you enough flexibility across various data centers with good consistency.

Fast and Iterative Development: Changing business requirements will no longer affect successful project delivery in your enterprise. A flexible data model with dynamic schema, and with powerful GUI and command line tools, makes it fast for developers to build and evolve applications. Automated provisioning enables continuous integration and delivery for productive operations. Static relational schemas and complex operations of RDBMS are now something from the past.

Flexible Data Model: MongoDB stores data in flexible JSON-like documents, which makes data persistence and combining easy. The objects in your application code is mapped to the document model, due to which working with data becomes easy. Needless to say that schema governance controls, data access, complex aggregations, and rich indexing functionality are not compromised in any way. Without downtime, one can modify the schema dynamically. Due to this flexibility, a developer needs to worry less about data manipulation.

Reduced TCO (Total Cost of Ownership): Application developers can do their job way better when MongoDB is used. The operations team also can perform their job well, thanks to the Atlas Cloud service. Costs are significantly lowered as MongoDB runs on commodity hardware. The technology gives out on-demand, pay-as-you-go pricing with annual subscriptions, along with 24/7 global support.

Integrated Feature Set: One can get a variety of real-time applications because of analytics and data visualization, event-driven streaming data pipelines, text and geospatial search, graph processing, in-memory performance, and global replication reliably and securely. For RDBMS to accomplish this, there requires additional complex technologies, along with separate integration requirements.

Long-term Commitment: You would be staggered to know about the development of this technology. It has garnered over 30 million downloads, 4,900 customers, and over 1,000 partners. If you include this technology in your firm, then you can be sure that your investment is in the right place.

MongoDB cannot support the SQL language for obvious reasons. MongoDB querying style is dynamic on documents as it is a document-based query language that can be as utilitarian as SQL. MongoDB is easy to scale, and there is no need to convert or map application objects to database objects. It deploys the internal memory for providing faster access to data and storing the working set.

‘The cost of managing traditional databases is high. Mistakes made during routine maintenance are responsible for 80 percent of application downtime.’ — Dev Ittycheria, President and CEO of MongoDB Inc.

Frequently Used Commands in MongoDB

Database Creation

  • MongoDB doesn’t have any methods to create a database. It automatically creates a database when you save values into the defined collection for the first time. The following command will create a database named ‘database_name’ if it doesn’t exist. If it does exist, then it will be selected.
  • Command: Use Database_name

Dropping Databases

  • The following command is used to drop a database, along with its associated files. This command acts on the current database.
  • Command: db.dropDatabase()

Creating a Collection

  • MongoDB uses the following command to create a collection. Normally, this is not required as MongoDB automatically creates collections when some documents are inserted.
  • Command: db.createCollection(name, options)
  • Name: The string type which specifies the name of the collection to be created
  • Options: The document type which specifies the memory size and the indexing of the collection. It is an optional parameter.

Showing Collections

  • When MongoDB runs the following command, it will display all the collections in the server.
  • Command: In shell you can type: db.getCollectionNames()

$in Operator

  • The $in operator selects those documents where the value of a field is equal to the value in the specified array. To use the $in expression, use the following prototype:
  • Command: { field: { $in: [<value1>, <value2>, … <valueN> ] } }

Projection

  • Often you need only specific parts of the database rather than the whole database. Find() method displays all fields of a document. You need to set a list of fields with value 1 or 0. 1 is used to show the field and 0 is used to hide it. This ensures that only those fields with value 1 are selected. Among MongoDB query examples, there is one which defines projection as the following query.
  • Command: db.COLLECTION_NAME.find({},{KEY:1})

Date Operator

  • This command is used to denote time.
  • Command:
    Date() — It returns the current date as a string.
    New Date() — It returns the current date as a date object.

$not Operator

  • $not does a logical NOT operation on the specified <operator-expression> and selects only those documents that don’t match the <operator-expression>. This includes documents that do not contain the field.
  • Command: { field: { $not: { <operator-expression> } } }

Delete Commands

  • Following are commands which explain MongoDB’s delete capabilities.
  • Commands:
    collection.remove() — It deletes a single document that matches a filter. db.collection.deleteOne() — It deletes up to only a single document even if the command selects more than one document.
    db.collection.deletemany() — It deletes all the documents that match the specified filter.

Where Command

  • To pass either a string which has a JavaScript expression or a full JavaScript function to the query system, the following operator can be used.
  • Command: $where

The forEach Command

  • JavaScript function is applied to each document from the cursor while iterating the cursor.
  • Command: cursor.forEach(function)

Where can you use MongoDB NoSQL database?

The MongoDB NoSQL database can be extensively used for Big Data and Hadoop applications for working with humongous amounts of NoSQL data that is a major portion of Big Data. MongoDB and SQL are all database systems, but what sets them apart is their efficiency in today’s world. MongoDB can also be successfully deployed for social media and mobile applications for parsing all streaming information which is in the unstructured format. Content management and delivery also sees extensive use for the MongoDB NoSQL database. Other domains are user data management and data hubs.

What is the scope of MongoDB NoSQL?

Some of the biggest companies on earth are successfully deploying MongoDB with already over half of the Fortune 100 companies being customers of this incredible NoSQL database system. It has a very vibrant ecosystem with over 100 partners and huge investor interest who are pouring money in the technology, relentlessly.

One of the biggest insurance companies on earth MetLife is extensively using MongoDB for its customer service applications; the online classifieds search portal, Craigslist is deeply involved in archiving its data using MongoDB. One of the most hailed brands in the media industry, The New York Times is using MongoDB for its photo submissions and the application that is deployed for form-building. Finally, the extent of the MongoDB dominance can be gauged by the fact that the world’s premier scientific endeavor that is spearheaded by the CERN physics laboratory is extensively using MongoDB for its data aggregation and data discovery applications.

Who is the right audience for learning MongoDB technology?

  • Software Developers, Architects, and Administrators
  • Database and Analytics Professionals
  • System Administrators and Research Professionals

How will this technology help you in your career growth?

  • MongoDB is the most widely used NoSQL database application –InfoWorld
  • A MongoDB Database Administrator in the United States can earn up to $129,000 per annum — Indeed
  • Hadoop and NoSQL markets are expected to reach $3.3 billion within the next two years — Wikibon

MongoDB is a very useful NoSQL database that is being used by some of the biggest corporations in the world. Due to some of the most powerful features of MongoDB, it offers a never before seen set of features to enterprises in order to parse all their unstructured data. Due to this, professionals who are qualified and certified in working with the basics and the advanced levels of MongoDB tool can expect to see their careers soar at a tremendous pace without any doubt. Due to its versatile and scalable nature, MongoDB can be used for datasets like social media, videos, and so on. MongoDB clients and users won’t feel a need for any other kind of databases.

--

--