MongoDB
MongoDB is a popular NoSQL document-oriented database that provides a flexible and
scalable data storage solution for modern applications. MongoDB stores data in a JSON-like
format called BSON (Binary JSON), which allows for rich data structures and supports a wide
range of data types.
●
●
●
●
Flexibility - MongoDB is a schema-less database, which means that it can store data without
requiring a predefined schema. This allows for greater flexibility and adaptability in evolving
applications.
High availability - MongoDB provides built-in support for replication and automatic failover,
which ensures that data is always available and accessible to applications.
Performance - MongoDB uses a memory-mapped storage engine that provides fast read
and write operations, making it well-suited for high-performance applications.
Ease of use - MongoDB provides a simple and intuitive query language, as well as a robust
set of drivers and tools for developers to interact with the database.
MongoDB
●
●
●
●
Content management systems (CMS) - MongoDB can be used to store and manage
content, such as blog posts, articles, and multimedia files.
E-commerce applications - MongoDB can be used to store product catalogs, customer
data, and transactional data.
Social networking applications - MongoDB can be used to store user profiles, friend
relationships, and social activity data.
Analytics and reporting - MongoDB can be used to store and process large volumes of
data for business intelligence, reporting, and analytics applications.
MongoDB is a popular NoSQL document-oriented database that provides a flexible and
scalable data storage solution for modern applications. Its features and benefits make it well-suited
for a wide range of use cases, including content management systems, e-commerce applications,
IoT applications, social networking applications, and analytics and reporting.
Versions of MongoDB
●
●
●
●
MongoDB Community
MongoDB Enterprise
MongoDB Atlas
Percona MongoDB
Versions of MongoDB
●
●
●
●
MongoDB Community
MongoDB Enterprise
MongoDB Atlas
Percona MongoDB
Install MongoDB
●
Install MongoDB Community on Ubuntu
●
Install MongoDB Community on Windows
MongoDB Shell
The mongo shell is an interactive JavaScript interface to MongoDB. The mongo shell allows
you to manage data in MongoDB as well as carry out administrative tasks. Uses SpiderMonkey
JavaScript engine, which is the same engine used by the Firefox web browser.
MongoDB Data Types
●
●
●
●
●
●
●
●
●
●
●
String: A sequence of UTF-8 characters, used to store text data.
Integer: A 32-bit signed integer, used to store numeric data.
Long: A 64-bit signed integer, used to store large numeric values.
Double: A 64-bit floating point value, used to store decimal values.
Boolean: A true/false value, used to store boolean data.
Date: A date and time value, stored as the number of milliseconds since the Unix epoch
(January 1, 1970).
ObjectID: A unique identifier for a document, automatically generated by MongoDB.
Array: An ordered list of values, used to store multiple values of the same data type.
Embedded Document: A document that is nested inside another document, used to
represent complex data structures.
Binary Data: A binary value, used to store binary data such as images or other media.
Null: A special value that represents a null or undefined value.
BSON (Binary JSON) is a binary-encoded serialization format used by MongoDB to store
and exchange data. BSON has several data types that are used to store different kinds of data.
MongoDB data structure
In MongoDB, data is stored in collections and documents, which have a flexible schema.
●
●
●
Collection: A collection is a grouping of MongoDB documents that share a similar structure.
Collections are analogous to tables in relational databases. A database can have one or
more collections.
Document: A document is a set of key-value pairs that represents a single entity in a
collection. Documents are analogous to rows in relational databases. Each document in a
collection can have a different structure, allowing for flexibility in data modeling.
Key-value pairs: A document consists of one or more key-value pairs, where the key is a
string that represents a field name and the value can be any valid BSON data type. BSON is
the binary representation of JSON-like documents that MongoDB uses for data storage.
CRUD Operation
1.
2.
3.
4.
5.
6.
Create Database
Create Collection
insertOne() or insertMany() methods inserts new document into collection object.
find() method reads documents from collection.
updateOne() or updateMany() methods updates documents.
deleteOne() or deleteMany() methods delete documents from collection object.
Needed sources
Sorting, Limiting and Skipping
1.
2.
3.
sort() method sorts the result set in ascending or descending order based on the specified
field. By default, sort() sorts in ascending order.
limit() method limits the number of documents returned in the result set.
skip() metod skips a specified number of documents. This can be useful in conjunction with
limit() to return results in batches.
Comparison query operators
1.
2.
3.
4.
5.
6.
7.
8.
$eq: Matches values that are equal to a specified value.
$ne: Matches values that are not equal to a specified value.
$gt: Matches values that are greater than a specified value.
$gte: Matches values that are greater than or equal to a specified value.
$lt: Matches values that are less than a specified value.
$lte: Matches values that are less than or equal to a specified value.
$in: Matches any of the values specified in an array.
$nin: Matches none of the values specified in an array.
documentation
Logical Query Operators
1.
2.
3.
4.
$and: Performs a logical AND operation on an array of two or more expressions.
$or: Performs a logical OR operation on an array of two or more expressions.
$not: Performs a logical NOT operation on an expression.
$nor: Performs a logical NOR operation on an array of two or more expressions.
documentation
Element Query Operators
1.
$exists: Matches documents that have the specified field.
documentation
Array Query Operators
1.
2.
$all: Matches documents that contain all elements in an array field.
$size: is an array query operator that allows you to select documents that have an array
containing a specified number of elements.
documentation
Document Manipulating in MongoDB
1.
2.
$rename: is a field update operator that allows you to rename a field in a document to the
new one.
$unset: is a field update operator that completely removes a particular field from a
document.
Aggregation Pipeline in MongoDB
Aggregation pipeline is a powerful feature of MongoDB that allows you to process data and
transform
documents
in
real-time.
db.collection.aggregate([{ $match:...},{$group:...},{$sort:...}]);
1.
2.
3.
4.
5.
The $match stage filters documents by a specified condition.
The $group stage groups documents by a specified key and performs operations on them.
The $sort stage sorts documents by a specified field.
The $group stage in the aggregation pipeline to perform the equivalent of the SQL
DISTINCT keyword, along with other aggregate functions.
Or use the distinct command to retrieve a list of distinct values for a specified field from a
collection.
Needed sources
Documentation
Create index on Collection
In MongoDB, we create indexes on one or more fields in a collection to improve query
performance.
db.collection.createIndex(keys, options)
1.
2.
The keys parameter is an object that specifies the fields to index and the index type.
The options parameter is an optional object that specifies additional options for the index,
such as the index name, unique constraints etc.
Create User in MongoDB
To create a user in MongoDB, we can use the db.createUser() method.
db.createUser(
{
user: "<username>",
pwd: "<password>",
roles: [ "readWrite" ]
}
)
Interview Questions