Unit-1: Concepts of NoSQL: MongoDB
INDEX
1.1 concepts of NoSQL. Advantages and features.
1.1.1 MongoDB Datatypes (String, Integer, Boolean, Double, Arrays, Objects)
1.1.2 Database creation and dropping database
1.2 create and Drop collections
1.3 CRUD operations (Insert, update, delete, find, Query and Projection operators)
1.4 Operators (Projection, update, limit(), sort()) and Aggregation command
NOTES
1.1 Concepts of NoSQL
Meaning of NoSQL
NoSQL, short for “Not Only SQL”, is a type of database system that provides an alternative to traditional relational databases (RDBMS). Unlike RDBMS, which stores data in tables with rows and columns and relies on a fixed schema, NoSQL databases are designed to store and manage unstructured, semi-structured, and structured data in a more flexible way. They use various data models such as key-value pairs, document-oriented, column-family, and graph databases to represent and process information. The fundamental concept behind NoSQL is scalability, flexibility, and high performance, making it suitable for modern applications dealing with big data, real-time analytics, IoT systems, and large-scale web applications.
NoSQL databases were developed as a response to the growing limitations of relational databases in handling large volumes of dynamic data generated by social media platforms, e-commerce websites, search engines, and cloud-based services. Unlike SQL databases that emphasize strict ACID (Atomicity, Consistency, Isolation, Durability) properties, NoSQL databases often follow the BASE (Basically Available, Soft state, Eventually consistent) model. This shift allows NoSQL systems to prioritize speed, availability, and distributed storage over rigid consistency, making them highly effective for modern business and technology needs.
2. Advantages of NoSQL
Scalability
Supports horizontal scaling (adding more servers) instead of vertical scaling (adding more power to a single machine).
Efficiently handles large datasets and high user loads.
Flexibility
Schema-less or dynamic schema → no need for predefined structures.
Supports storage of structured, semi-structured, and unstructured data.
High Performance
Optimized for fast read and write operations.
Suitable for real-time applications like messaging, e-commerce, and gaming.
Big Data Handling
Designed to handle large-scale data (terabytes or petabytes).
Variety of Data Models
Offers multiple data storage models (document, key-value, graph, columnar).
Allows organizations to choose the best fit for their use case.
Distributed Architecture
Built on distributed systems → supports replication, fault tolerance, and high availability.
Cost-Effectiveness
Open-source options available.
Uses commodity hardware instead of expensive enterprise machines.
3. Features of NoSQL
Schema-Free (Flexible Data Model)
No fixed schema required.
Developers can modify structure without downtime.
Horizontal Scalability
Data can be distributed across multiple servers.
Useful for cloud and large-scale web applications.
High Availability & Fault Tolerance
Provides automatic replication across servers.
Ensures minimal downtime.
Support for Different Data Types
Can store JSON, XML, images, videos, documents, and more.
Eventual Consistency (BASE Model)
Unlike RDBMS (ACID model), NoSQL often follows BASE:
Basically Available
Soft-state
Eventually consistent
Faster Development Cycle
Since schema is not strict, developers can quickly adapt to changes in application requirements.
Supports Big Data Analytics
Well-suited for handling real-time analytics and streaming data.
📘 MongoDB Data Types
Introduction
MongoDB is a document-oriented NoSQL database that stores data in the form of BSON (Binary JSON). BSON extends the flexibility of JSON by supporting additional data types and making storage and retrieval more efficient. Each document in MongoDB consists of fields and values, where values can belong to different data types. Understanding the commonly used MongoDB data types is essential for designing efficient databases and applications. Among the most frequently used data types in MongoDB are String, Integer, Boolean, Double, Arrays, and Objects (Embedded Documents).
String
The String data type in MongoDB is used to store textual data. It is the most commonly used data type and all string values in MongoDB must be UTF-8 encoded. Strings are widely used to represent information such as names, addresses, cities, and other text-based data. For example, a field name in a document may store "Hardik" or a field city may store "Mumbai". Since most real-world applications involve the storage of text, the string type becomes one of the foundational data types in MongoDB.
Integer
The Integer data type is used to store numeric values without decimal points. MongoDB supports both 32-bit and 64-bit integers, depending on the size of the value and the server architecture. Integers are commonly used for representing quantities, counts, or other numerical data that do not require fractions. For instance, a field age may store the value 25, or a field quantity may store 100. Choosing the correct integer size is important for optimizing storage and performance.
Boolean
The Boolean data type is used to represent logical values and can only have two states: true or false. This type is particularly useful for representing binary conditions such as yes/no, on/off, or active/inactive states. For example, in a document, a field isStudent may store true to indicate that a person is a student, while a field active may store false to show that an account is inactive. Booleans are widely used in applications to control logic and conditional workflows.
Double
The Double data type is used to store floating-point numbers, that is, numbers with decimal points. It is useful for scenarios where precise values are required, such as measurements, ratings, percentages, or prices. For example, a field rating may store 4.5, while a field price may store 199.99. The double type ensures accuracy in representing decimal values and is essential for applications in finance, scientific calculations, and statistical data analysis.
Arrays
The Array data type in MongoDB is used to store multiple values in a single field. An array can contain a list of values of the same type or different types, offering flexibility in storing complex data. For example, a field skills may store ["Java", "Python", "MongoDB"], or a field marks may store [88, 92, 95]. Arrays are extremely useful for representing data that naturally exists in collections or lists, such as phone numbers, product categories, or tags associated with blog posts. They allow developers to model one-to-many relationships directly within a single document.
Objects (Embedded Documents)
The Object data type, also known as Embedded Document, allows the storage of nested documents inside another document. This provides a hierarchical and more natural way of representing complex data. For example, an address field may itself be a document containing street, city, and pincode fields, such as
📘 1.1.2 Database Creation and Dropping Database in MongoDB
Introduction
In MongoDB, databases act as containers that hold collections, which in turn contain documents. A database is the highest-level organizational unit, and it allows developers to manage different sets of data independently. MongoDB provides simple commands to create and drop (delete) databases, making it highly flexible for developers. Since MongoDB is schema-less, databases are created dynamically and only when they store data. Similarly, dropping a database is equally straightforward, but it must be used with caution because the operation permanently removes all collections and documents inside that database.
Database Creation
Creating a database in MongoDB is very simple. Unlike relational databases, MongoDB does not require explicit commands to create a database structure in advance. Instead, a database is created when it is first accessed or when data is inserted into it. To switch to or create a database, the use command is employed.
For example, the command:
will switch the current working database to myDatabase. If the database does not already exist, MongoDB will create it once a collection and document are inserted into it. At this stage, the database exists in memory, but it becomes permanent only after actual data is stored.
The advantage of this dynamic approach is that developers do not need to predefine schemas or allocate space before starting development. This makes MongoDB highly flexible and efficient for applications where requirements change frequently.
Dropping a Database
Dropping a database in MongoDB means permanently deleting the database along with all its collections and documents. This operation is useful when a database is no longer required or when test databases need to be removed to save resources. The command used for this purpose is db.dropDatabase().
For example, if the current working database is myDatabase, running:
will delete the entire database along with everything stored in it. It is important to note that this operation is irreversible, and once executed, the data cannot be recovered unless a backup is available.
Because of its destructive nature, dropping a database must be performed carefully. In production environments, administrators usually ensure that backups are taken before executing such commands to avoid accidental data loss.
📘 1.2 Create and Drop Collections in MongoDB
Introduction
In MongoDB, a collection is equivalent to a table in relational databases. It is a group of BSON documents that share a common purpose. Unlike relational databases, MongoDB collections do not enforce a fixed schema, which means each document in the same collection can have different fields and structures. Collections make it possible to organize data logically while still benefiting from the flexibility of schema-less design. MongoDB allows developers to create collections both explicitly and implicitly, and also provides commands to drop (delete) collections when they are no longer needed.
Creating a Collection
There are two ways to create a collection in MongoDB: implicitly and explicitly.
Implicit Creation
In MongoDB, collections are often created automatically when a document is inserted for the first time. For example, if you switch to a database and insert a document into a new collection, MongoDB will create that collection dynamically.
In this example, the
studentscollection is created automatically when the first document is inserted.Explicit Creation
Developers can also create collections explicitly using thedb.createCollection()command. This method is useful when specific options, such as size limits or document validation rules, are required.
This command explicitly creates a collection named teachers. Additionally, options such as capped collections (fixed-size collections) can be specified:
Dropping a Collection
Dropping a collection means deleting the collection and all of the documents stored inside it. MongoDB provides a simple command to drop collections:
For example, if you want to drop the students collection, you would run:
Once this command is executed, the entire collection and its data will be permanently removed from the database. This operation is irreversible, so it should be performed with caution, especially in production environments. In practice, developers and administrators often ensure backups are available before dropping important collections.
📘 1.3 CRUD Operations in MongoDB
Introduction
CRUD stands for Create, Read, Update, and Delete, which are the four fundamental operations used in database management. MongoDB, being a document-oriented NoSQL database, provides flexible commands to perform CRUD operations on collections. These operations allow developers to manage data effectively by inserting new documents, retrieving them using queries, updating existing records, and deleting unnecessary ones. Along with basic CRUD, MongoDB also supports query operators for filtering data and projection operators to control which fields are displayed in query results.
Insert Operation (Create)
The insert operation is used to add new documents to a collection. MongoDB allows inserting a single document or multiple documents at once.
Insert One Document:
This command inserts a single document into the students collection.
Insert Many Documents:
Find Operation (Read)
The find operation retrieves documents from a collection. MongoDB provides the find() method to read data and supports both filtering and projections.
Retrieve All Documents:
This command displays all documents in the students collection.
Retrieve Specific Document:
This retrieves all documents where the course field equals "BCA".
Projection Example:
Update Operation
The update operation modifies existing documents in a collection. MongoDB provides updateOne(), updateMany(), and replaceOne() for different scenarios.
Update One Document:
Delete Operation
The delete operation removes documents from a collection. MongoDB provides deleteOne() and deleteMany().
Delete One Document:
Query Operators
MongoDB provides query operators to filter documents based on conditions.
Comparison Operators:
Projection Operators
Projection operators control which fields are returned in the query result.
Include Specific Fields:
📘 1.4 Operators and Aggregation Commands in MongoDB
Introduction
MongoDB provides a variety of operators and commands to enhance the way data is stored, retrieved, and manipulated. While CRUD operations form the foundation of database interaction, operators such as Projection, Update, Limit, and Sort give more control over data handling. Additionally, MongoDB’s Aggregation framework is a powerful tool for performing advanced data analysis, transforming, and summarizing documents within collections. Together, these features enable developers to extract meaningful insights, optimize queries, and manage data more effectively.
Projection Operator
Projection in MongoDB is used to control which fields of a document should be displayed when retrieving data. Instead of showing all the fields, we can choose to include or exclude specific fields.
Example – Including Fields:
Update Operators
Update operators are used to modify existing documents in MongoDB. Instead of replacing the entire document, these operators allow fine-grained updates.
$set: Updates or adds a field.
limit() Operator
The limit() function restricts the number of documents returned in a query. This is useful when dealing with large datasets, as it helps in controlling the output size.
Example:
sort() Operator
The sort() operator is used to order query results based on field values. By default, documents are not returned in any specific order, so sorting helps in organizing the results.
Ascending Order (1):
Aggregation Commands
Aggregation in MongoDB is a framework for processing data and performing advanced computations. It works through a pipeline, where documents pass through different stages, and each stage transforms the data in some way.
Common Aggregation Stages:
$match – Filters documents based on conditions (similar to
find)
2. $group – Groups documents by a field and performs operations like count, sum, average.
3. $project – Reshapes documents by including, excluding, or computing new fields.
4. $sort – Sorts documents within the pipeline
5. $limit – Restricts the number of documents in the result.