0% found this document useful (0 votes)
32 views33 pages

Mongo DB

Uploaded by

shashipavan5203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views33 pages

Mongo DB

Uploaded by

shashipavan5203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Unit-II

Compare and contrast NoSQL and SQL databases:

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.

Examples of NOSQL databases:


NoSQL databases come in various types, each catering to different use
cases and data management needs.
Here are some examples of NoSQL databases along with their types:

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)

3. Wide Column Stores:


- Apache Cassandra
- HBase

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.

Examples of SQL Databases:


SQL databases, also known as relational databases, have been the traditional choice for
storing structured data.
1. MySQL:
2. PostgreSQL:
3. Microsoft SQL Server:
4. Oracle Database:.
5. SQLite: SQLite is a lightweight, embedded SQL database engine that is self-
contained and serverless. It is widely used in mobile and desktop applications as well as
embedded systems due to its simplicity and minimal resource requirements.
6. IBM Db2: IBM Db2 is a family of data management products that includes relational
database management systems for various platforms, including mainframes,
distributed systems, and cloud environments. It offers features for data warehousing,
transaction processing, and analytics.

Notable Features and Characteristics of MongoDB


 Document Oriented: In MongoDB, all the data stored in the documents
instead of tables like in RDBMS. In these documents, the data is stored in
fields (key-value pair) instead of rows and columns which make the data much
more flexible in comparison to RDBMS. And each document contains its
unique object id.

 Indexing: In MongoDB database, every field in the documents is indexed with


primary and secondary indices this makes easier and takes less time to get or
search data from the pool of the data.

 Scalability: MongoDB provides horizontal scalability with the help of


sharding.

o Sharding means to distribute data on multiple servers, here a large


amount of data is partitioned into data chunks using the shard key, and
these data chunks are evenly distributed across shards that reside across
many physical servers. It will also add new machines to a running
database.

 Replication: MongoDB provides high availability and redundancy with the


help of replication, it creates multiple copies of the data and sends these copies
to a different server so that if one server fails, then the data is retrieved from
another server.
 Aggregation: It allows to perform operations on the grouped data and get a
single result or computed result. It is similar to the SQL GROUPBY clause. It
provides three different aggregations i.e, aggregation pipeline, map-reduce
function, and single-purpose aggregation methods
 High Performance: The performance of MongoDB is very high and data
persistence as compared to another database due to its features like scalability,
indexing, replication, etc.
Advantages of MongoDB :

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.

2. Scalability: MongoDB is designed to scale out horizontally, making it easy to


handle large volumes of data and high traffic loads.
It supports sharding, which enables distributing data across multiple servers,
allowing for linear scalability as the dataset grows.

3. High Performance: MongoDB offers high-performance read and write


operations due to its use of memory-mapped files for storage and internal
optimizations like index structures (including compound indexes and geospatial
indexes) and aggregation pipelines.

4. Automatic Failover and Replication: MongoDB provides built-in support for


replica sets, which are self-healing clusters of database nodes. Replica sets provide
automatic failover and data redundancy, ensuring high availability and data
durability.

5. Schema-less Design: MongoDB's schema-less design allows for flexible and


dynamic schema evolution. Developers can easily modify the structure of
documents without needing to update a rigid schema beforehand.

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.

8. Community and Ecosystem: MongoDB has a large and active community of


developers and users, providing extensive documentation, tutorials, and resources.

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.

Easily integrates with Big data platforms.

Limitations of MongoDB :

 It uses high memory for data storage.


 You are not allowed to store more than 16MB data in the documents.
 The nesting of data in BSON is also limited you are not allowed to nest data
more than 100 levels.

Differences between MongoDB and RDBMS


Some major differences in between MongoDB and the RDBMS are as follows:

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.

It has a dynamic schema. It has a predefined schema.

It centers around the CAP theorem It centers around ACID properties


(Consistency, Availability, and Partition (Atomicity, Consistency, Isolation, and
tolerance). Durability).

In terms of performance, it is much faster In terms of performance, it is slower than


than RDBMS. MongoDB.

Install MongoDB Community Edition

Procedure

Follow these steps to install MongoDB Community Edition using the MongoDB
Installer wizard.

Download the installer.

Download the MongoDB Community .msi installer from the following link:
➤ MongoDB Download Center

a. In the Version dropdown, select the version of MongoDB to download.

b. In the Platform dropdown, select Windows.

c. In the Package dropdown, select msi.

