Mongo DB
Mongo DB
1. Data Model:
SQL: SQL databases use a structured data model known as the relational model.
Data is organized into tables with predefined schemas, where rows represent
individual records and columns represent attributes.
NoSQL: NoSQL databases support various data models, including document,
key-value, wide-column, and graph models.
They offer more flexibility in data representation and often don't require a
predefined schema.
2. Schema:
SQL: SQL databases enforce a rigid schema, where the structure of the database,
including data types and relationships between tables, is defined upfront.
NoSQL: NoSQL databases are schema-less or schema-flexible, allowing for
dynamic and agile schema changes. They can handle unstructured or semi-
structured data more efficiently.
3. Scalability:
SQL: Traditional SQL databases are typically scaled vertically, meaning you scale
up by adding more resources (CPU, RAM) to a single server.
NoSQL: NoSQL databases are designed for horizontal scalability, meaning they
can scale out by distributing data across multiple servers or clusters. This makes
them suitable for handling large volumes of data and high traffic loads.
4. Consistency:
SQL: SQL databases generally adhere to ACID (Atomicity, Consistency,
Isolation, Durability) properties, providing strong consistency guarantees.
NoSQL: NoSQL databases often prioritize availability and partition tolerance
(CAP theorem), sacrificing strong consistency for eventual consistency.
However, consistency models vary among different NoSQL databases.
5. Query Language:
SQL: SQL databases use structured query language (SQL) for defining and
manipulating data.
NoSQL: NoSQL databases may use different query languages or APIs
depending on the data model.
For example, MongoDB uses a JSON-like query language, while Cassandra
uses CQL (Cassandra Query Language).
6. Use Cases:
SQL: SQL databases are suitable for applications that require complex
transactions, strict consistency, and structured data, such as financial
systems, ERP (Enterprise Resource Planning), and traditional RDBMS
applications.
NoSQL: NoSQL databases are well-suited for applications with large-scale
data requirements, high write throughput, flexible schema, and distributed
data models. They are commonly used in web applications, real-time
analytics, IoT (Internet of Things), and content management systems.
SQL databases offer strong consistency and a structured data model, NoSQL
databases provide scalability, flexibility, and performance advantages for certain
use cases, particularly those involving large-scale distributed systems and
unstructured data.
The choice between SQL and NoSQL depends on the specific requirements and
characteristics of the application.
1. Document-oriented Databases:
- MongoDB
- Couchbase
- CouchDB
- Amazon DocumentDB
2. Key-value Stores:
- Redis
- Amazon DynamoDB
- Apache Cassandra (can be used as a key-value store as well as a wide column
store)
4. Graph Databases:
- Neo4j
- Amazon Neptune
5. Time-series Databases:
- InfluxDB
- TimescaleDB
- Amazon Timestream
6. Object-oriented Databases:
- db4o
- ObjectDB
7. Multi-model Databases:
- Couchbase (supports document-oriented and key-value storage)
Each of these databases has its own strengths and weaknesses, and the choice
depends on the specific requirements of the application, including scalability, data
structure, query patterns, and consistency needs.
MongoDB offers several advantages and benefits, making it a popular choice for
many developers and businesses:
1. Flexible Data Model: MongoDB is a NoSQL database that uses a flexible, JSON-
like document data model. This allows developers to store and query data in a way
that closely resembles how their data objects are represented in their application
code.
6. Rich Query Language: MongoDB provides a powerful query language that supports
a wide range of operations, including CRUD (Create, Read, Update, Delete),
aggregation, geospatial queries, text search, and more.
7. Support for ACID Transactions: Starting from version 4.0, MongoDB offers multi-
document ACID transactions, ensuring data consistency and integrity even in
complex, multi-step operations.
rich ecosystem of tools and libraries that integrate with MongoDB, making it easy to
use with popular programming languages and frameworks.
9. Ad hoc Queries: MongoDB's query language allows for ad hoc queries, enabling
developers to quickly retrieve and analyze data without needing to pre-define complex
joins or schemas.
10. Support for Multiple Data Types: MongoDB supports a variety of data types,
including integers, strings, arrays, embedded documents, and binary data. This
flexibility makes it suitable for storing diverse types of data, ranging from simple key-
value pairs to complex hierarchical structures.
Limitations of MongoDB :
MongoDB RDBMS
It is a non-relational and document-oriented
database. It is a relational database.
It is suitable for hierarchical data storage. It is not suitable for hierarchical data storage.
Procedure
Follow these steps to install MongoDB Community Edition using the MongoDB
Installer wizard.
Download the MongoDB Community .msi installer from the following link:
➤ MongoDB Download Center
d. Click Download.
a. Go to the directory where you downloaded the MongoDB installer (.msi file). By
default, this is your Downloads directory.
The wizard steps you through the installation of MongoDB and MongoDB Compass.
MongoDB Service
MongoDB
The following installs and configures MongoDB as a Windows service.
Starting in MongoDB 4.0, you can configure and start MongoDB as a Windows
service during the install, and the MongoDB service is started upon successful
installation.
o Select either:
or
>show dbs
// lists of databases
>use databasename
>db.dropDatabase()
Rename a collection:
myDatabase> db.post.renameCollection("blog_post",true);
Insert operation :
insertOne();
insertMany();
db.posts.insertOne({_id:2,title:"Post title 2",body:' post
body2',category:'sports',likes:4,tags:['news','sports'],date:new
Date()});
The first parameter is a query object to define which document or documents should be
updated.
Examples:
myDatabase> db.Product.find()
myDatabase> db.Product.updateOne({_id:101},{$set:{price:400}});
myDatabase> db.Product.updateOne({prodName:'TV'},{$set:
{price:200000,use:'timepass'}});
myDatabase>db.student.find();
_id: 3,
Name: 'Pooja',
rollno: '21b81a0526',
branch: 'AIML',
age: 23,
myDatabase> db.student.updateOne({Name:'pooja'},{$set:
{"course.duration":5}});
{
_id: 3,
Name: 'Pooja',
rollno: '21b81a0526',
branch: 'AIML',
age: 23,
course: { courseName: 'BTech', duration: 5 },
address: { city: 'Hyderabad', state: 'Telangana', country: 'India' }
}
exampledatabase> db.pets.updateOne({'name':'Wag'},{$unset:
{"details.type":'camel'}});
exampledatabase> db.pets.find();
[
{
_id: 1,
name: 'Wag',
details: {
weight: '40',
awards: {
'Florida Dog Awards': 'Top Dog',
'New York Marathon': 'Fastest Dog',
'Sumo 2020': 'Biggest Dog'
}
}
}
]
UpdateMany:
// updates likes on all the documents by ‘1’ that matched the provided query.
myDatabase> db.posts.updateMany({}, { $inc: { likes: 1 } })
myDatabase> db.posts.updateMany({},{$inc: {likes:2}});
db.dogs.updateMany(
{ },
{ $unset: { type: "" } }
)
db.dogs.updateMany(
{ },
{ $unset: { name: "", weight: "" } }
)
{
"_id" : 1,
"name" : "Wag",
"details" : {
"type" : "Dog",
"weight" : 20,
"awards" : {
"Florida Dog Awards" : "Top Dog",
"New York Marathon" : "Fastest Dog",
"Sumo 2020" : "Biggest Dog"
}
}
}
exampledatabase> db.pets.updateOne({name:'Wag'},{$set:{'details.weight':'40'}});
myDatabase> db.posts.updateMany({likes: {$gte:9,$lte:11}},{$set:
{category:'science'}});
myDatabase> db.posts.find({likes:{$in:[9,11]}},{likes:1,_id:0});
myDatabase> db.posts.find({likes:{$gte:7,$lte:13}},{likes:1,_id:0});
Comparison Operators:
$gt operator
myDatabase> db.inventory.find({'qty':{$gt:75}},
{qty:1,dim_cm:1,_id:0}).pretty();
$lt operator
myDatabase> db.inventory.find({'qty':{$lt:75}},
{qty:1,dim_cm:1,_id:0}).pretty();
[
{ qty: 25 },
{ qty: 50 },
{ qty: 45 },
{ qty: 25, dim_cm: [ 14, 21 ] },
{ qty: 50, dim_cm: [ 14, 21 ] }
]
$eq operator
exampledatabase> db.student.find({$expr:{$eq:
[{$year:"$DOB"},1999]}});
$nin operator
myDatabase> db.posts.find({'category':{$nin:['science','Technology']}},
{category:1,likes:1,_id:0});
[
{ category: 'News', likes: 7 },
{ category: 'xyz', likes: 10 },
{ category: 'Event', likes: 5 }
]
$ne operator
Returns the documents that match values that are not equal to the given conditions
myDatabase> db.posts.find({category:{$ne:'Technology'}});
logical operators
Logical $and:
Returns the documents that match both the query conditions.
Q1:
db.employees.find({ $and: [{"job_role": "Store Associate"}, {"emp_age": {$gte: 20,
$lte: 30}}] }).pretty()
q2:
myDatabase> db.persons.find({$and: [{'Gender':'Male'},{age:{$gte:40,$lte:50}}]});
Logical $or:
Returns the documents that match either of the query conditions.
myDatabase> db.posts.find({$or: [{category:'science'},{tags:{$eq:['news','sports']}} ]},
{category:1,tatags:1,_id:0});
[
{ category: 'xyz', tags: [ 'news', 'sports' ] },
{ category: 'science', tags: [ 'news', 'events' ] },
{ category: 'science', tags: [ 'news', 'sports' ] },
{ category: 'Event', tags: [ 'news', 'sports' ] }
]
Logical $nor
Returns the documents that match neither of the query conditions
myDatabase> db.posts.find({$nor: [{category:'science'},{tags:{$eq:
['news','sports']}} ]},{category:1,tags:1,_id:0});
[
{ category: 'News', tags: [ 'news', 'events' ] },
{ category: 'Technology', tags: [ 'news', 'entertainment' ] }
]
Logical $not
Returns documents that does not match the provided query
myDatabase> db.posts.find({'category':{$not:{$eq:'science'}}},{date:0,_id:0,body:0});
myDatabase> db.student.find({'address.city':{$not:{$eq:'Hyderabad'}}});
$regex operator
myDatabase> db.persons.find({"Name":{$regex:/^S/i}}).pretty();
//persons Name starts with ‘d’
myDatabase> db.Product.find({ 'prodName':{ $regex: /l$/i } });
//prodName ends with ‘l’
Aggregate $group
myDatabase> db.Users.aggregate([{
... $group:{_id:"$role"}}]);
[ { _id: 'admin' }, { _id: 'programmer' }, { _id: 'Developer' } ]
myDatabase> db.student.aggregate([
... { $sort:{"address.city":1} },{$project:{"Name":1,"address":1,_id:0}}]);
[
{ Name: 'raj' },
{ address: { city: 'Bangalore', state: 'KA', country: 'India' } },
{
Name: 'Pooja',
address: { city: 'Hyderabad', state: 'Telangana', country: 'India' }
}
]
Aggregation $Project
This aggregation stage passes only the specified fields along to the next aggregation
stage.
myDatabase> db.Users.aggregate([ { $sort: { "age": -1 } }, { $project: { "Name": 1,
"age": 1,_id:0 } }
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
db.students.updateOne(
{ name: "joe" },
{ $push: { scores: { $each: [ 90, 92, 85 ] } } }
)
myDatabase> db.posts.updateMany({'category':'Event'},{$push:{tags:{$each:
['culture','quiz']}}});
db.listingsAndReviews.aggregate([
{ $match : { property_type : "House" } },
{ $limit: 2 },
{ $project: {
"name": 1,
"bedrooms": 1,
"price": 1
}}
])
Tutorial Mongodb shell queries:
db.posts.find({likes: {$in:[1,4]}},{tags:1,category:1});
db.posts.find({likes: {$in:[1,4]}},{tags:1,category:1}).count();
update the likes of the posts documents having item value as ‘journal’
Ebox queries:
How many fields will be displayed in the result, when below query is executed?
myDatabase>db.Customer.aggregate([
{$match:{"Status":"A"}},
{$group: {_id:"$CustomerNo", totalamount:{$sum:"$Amount"}}}]);
[
{ _id: 2003, totalamount: 200 },
{ _id: 2002, totalamount: 200 },
{ _id: 2001, totalamount: 500 }
]
myDatabase> db.Customer.aggregate([{ $group:{_id:"$CustomerNo",total1:
{$sum:"$Amount"}}}]);
[
{ _id: 2001, total1: 750 },
{ _id: 2002, total1: 450 },
{ _id: 2003, total1: 200 }
]
myDatabase> db.Customer.find({},{"CustomerNo":1,_id:0,Status:1}).limit(1).skip(1);
[ { CustomerNo: 2002, Status: 'B' } ]
myDatabase> db.orders.find();
[
{
_id: 2,
customerName: 'Ramesh',
OrderTotal: 2340,
orderitems: [ { item: 'soap', price: 24 }, { item2: 'dal', price: 234 } ]
}
]
myDatabase> db.orders.find({},{orderitems:{$slice:-1}});
[
{
_id: 2,
customerName: 'Ramesh',
OrderTotal: 2340,
orderitems: [ { item2: 'dal', price: 234 } ]
}
]
Write a query to display all the details of an authors with age greater than or equal
to 30. Display the documents sorted in ascending order based on age.
\]
myDatabase> db.Authors.find({age:{$gte:30}}).sort({age:-1});
Aggregation Pipelines:
Examples :
Aggregating : $group
Example: group posts documents based on category
myDatabase> db.posts.aggregate({$group: {_id:"$category"}});
[
{ _id: 'science' },
{ _id: 'xyz' },
{ _id: 'News' },
{ _id: 'Technology' }
]
Query: aggregating the likes under each category where likes >1
myDatabase> db.posts.aggregate([
// determine documents where likes gt one
{$match: {likes:{$gt:1}}},
//group the likes of those documents based on categories
{$group:{_id:"$category", totallikes: {$sum:"$likes"}}}]);
Find the aggregated amount of each status in collection “Customer”. (use above
schema)
myDatabase> db.Customer.aggregate([{$group: {_id:"$Status",totalamount:
{$sum:"$"Amount"}}}]);
Aggregate: $count
Counts the total no of documents passed from the previous stage.
myDatabase> db.posts.aggregate([ {$match:{tags:{$eq:['news','sports']}}},
{$count:"sportsandnews"}]);
[ { sportsandnews: 2 } ]
myDatabase> db.posts.aggregate([
{$match: {"category":"science"}},{$count:"Science-category"}]);
[ { 'Science-category': 2 } ]
myDatabase> db.persons.aggregate([
... {$match: {'Gender':'Female'}},{$count:"total females"}]);
[ { 'total females': 2 } ]
myDatabase> db.author.aggregate([{$match: {age:{$gte:30}}},{$sort: {age:-1}},
{$limit:2}]);
Query 2:
author schema:
myDatabase> db.authors.insertOne(
{
_id: 46,
author_name:{firstname:"Reb",middlename:"v",lastname: "Doubinsky"},
age: 50,
gender:"Male",
email_id:"Doubinsky @gmail.com",
phoneno:9339812543,
Address:
[
{street: "45th main street",City:"Avenue Montaigne",state:"Paris",
country:"France"},
{street: "31c main street",City:"Champs-Elysees",state:"Paris",country:"France"}
]
}
);
Write a query to display all the details of an authors with age greater than or equal
to 30. Display the documents sorted in ascending order based on age.
myDatabase> db.author.find({age:{$gte:30}}).sort({age:1});
Query 3:
Write a query to display the title of all the documents present in the 'books' collection.
myDatabase> db.books.find({},{title:1,_id:0});
[
{ title: 'MongoDB: The Definitive Guide' },
{ title: 'react js unleashed' }
]
Query: 4
Write a query to display the lastname of all the documents present in the 'author'
collection.
myDatabase> db.author.find({},{“author_name.lastname”:1,_id:0});
Query:5 Write a query to display _id, firstname and email_id of the authors
with age greater than or equal to 30.
myDatabase> db.Authors.find({age:{$gte:30}},
{_id:1,"author_name.firstname":1,email_id:1});
Query 6:
//Inserting publisher data:
{
_id:61,
pub_name:"O'Reilly Media",
country:"US"
}
Write a query to display _id of the publishers whose name ends with the letter 'i'.
exampledatabase> db.publisher.find({pub_name:{$regex: /i$/i}},{_id:1});
Query 7: Write a query to display _id, title and author_id details of the books published
in the date '2010-09-24'.
Books_Database> myDatabase> db.books.find({'published_date':
{'$eq':ISODate("2010-09-24T00:00:00.000Z")}});
[
{
_id: 11,
title: 'MongoDB: The Definitive Guide',
author_id: [ 41, 42, 43, 44 ],
published_date: ISODate('2010-09-24T00:00:00.000Z'),
pages: 216,
language: 'English',
publisher_id: 61,
price: 2000
}
]
Query 8:
query 9:
Write a query to display the number of authors with age greater than 30. Give an alias
name to number of authors as 'Authorcount'.
1
db.author.aggregate([
{$match: {age:{$gt:30}}},{$count:"AuthorCount"}]);
[ { AuthorCount: 2 } ]
i-Assess queries;
1. Write a query to display author_name of first 3 documents present in the
'author' collection.
db.author.find({},{author_name:1,_id:0}.limit(3);
2. Write a query to display first 4 documents present in the 'author' collection after
sorting the documents in descending order based on age.
db.author.find({}).sort({age:-1}).limit(4);
3. Write a query to display _id and author_name of the author with age greater than
30.
db.author.find({}).sort({age:-1}).limit(4);
2. Choosing a Shard Key: MongoDB uses a shard key to partition data across shards.
chosen based on the access patterns and distribution of your data.
3. Enabling Sharding on a Database: Once the cluster is set up, you can enable sharding
on a specific database. This essentially tells MongoDB to start partitioning data across
shards.
4. Sharding a Collection: After enabling sharding on a database, you can shard
individual collections within that database. MongoDB splits data in a collection into
chunks based on the shard key and distributes these chunks across shards.
5. Monitoring and Balancing: MongoDB continuously monitors the distribution of data
across shards and balances the data to ensure even distribution. If data becomes
imbalanced (e.g., if one shard has significantly more data than others), MongoDB
automatically moves chunks between shards to rebalance the cluster.
Significance of Sharding:
Horizontal Scalability: Sharding allows MongoDB to scale horizontally by
distributing data across multiple servers. This enables MongoDB to handle large
volumes of data and high throughput.
High Availability: Sharding can also improve fault tolerance and availability. By
distributing data across multiple shards, if one shard goes down, the cluster can
continue to serve requests using data from the remaining shards.
Improved Performance: Sharding can improve query performance by
distributing the query load across multiple shards. This can help mitigate the
performance impact of heavy read and write operations on a single server.
Isolation of Workloads: Sharding allows you to isolate workloads by placing
different types of data on different shards. For example, you can shard data
based on geographical location or customer segment, which can help optimize
performance and resource utilization.