🍃
Mongo DB
INTRODUCTION TO MONGO
SQL vs NOSQL
MongoDB terminologies
JSON vs BSON
INSTALLATION
CRUD OPERATIONS
Insert Operation
Search Operations
Update Operations
Updating arrays and embedded Documents
Deletion Operations
Importing JSON in MongoDB
Mongo export
Comparison Operators
Cursors
Cursor methods
Logical Operators
Complex Expressions
Elements Operators
Projections
Embedded Documents
Indexes
Aggregation
MongoDB atlas
MongoDB Compass
MongoDB Drivers
Python
INTRODUCTION TO MONGO
MongoDB is a document database. It stores data in a type of JSON format
called BSON. Created by company called 10gen which is now known as
MongoDB, founded by Eliot Horowitz and Merriman in 2007
Mongo DB 1
A record in MongoDB is a document, which is a data structure composed of key
value pairs similar to the structure of JSON objects
Records in a MongoDB database are called documents, and the field
values may include numbers, strings, booleans, arrays, or even nested
documents.
{
title: "Post Title 1",
body: "Body of post.",
category: "News",
likes: 1,
tags: ["news", "events"],
date: Date()
}
SQL vs NOSQL
SQL NOSQL
Data Structure: Document-oriented,
Data Structure: Tables with fixed
key-value pairs, wide-column stores, or
schema.
graphs. Redis.
Schema: Dynamic or schema-less,
Schema: Predefined, rigid structure.
allowing flexibility.
Scale: Vertical scaling (adding power to Scale: Horizontal scaling (adding more
a single server). servers to a distributed system).
Query Language: SQL (Structured Query Query Language: Varies; can be SQL-
Language). like or specialized query languages.
ACID Properties: Follows ACID ACID Properties: May relax ACID
properties for data integrity. properties for better performance.
Use Cases: Large-scale, rapidly evolving
Use Cases: Complex query-intensive data sets, and applications with changing
applications. data requirements.
Examples: MySQL, PostgreSQL, Oracle Examples: MongoDB, Cassandra,
CouchDB,
Scalability: SQL is vertically scalable; NoSQL is horizontally scalable.
Mongo DB 2
Flexibility: SQL is rigid; NoSQL is flexible, suitable for evolving data models.
Complexity: SQL for complex queries and transactions; NoSQL is simpler
but may not handle complex queries as efficiently.
MongoDB terminologies
graph TD
id1[(Database)] --collections-->id3(students) & id4(Teache
id3 --> id31[document]
id4 --> id41[document]
id5 --> id51[document]
JSON vs BSON
In mongo dB, we write JSON format only but behind the scene data is stored in
BSON (binary json) format, Using BSON mongo dB can achieve higher read and
write speeds, reduce stroage requirements, improved data manipulation
capabilities
INSTALLATION
1. Download MongoDB:
Visit the MongoDB download page and select the appropriate version
for Windows.
download database tools
download mongo DB shell
Follow the installation instructions provided.
2. Install MongoDB:
Run the installer you downloaded.
Follow the setup wizard instructions.
Choose the Complete installation type, which installs MongoDB as a
service.
3. Start MongoDB:
Mongo DB 3
MongoDB should start automatically after installation. If not, you can
start it manually by running the mongod command in the Command
Prompt.
4. Connect to MongoDB:
Open a new Command Prompt window.
Run the mongo command to connect to the MongoDB server.
CRUD OPERATIONS
To see all databases
test> show dbs;
To create database
test> use <database-name>;
To delete database
test> db.dropDatabase();
To see all collections
test> show collections;
To create new collections
test> db.createCollection('<collection-name>');
Mongo DB 4
To delete a collection
test> db.<collection-name>.drop();
Insert Operation
To insert single document in the collection
student> db.collectionName.insertOne({
key1: value1,
key2: value2,
// Additional fields as needed
});
To insert many documents in the collections
student> db.collectionName.insertMany([
{
key1: value1,
key2: value2,
// Additional fields as needed
},
{
key1: value1,
key2: value2,
// Additional fields as needed
},
// Additional documents
]);
Search Operations
To read all documents in the collections
Mongo DB 5
student> db.<collectionName>.find();
To find only the first document occurred in the list
student> db.<collectionName>.findOne({"name":"vinod"});
To search a specific Document in the collection
student> db.<collectionName>.find({ key: value });
Setting limit and skip in results of Find
student> db.<collectionName>.find().limit(5); // Limits the
student> db.<collectionName>.find().skip(5); // Skips the f
Update Operations
To update a existing document
db.<collectionName>.updateOne(
{ key: value },
{ $set: { updatedKey: updatedValue } }
);
To update multiple documents
db.<collectionName>.updateMany(
{ key: value },
{ $set: { updatedKey: updatedValue } }
);
Mongo DB 6
Removing Fields
db.mycollection.updateMany(
{filter}, // Empty filter matches all documents
{ $unset: { fieldName: 1 } }
);
Rename Fields
db.mycollection.updateMany(
{filter}, // Empty filter matches all documents
{ $rename: { "oldFieldName": "newFieldName" } }
);
Updating arrays and embedded Documents
add an element to the array
db.collectionName.updateOne(
{ filter },
{ $push: { arrayfield: "Newelement" } }
);
Remove an element from the array
db.collectionName.updateOne(
{ filter },
{ $pull: { arrayfield: "value" } }
);
Update an element in the array
Mongo DB 7
db.books.update(
{ _id: 1, "authors": "Author2" },
{ $set: { "authors.$": "UpdatedAuthor2" } }
);
Deletion Operations
To delete a single document
db.collectionName.deleteOne({ key: value });
To delete multiple documents
db.collectionName.deleteMany({ key: value });
Importing JSON in MongoDB
Open a terminal or command prompt and use the mongoimport command to
import the JSON file into MongoDB. The basic syntax is as follows:
mongoimport --db <databaseName> --collection <collectionName>
OR
mongoimport jsonfile.json -d <databaseName> -c <collectionName
The --jsonArray option simplifies the mongoimport process by indicating that the
input JSON file is a single array, where each element becomes a separate
document in the MongoDB collection. Use it for datasets structured as arrays of
objects.
Mongo export
To export data from MongoDB, you can use the mongoexport tool. This tool allows
you to export data from a collection into a JSON, CSV, or TSV file.
Mongo DB 8
mongoexport --db <databaseName> --collection <collectionName>
mongoexport --db mydatabase --collection mycollection --type=c
mongoexport --db <databaseName> --collection <collectionName>
Comparison Operators
Equality
$eq : Matches values that are equal to a specified value.
db.collectionName.find({ field: { $eq: value } })
Inequality
db.collectionName.find({ field: { $ne: value } })
Greater Than
$gt : Matches values that are greater than a specified value.
db.collectionName.find({ field: { $gt: value } })
Greater than or Equal to
$gte : Matches values that are greater than or equal to a specified value.
db.collectionName.find({ field: { $gte: value } })
Less than
Mongo DB 9
$lt : Matches values that are less than a specified value.
db.collectionName.find({ field: { $lt: value } })
Less than or Equal to
$lte : Matches values that are less than or equal to a specified value.
db.collectionName.find({ field: { $lte: value } })
IN
$in : Matches any of the values specified in an array.
db.collectionName.find({ field: { $in: [value1, value2] } })
Not IN
$nin : Matches none of the values specified in an array.
db.collectionName.find({ field: { $nin: [value1, value2] } })
Modulus
$mod : Performs a modulus operation on the field value and selects documents
with a specified remainder.
db.collectionName.find({ field: { $mod: [divisor, remainder]
Regualr Expression
$regex: Selects documents where the field matches a specified regular
expression pattern.
db.collectionName.find({ field: { $regex: /pattern/ } })
Mongo DB 10
Cursors
In MongoDB, a cursor is a pointer to the result set of a query. When you query a
MongoDB collection, the result set can be quite large, and it might not fit
entirely in memory. To efficiently handle large result sets, MongoDB uses
cursors to retrieve a small batch of documents at a time.
Cursors in MongoDB provide a flexible and memory-efficient way to handle
large result sets while allowing you to process documents as needed.
Remember to close the cursor when you are done with it, to free up server
resources.
Cursor methods
next() Method:
The next() method retrieves the next document from the result set.
var cursor = db.collection.find();
var document = cursor.next();
forEach() Method:
The forEach() method applies a JavaScript function to each document
in the result set.
var cursor = db.collection.find();
cursor.forEach(function(doc) {
printjson(doc);
});
hasNext() Method:
The hasNext() method checks if there are more documents in the result
set.
var cursor = db.collection.find();
while (cursor.hasNext()) {
var document = cursor.next();
Mongo DB 11
// Do something with the document
}
toArray() Method:
The toArray() method converts the cursor to an array, allowing you to
work with the entire result set in memory.
var cursor = db.collection.find();
var resultArray = cursor.toArray();
limit() Method:
The limit() method restricts the number of documents returned by the
cursor.
var cursor = db.collection.find().limit(10);
skip() Method:
The skip() method skips a specified number of documents and returns
the rest.
var cursor = db.collection.find().skip(5);
sort() Method:
The sort() method sorts the result set based on one or more fields.
1 for ascending order and -1 for descend
var cursor = db.collection.find().sort({ field: 1 });
count() Method:
The count() method returns the number of documents in the result set
without retrieving them.
var count = db.collection.find().count();
distinct() Method:
Mongo DB 12
The distinct() method returns an array of distinct values for a specified
field.
var distinctValues = db.collection.distinct("fieldName"
Logical Operators
AND $and
db.collection.find({
$and: [
{ field1: value1 },
{ field2: value2 }
]
});
OR $or
db.collection.find({
$or: [
{ field1: value1 },
{ field2: value2 }
]
});
NOT $not
db.collection.find({
field: { $not: { $eq: value } }
});
Complex Expressions
Mongo DB 13
The $expr operator in MongoDB is used to evaluate expressions within the
query language. It allows you to use aggregation expressions to compare
values in the same document during the query stage. This operator is
particularly useful when you need to compare fields within the same document
or perform more complex evaluations that go beyond the capabilities of regular
query operators.
The $expr operator in MongoDB is used to evaluate expressions within the
query language. It allows you to use aggregation expressions to compare
values in the same document during the query stage. This operator is
particularly useful when you need to compare fields within the same document
or perform more complex evaluations that go beyond the capabilities of regular
query operators.
Here's a basic example to illustrate the use of $expr :
db.sales.find({
$expr: {
$gt: ["$salesAmount", "$targetAmount"]
}
});
In this example, the query retrieves documents from the "sales" collection
where the value of the "salesAmount" field is greater than the value of the
"targetAmount" field within the same document.
Elements Operators
Equality ( $eq ) Operator:
Matches documents where the value of a field is equal to a specified value.
db.collection.find({ field: { $eq: value } });
Existence ( $exists ) Operator:
Matches documents where a specified field exists or does not exist.
Mongo DB 14
// Documents where the "field" exists
db.collection.find({ field: { $exists: true } });
// Documents where the "field" does not exist
db.collection.find({ field: { $exists: false } });
Type ( $type ) Operator:
Matches documents where the value of a field is of a specified BSON data
type.
// Documents where the "field" is of type String (2)
db.collection.find({ field: { $type: 2 } });
Modulus ( $mod ) Operator:
Performs a modulus operation on the value of a field and selects
documents with a specified remainder.
// Documents where the value of "field" divided by 5 has a
remainder of 1
db.collection.find({ field: { $mod: [5, 1] } });
Array Operators (e.g., $size , $elemMatch ):
MongoDB provides various operators for working with arrays.
$size : Matches documents where the size of an array field is a specified
size.
// Documents where the size of the "field" array is 3
db.collection.find({ field: { $size: 3 } });
: Matches documents where at least one element in an array
$elemMatch
matches specified criteria.
Mongo DB 15
// Documents where the "field" array has at least one el
ement greater than 10
db.collection.find({ field: { $elemMatch: { $gt: 10 } }
});
$alloperator is used as part of query expressions to match documents
where an array field contains all the specified values.
db.collection.find({ field: { $all: [value1, value2, ...]
Projections
projections refer to the mechanism of specifying which fields of a document
should be included or excluded in the result set of a query. Projections allow
you to shape the output of your queries by determining which fields are
returned and which are omitted.
To include specific fields in the result set, you specify the field names with a
value of 1 :
// Include only the "name" and "age" fields
db.collection.find({}, { name: 1, age: 1, _id: 0 });
for excluding a specific field
// Exclude the "password" field
db.collection.find({}, { password: 0 });
Embedded Documents
Query by Embedded Field:
// Find persons living in Cityville
db.people.find({ "address.city": "Cityville" });
Mongo DB 16
Query by Multiple Embedded Fields:
// Find persons in CA with age greater than 25
db.people.find({ "address.state": "CA", age: { $gt: 25 }
});
Updating Embedded Documents:
// Update the zip code of John Doe's address
db.people.update(
{ name: "John Doe" },
{ $set: { "address.zip": "54321" } }
);
Adding New Embedded Documents
// Add a new person with an embedded address
db.people.insert({
name: "Alice Smith",
age: 25,
address: {
street: "456 Oak St",
city: "Townsville",
state: "NY",
zip: "67890"
}
});
Indexes
indexes are data structures that improve the speed of data retrieval operations
on a MongoDB database. Indexes are similar to the indexes found in books:
Mongo DB 17
they provide a quick way to look up information based on the values of specific
fields. Without indexes, MongoDB would have to perform a collection scan,
which can be slow and resource-intensive, especially as the size of the
collection grows.
To create new Index
db.products.createIndex({field:1})
//1 for ascending order and -1 for descending order
To Get all the index
db.collection.getIndexes()
💡 _id is the default index
To delete all the indes
db.collection.dropIndex({field:1})
db.collection.dropIndex("index_name")
We can set to Unique:true
db.collection.createIndex({field:1}, {Unique:true})
Searching using index is faster than $regex searching
db.collection.createIndex({field:"text"})
db.products.find({field{$regex:"air"}}) //slower
db.collection.find( $text: {$search:"keyboard"}})
Mongo DB 18
Aggregation
Aggregation in MongoDB refers to the process of transforming and
manipulating documents in a collection to produce computed results. The
aggregation framework is a powerful and flexible tool that enables you to
perform data transformations, group data, filter data, and compute
aggregations on documents. It is a pipeline-based framework that processes
documents through a sequence of stages, allowing you to perform complex
transformations on your data.
$match: Filters the documents based on specified criteria.
db.collection.aggregate([
{ $match: { field: value } }
]);
$group: Groups documents based on a specified key and
performs aggregations within each group.
db.collection.aggregate([
{ $group: { _id: "$field", count: { $sum: 1 } } }
]);
$project: Reshapes the documents, including or excluding
fields.
db.collection.aggregate([
{ $project: { newField: "$oldField", _id: 0 } }
]);
$sort: Sorts the documents based on specified fields.
db.collection.aggregate([
{ $sort: { field: 1 } }
Mongo DB 19
]);
$limit and $skip: Limits the number of documents in the
output or skips a specified number of documents.
db.collection.aggregate([
{ $limit: 10 },
{ $skip: 5 }
]);
$unwind: Deconstructs an array field, creating multiple
documents for each array element.
db.collection.aggregate([
{ $unwind: "$arrayField" }
]);
$lookup: Performs a left outer join with another collection.
db.orders.aggregate([
{
$lookup:
{
from: "products",
localField: "productId",
foreignField: "_id",
as: "product"
}
}
]);
Example of aggregation
Mongo DB 20
db.orders.aggregate([
{
$lookup: {
from: "products",
localField: "productId",
foreignField: "_id",
as: "product"
}
},
{
$unwind: "$product"
},
{
$group: {
_id: "$product.category",
totalRevenue: { $sum: { $multiply: ["$quantity", "$prod
}
},
{
$sort: { totalRevenue: -1 }
}
]);
MongoDB atlas
MongoDB Atlas is a fully-managed, cloud-based database service by
MongoDB, Inc., offering a hassle-free way to deploy and scale MongoDB
databases on popular cloud platforms. It handles routine operational tasks,
such as backups, security, and scaling, allowing developers to focus on
building applications. With support for major cloud providers, robust security
features, automated backups, and real-time monitoring, MongoDB Atlas
provides an efficient and scalable solution for deploying MongoDB databases in
the cloud, suitable for a wide range of applications.
MongoDB Compass
Mongo DB 21
MongoDB Compass is the official graphical user interface for MongoDB,
offering an intuitive visual environment to explore and interact with MongoDB
databases. With features like schema analysis, query building, index
management, and real-time performance monitoring, Compass simplifies
database tasks for both developers and non-developers. It provides a user-
friendly interface to visualize and edit data, build aggregation pipelines, and
connect seamlessly to MongoDB Atlas. Whether examining data structures,
executing queries, or managing security settings, MongoDB Compass
enhances the MongoDB experience through a graphical and accessible
interface.
MongoDB Drivers
MongoDB drivers are language-specific tools that facilitate communication
between applications and MongoDB databases. They offer a simplified
interface for developers to perform common database operations (e.g., CRUD
tasks), manage connections, handle data serialization, and ensure secure
interactions. These drivers, available for various programming languages,
abstract the complexities of interacting with MongoDB, allowing developers to
work with the database using familiar language-specific syntax and
conventions.
Python
PyMongo
Mongo DB 22