d. Click Download.

Run the MongoDB installer.

For example, from the Windows Explorer/File Explorer:

a. Go to the directory where you downloaded the MongoDB installer (.msi file). By
default, this is your Downloads directory.

b. Double-click the .msi file.

Follow the MongoDB Community Edition installation wizard.

The wizard steps you through the installation of MongoDB and MongoDB Compass.

a. Choose Setup Type


You can choose either the Complete (recommended for most users)
or Custom setup type. The Complete setup option installs MongoDB and the
MongoDB tools to the default location. The Custom setup option allows you to
specify which executables are installed and where.
b. Service Configuration
Starting in MongoDB 4.0, you can set up MongoDB as a Windows service during
the install or just install the binaries.

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 Install MongoD as a Service MongoDB as a service.

o Select either:

 Run the service as Network Service user (Default)

This is a Windows user account that is built-in to Windows

or

 Run the service as a local or domain user

 For an existing local user account, specify a period (i.e. .) for


the Account Domain and specify the Account
Name and the Account Password for the user.

 For an existing domain user, specify the Account


Domain, the Account Name and the Account
Password for that user.

o Service Name. Specify the service name. Default name is MongoDB. If


you already have a service with the specified name, you must choose
another name.

o Data Directory. Specify the data directory, which corresponds to the --


dbpath. If the directory does not exist, the installer will create the
directory and sets the directory access to the service user.

o Log Directory. Specify the Log directory, which corresponds to the --


logpath. If the directory does not exist, the installer will create the
directory and sets the directory access to the service user.
c. Install MongoDB Compass
Optional. To have the wizard install MongoDB Compass, select Install
MongoDB C
MongoDB Query API

Creating database using mongosh


>use databasename

>show dbs
// lists of databases
>use databasename

//Drop the connected database using the dropDatabase() function.

>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()});

Selecting/Querying the documents: find();


myDatabase> db.inventory.find({dim_cm:{$gt:22.85}});
myDatabase> db.inventory.find({dim_cm:{$gt:22.85,$lt:30}});
myDatabase> db.persons.find({age:{$gt:25}},{Name:1,Gender:1,_id:0});
Update Document:
UpdateOne();
//update the first document that is found matching the provided query
UpdateMany();
//update all documents that match the provided query

The first parameter is a query object to define which document or documents should be
updated.

The second parameter is an object defining the updated data.

Examples:

myDatabase> db.Product.find()

{ _id: 101, prodName: 'dal', price: 400, use: 'household' },

{ _id: 2, prodName: 'TV', price: 100000, use: 'entertainment' }

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,

course: { courseName: 'BTech', duration: '4 Years' },

address: { city: 'Hyderabad', state: 'Telangana', country: 'India' }

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'},{$set: `{"details.type":'camel'}});


exampledatabase> db.pets.find();
[
{
_id: 1,
name: 'Wag',
details: {
type: 'camel',
weight: '40',
awards: {
'Florida Dog Awards': 'Top Dog',
'New York Marathon': 'Fastest Dog',
'Sumo 2020': 'Biggest Dog'
}
}
}

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'
}
}
}
]

Insert if not found


db.posts.updateOne(
{title: "Post Title 5” },
{
$set:
{
title: "Post Title 5",
body: "Body of post.",
category: "Event",
likes: 5,
tags: ["news", "events"],
date: Date()
}
},
{ upsert: true }
)

Example 2: myDatabase> db.post.updateOne({title:'title 8'},{$set:{title:'title


8',body:'body 8',likes:9,category:'news',tags:['news','sports'],date:Date()}},
{upsert:true});

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}});

Unsetting the value:


Removing individual field from the collections.
myDatabase> db.posts.updateMany({},{$set: {status:"attended"}});
myDatabase> db.posts.updateMany({},{$unset:{status:"attended"}});
Example:
Collection Name:Dogs

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }


{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 }
{ "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }

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});

Deleting the documents from Collection:


deleteOne()
deleteMany()
findOneAndDelete()
Inserting Many documents:
exampledatabase> db.programmers.insertMany(
... [
... { name: "James Gosling" },
... { name: "Dennis Ritchie" },
... { name: "Bjarne Stroustrup" }
... ]
... );

Deleting Single Document:


