SlideShare a Scribd company logo
1 of 3
Download to read offline
Practical No:11
Problem Statement: Implement aggregation and indexing with suitable example
using MongoDB.
*Index*
> use prac11
switched to db prac11
> db.stud.insert({_id:1,rollno:28,name:"Shail",dept:10});
> db.stud.insert({_id:2,rollno:26,name:"Shivesh",dept:10});
> db.stud.find();
{ "_id" : 1, "rollno" : 28, "name" : "Shail", "dept" : 10 }
{ "_id" : 2, "rollno" : 26, "name" : "Shivesh", "dept" : 10 }
> db.stud.insert({_id:3,rollno:22,name:"Alquama",dept:11});
> db.stud.insert({_id:4,rollno:33,name:"Swapnil",dept:11});
>
> db.stud.find().pretty();
{ "_id" : 1, "rollno" : 28, "name" : "Shail", "dept" : 10 }
{ "_id" : 2, "rollno" : 26, "name" : "Shivesh", "dept" : 10 }
{ "_id" : 3, "rollno" : 22, "name" : "Alquama", "dept" : 11 }
{ "_id" : 4, "rollno" : 33, "name" : "Swapnil", "dept" : 11 }
db.stud.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "prac11.stud",
"name" : "_id_"
}
]
db.stud.ensureIndex({name:1});
> db.stud.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "prac11.stud",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "prac11.stud",
"name" : "name_1"
}
db.stud.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "prac11.stud",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "prac11.stud",
"name" : "name_1"
},
{
"v" : 1,
"key" : {
"rollno" : 1
},
"unique" : true,
"ns" : "prac11.stud",
"name" : "rollno_1"
}
db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.stud", "name" : "_id_" }
{ "v" : 1, "key" : { "name" : 1 }, "ns" : "prac11.stud", "name" : "name_1" }
{ "v" : 1, "key" : { "rollno" : 1 }, "unique" : true, "ns" : "prac11.stud",
"name" : "rollno_1" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.item", "name" : "_id_" }
--------------------------------------------------------------------------------
------------------------
*Aggergation*
db.item.insert({Customer:'a',Name:"Mouse",Quantity:3,Price:200});
db.item.insert({Customer:'a',Name:"Keyboard",Quantity:5,Price:800});
> db.item.insert({Customer:'b',Name:"Mouse",Quantity:3,Price:500});
> db.item.insert({Customer:'a',Name:"Keyboard",Quantity:4,Price:2000});
> db.item.find().pretty();
{
"_id" : ObjectId("5d8f128237dc2e143b51bde5"),
"Customer" : "a",
"Name" : "Mouse",
"Quantity" : 3,
"Price" : 200
}
{
"_id" : ObjectId("5d8f12a637dc2e143b51bde6"),
"Customer" : "a",
"Name" : "Keyboard",
"Quantity" : 5,
"Price" : 800
}
{
"_id" : ObjectId("5d8f12be37dc2e143b51bde7"),
"Customer" : "b",
"Name" : "Mouse",
"Quantity" : 3,
"Price" : 500
}
{
"_id" : ObjectId("5d8f12da37dc2e143b51bde8"),
"Customer" : "a",
"Name" : "Keyboard",
"Quantity" : 4,
"Price" : 2000
}
> db.item.aggregate([{$group:{_id:"Name",total:{$sum:1}}}]);
{ "result" : [ { "_id" : "Name", "total" : 4 } ], "ok" : 1 }
aggregation and indexing with suitable example using MongoDB.

More Related Content

What's hot

Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
JSON-LD for RESTful services
JSON-LD for RESTful servicesJSON-LD for RESTful services
JSON-LD for RESTful servicesMarkus Lanthaler
 
MongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad QueryMongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad QueryMongoDB
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Normalization in Database
Normalization in DatabaseNormalization in Database
Normalization in DatabaseRoshni Singh
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptxAnkit Rai
 
database application using SQL DML statements: Insert, Select, Update, Delet...
 database application using SQL DML statements: Insert, Select, Update, Delet... database application using SQL DML statements: Insert, Select, Update, Delet...
database application using SQL DML statements: Insert, Select, Update, Delet...bhavesh lande
 
Cat database
Cat databaseCat database
Cat databasetubbeles
 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexingYoshinori Matsunobu
 

What's hot (20)

MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
JSON-LD for RESTful services
JSON-LD for RESTful servicesJSON-LD for RESTful services
JSON-LD for RESTful services
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
Report 02(Binary Search)
Report 02(Binary Search)Report 02(Binary Search)
Report 02(Binary Search)
 
MongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad QueryMongoDB World 2019: The Sights (and Smells) of a Bad Query
MongoDB World 2019: The Sights (and Smells) of a Bad Query
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Normalization in Database
Normalization in DatabaseNormalization in Database
Normalization in Database
 
Sql query patterns, optimized
Sql query patterns, optimizedSql query patterns, optimized
Sql query patterns, optimized
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Schedule in DBMS
Schedule in DBMSSchedule in DBMS
Schedule in DBMS
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
database application using SQL DML statements: Insert, Select, Update, Delet...
 database application using SQL DML statements: Insert, Select, Update, Delet... database application using SQL DML statements: Insert, Select, Update, Delet...
database application using SQL DML statements: Insert, Select, Update, Delet...
 
Sql queries
Sql queriesSql queries
Sql queries
 
Quick sort
Quick sortQuick sort
Quick sort
 
Cat database
Cat databaseCat database
Cat database
 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexing
 

More from bhavesh lande

The Annual G20 Scorecard – Research Performance 2019
The Annual G20 Scorecard – Research Performance 2019 The Annual G20 Scorecard – Research Performance 2019
The Annual G20 Scorecard – Research Performance 2019 bhavesh lande
 
information control and Security system
information control and Security systeminformation control and Security system
information control and Security systembhavesh lande
 
information technology and infrastructures choices
information technology and  infrastructures choicesinformation technology and  infrastructures choices
information technology and infrastructures choicesbhavesh lande
 
ethical issues,social issues
 ethical issues,social issues ethical issues,social issues
ethical issues,social issuesbhavesh lande
 
managing inforamation system
managing inforamation systemmanaging inforamation system
managing inforamation systembhavesh lande
 
• E-commerce, e-business ,e-governance
• E-commerce, e-business ,e-governance• E-commerce, e-business ,e-governance
• E-commerce, e-business ,e-governancebhavesh lande
 
organisations and information systems
organisations and  information systemsorganisations and  information systems
organisations and information systemsbhavesh lande
 
IT stratergy and digital goods
IT stratergy and digital goodsIT stratergy and digital goods
IT stratergy and digital goodsbhavesh lande
 
Implement Mapreduce with suitable example using MongoDB.
 Implement Mapreduce with suitable example using MongoDB. Implement Mapreduce with suitable example using MongoDB.
Implement Mapreduce with suitable example using MongoDB.bhavesh lande
 
Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
 Unnamed PL/SQL code block: Use of Control structure and Exception handling i... Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
Unnamed PL/SQL code block: Use of Control structure and Exception handling i...bhavesh lande
 
applications and advantages of python
applications and advantages of pythonapplications and advantages of python
applications and advantages of pythonbhavesh lande
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data sciencebhavesh lande
 
data scientists and their role
data scientists and their roledata scientists and their role
data scientists and their rolebhavesh lande
 
statistics techniques to deal with data
statistics techniques to deal with datastatistics techniques to deal with data
statistics techniques to deal with databhavesh lande
 
introduction to data science
introduction to data scienceintroduction to data science
introduction to data sciencebhavesh lande
 

More from bhavesh lande (19)

The Annual G20 Scorecard – Research Performance 2019
The Annual G20 Scorecard – Research Performance 2019 The Annual G20 Scorecard – Research Performance 2019
The Annual G20 Scorecard – Research Performance 2019
 
information control and Security system
information control and Security systeminformation control and Security system
information control and Security system
 
information technology and infrastructures choices
information technology and  infrastructures choicesinformation technology and  infrastructures choices
information technology and infrastructures choices
 
ethical issues,social issues
 ethical issues,social issues ethical issues,social issues
ethical issues,social issues
 
managing inforamation system
managing inforamation systemmanaging inforamation system
managing inforamation system
 
• E-commerce, e-business ,e-governance
• E-commerce, e-business ,e-governance• E-commerce, e-business ,e-governance
• E-commerce, e-business ,e-governance
 
IT and innovations
 IT and  innovations  IT and  innovations
IT and innovations
 
organisations and information systems
organisations and  information systemsorganisations and  information systems
organisations and information systems
 
IT stratergy and digital goods
IT stratergy and digital goodsIT stratergy and digital goods
IT stratergy and digital goods
 
Implement Mapreduce with suitable example using MongoDB.
 Implement Mapreduce with suitable example using MongoDB. Implement Mapreduce with suitable example using MongoDB.
Implement Mapreduce with suitable example using MongoDB.
 
Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
 Unnamed PL/SQL code block: Use of Control structure and Exception handling i... Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
 
working with python
working with pythonworking with python
working with python
 
applications and advantages of python
applications and advantages of pythonapplications and advantages of python
applications and advantages of python
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data science
 
tools
toolstools
tools
 
data scientists and their role
data scientists and their roledata scientists and their role
data scientists and their role
 
applications
applicationsapplications
applications
 
statistics techniques to deal with data
statistics techniques to deal with datastatistics techniques to deal with data
statistics techniques to deal with data
 
introduction to data science
introduction to data scienceintroduction to data science
introduction to data science
 

aggregation and indexing with suitable example using MongoDB.

  • 1. Practical No:11 Problem Statement: Implement aggregation and indexing with suitable example using MongoDB. *Index* > use prac11 switched to db prac11 > db.stud.insert({_id:1,rollno:28,name:"Shail",dept:10}); > db.stud.insert({_id:2,rollno:26,name:"Shivesh",dept:10}); > db.stud.find(); { "_id" : 1, "rollno" : 28, "name" : "Shail", "dept" : 10 } { "_id" : 2, "rollno" : 26, "name" : "Shivesh", "dept" : 10 } > db.stud.insert({_id:3,rollno:22,name:"Alquama",dept:11}); > db.stud.insert({_id:4,rollno:33,name:"Swapnil",dept:11}); > > db.stud.find().pretty(); { "_id" : 1, "rollno" : 28, "name" : "Shail", "dept" : 10 } { "_id" : 2, "rollno" : 26, "name" : "Shivesh", "dept" : 10 } { "_id" : 3, "rollno" : 22, "name" : "Alquama", "dept" : 11 } { "_id" : 4, "rollno" : 33, "name" : "Swapnil", "dept" : 11 } db.stud.getIndexes(); [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.stud", "name" : "_id_" } ] db.stud.ensureIndex({name:1}); > db.stud.getIndexes(); [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.stud", "name" : "_id_" }, { "v" : 1, "key" : { "name" : 1 }, "ns" : "prac11.stud", "name" : "name_1" } db.stud.getIndexes(); [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.stud", "name" : "_id_" }, {
  • 2. "v" : 1, "key" : { "name" : 1 }, "ns" : "prac11.stud", "name" : "name_1" }, { "v" : 1, "key" : { "rollno" : 1 }, "unique" : true, "ns" : "prac11.stud", "name" : "rollno_1" } db.system.indexes.find(); { "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.stud", "name" : "_id_" } { "v" : 1, "key" : { "name" : 1 }, "ns" : "prac11.stud", "name" : "name_1" } { "v" : 1, "key" : { "rollno" : 1 }, "unique" : true, "ns" : "prac11.stud", "name" : "rollno_1" } { "v" : 1, "key" : { "_id" : 1 }, "ns" : "prac11.item", "name" : "_id_" } -------------------------------------------------------------------------------- ------------------------ *Aggergation* db.item.insert({Customer:'a',Name:"Mouse",Quantity:3,Price:200}); db.item.insert({Customer:'a',Name:"Keyboard",Quantity:5,Price:800}); > db.item.insert({Customer:'b',Name:"Mouse",Quantity:3,Price:500}); > db.item.insert({Customer:'a',Name:"Keyboard",Quantity:4,Price:2000}); > db.item.find().pretty(); { "_id" : ObjectId("5d8f128237dc2e143b51bde5"), "Customer" : "a", "Name" : "Mouse", "Quantity" : 3, "Price" : 200 } { "_id" : ObjectId("5d8f12a637dc2e143b51bde6"), "Customer" : "a", "Name" : "Keyboard", "Quantity" : 5, "Price" : 800 } { "_id" : ObjectId("5d8f12be37dc2e143b51bde7"), "Customer" : "b", "Name" : "Mouse", "Quantity" : 3, "Price" : 500 } { "_id" : ObjectId("5d8f12da37dc2e143b51bde8"), "Customer" : "a", "Name" : "Keyboard", "Quantity" : 4, "Price" : 2000 } > db.item.aggregate([{$group:{_id:"Name",total:{$sum:1}}}]); { "result" : [ { "_id" : "Name", "total" : 4 } ], "ok" : 1 }