Example:
exampledatabase> db.programmers.deleteOne({name:"Bjarne Stroustrup"});
{ acknowledged: true, deletedCount: 1 }
example:
exampledatabase> db.programmers.findOneAndDelete({name:"James Gosling"});
{ _id: ObjectId('65f6ee38547afd18e18b1057'), name: 'James Gosling' }
Deleting Many documents:
Example:
exampledatabase> db.programmers.deleteMany({name:{$in:
["David","Herbert","Raju","Dennis"]}});
{ acknowledged: true, deletedCount: 2 }

Deleting the collection from the Database:


exampledatabase> db.programmers.drop();
true

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]}});

myDatabase> db.persons.find({$expr: {$eq:["$Name","swapna"]}});


[
{
_id: ObjectId('65f1f225c7d32c4b9963b713'),
Name: 'swapna',
age: 25,
Gender: 'Female'
}
]

$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'}});

$in , $nin operators


db.inventory.find({"price": { $in: [3, 6]}}).pretty()
db.inventory.find({"price": { $nin: [5.23, 3, 6, 3.59, 4.95]}}).pretty()

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}}]});

myDatabase> db.persons.find({$and: [{'Name':'Ramesh'},{age:{$gte:40,$lte:50}}]});

myDatabase> db.posts.find({$and: [{likes:{$gte:5}},{tags:{$eq:['news','sports']}}]},


{likes:1,tags:1,_id:0});
[
{ likes: 10, tags: [ 'news', 'sports' ] },
{ likes: 13, tags: [ 'news', 'sports' ] },
{ likes: 5, tags: [ 'news', 'sports' ] }
]
myDatabase> db.Product.find({$and: [{prodName:'dal'},{price:500}]});

[ { _id: 101, prodName: 'dal', price: 500, use: 'household' } ]


myDatabase> db.Product.find({$and: [{prodName:{$regex:/^d/i}},{price:
{$gte:500}}]});
[ { _id: 101, prodName: 'dal', price: 500, use: 'household' } ]

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.inventory.aggregate([{$group: {_id:"$item" ,totalqty:


{$sum: "$qty"}}}]);
[
{ _id: 'planner', totalqty: 150 },
{ _id: 'postcard', totalqty: 45 },
{ _id: 'paper', totalqty: 200 },
{ _id: 'notebook', totalqty: 100 },
{ _id: 'journal', totalqty: 50 }
]

myDatabase> db.persons.find({}).sort({"Name":-1});// sorting with


aggregate
Aggregate : $sort
myDatabase> db.inventory.aggregate([{$sort:{"qty":-1}}]);
myDatabase> db.persons.aggregate([{$sort:{"age":-1}},{$project:
{"Name":1,"age":1,_id:0}}]);
[
{ Name: 'Sachin', age: 50 },
{ Name: 'Ramesh', age: 40 },
{ Name: 'Geetha', age: 25 },
{ Name: 'swapna', age: 25 }
]

Limit the retrieved documents:


myDatabase> db.persons.aggregate([{$sort:{"age":-1}},{$project:
{"Name":1,"age":1,_id:0}},{$limit:2}]);

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 } }

Aggregate operators $match and $group


myDatabase> db.posts.aggregate([ {$match: {likes:{$gt:1}}}, {$group:
{_id:"$category", totallikes: {$sum: "$likes"}}}]);
[
{ _id: 'News', totallikes: 7 },
{ _id: 'Technology', totallikes: 3 },
{ _id: 'xyz', totallikes: 10 },
{ _id: 'Event', totallikes: 5 },
{ _id: 'science', totallikes: 24 }
]
db.students.updateMany(
{ },
{ $push: { scores: 95 } }
)

db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
db.students.updateOne(
{ name: "joe" },
{ $push: { scores: { $each: [ 90, 92, 85 ] } } }
)

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

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.Customer.find().sort({"_id":-1}); //descending order


Select the query which will display _id, Name and Dob of the students born in the
month 'January'.

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: group posts documents based on tags


myDatabase> db.posts.aggregate({ $group: { _id: "$tags" } });
[
{ _id: [ 'news', 'events' ] },
{ _id: [ 'news', 'sports' ] },
{ _id: [ 'news', 'entertainment' ] }
]

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"}}}]);

//Refer for Customer schema :


myDatabase> db.Customer.find();
[
{ _id: 1, CustomerNo: 2001, Amount: 500, Status: 'A' },
{ _id: 2, CustomerNo: 2002, Amount: 250, Status: 'B' },
{ _id: 3, CustomerNo: 2003, Amount: 200, Status: 'A' },
{ _id: 4, CustomerNo: 2001, Amount: 250, Status: 'B' },
{ _id: 5, CustomerNo: 2002, Amount: 200, Status: 'A' }
]

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}]);

Ebox i-design queries:


Q1: Write a query to display first 5 documents present in the 'books' collection
Sol: myDatabase> db.books.find({}).limit(1);
[
{
_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 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:

db.author.aggregate([ { $group: { _id: null, AuthorCount: { $sum: 1 } } },


{ $project: { _id: 0, AuthorCount: 1 } }] );

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);

What is the CAP Theorem?


The CAP Theorem in MongoDB, also known as Brewer's theorem, is a principle in
distributed system theory that states that it is impossible for a distributed system to
simultaneously guarantee three properties: consistency "C", availability "A", and
partition tolerance "P".
The "CAP" in the CAP Theorem MongoDB refers to the three properties that a
distributed system cannot simultaneously guarantee: consistency "C", availability "A",
and partition tolerance "P".
Consistency "C" means that all nodes in a distributed system have the same view of
the data at the same time. Any read operation on the system should return the most
recent write or an error. In other words, the data remains consistent across all nodes.
Availability "A" ensures that every request made to a distributed system receives a
response, regardless of system failures or disruptions. The system remains operational
and responsive to user requests, providing uninterrupted services.
Partition tolerance "P" refers to the system's ability to continue functioning and
providing services even in the presence of network partitions or failures. Network
partitions occur when communication between nodes is disrupted, leading to the
formation of separate groups of nodes.
CAP Theorem MongoDB states that in the presence of a network partition "P", a
distributed system must choose between consistency "C" and availability "A". It is
impossible to have both perfect consistency and availability during a partition. The
system must prioritize one over the other.
CAP Theorem MongoDB highlights the inherent trade-offs and challenges in designing
distributed systems. Architects and developers need to consider the specific
requirements and constraints of their systems to determine whether to prioritize
consistency (CP systems) or availability (AP systems) in the event of a partition.
It's important to note that the CAP theorem assumes that network partitions are a
reality in distributed systems. However, in the absence of a partition, a distributed
system can achieve both consistency and availability (CA).
In the context of MongoDB, it is often said that MongoDB prioritizes Consistency and
Partition tolerance over Availability. This means that MongoDB emphasizes data
consistency and partition tolerance, while sacrificing immediate availability in the
event of network partitions.
However, MongoDB provides tunable consistency, allowing users to configure the level
of consistency based on their application requirements through features like Read
Concern and Write Concern.

Strategies For Migrating From SQL to NoSQL Database


Migrating from a SQL database to a NoSQL database can be a complex process, but
there are several strategies that can be used to make the transition smoother.
Some common strategies for migrating from SQL to NoSQL:
Analyze the current SQL schema and data model:
 Analyze the current SQL schema and data model to identify any potential issues
that may arise during the migration.
 identify any data relationships that may need to be modified, and any data that
may need to be denormalized or split across multiple tables. (redesign the
schema).

Choose the right NoSQL database:


 There are several types of NoSQL databases, each with their own strengths and
weaknesses.
 Choosing the right NoSQL database for the specific needs of the application is
important to ensure that the data is stored efficiently and can be accessed easily.
Identify data access patterns:
 NoSQL databases are optimized for different types of data access patterns than
SQL databases.
 Identifying the data access patterns in the application.
Plan the data migration:
 Export data from the SQL database into a format that can be imported into the
NoSQL database, and then importing the data into the new database.
Modify the application code:
 Because NoSQL databases often have different data access patterns and query
languages than SQL databases, it may be necessary to modify the application
code to work with the new database.
 This can involve modifying data access code, query language, and data
structures.
Test the new system:
 Test data access times, query performance, and overall system reliability.

Sharding Process in MongoDB:


Sharding in MongoDB is a method for distributing data across multiple machines.
It's a horizontal scaling approach where data is partitioned and distributed across
multiple servers, called shards.
Each shard is essentially an independent database that stores a subset of the data.
The process of sharding involves several steps:
1. Setting up a Sharded Cluster: To start sharding, you first need to set up a sharded
cluster. A sharded cluster consists of multiple components:
- Shard: Each shard is a separate MongoDB instance, possibly a replica set.
- Config servers: These servers store metadata about the cluster configuration, such as
which data is stored on which shard.
- Query routers (mongos): These are the routing components that direct client
requests to the appropriate shard.

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.

You might also like