Nosql 4
Nosql 4
PRACTICAL – 4
AIM: Indexing , Aggregation and Map Reduce in NoSQL-DB.
1. Indexing
creating the Assets collection.
for( var iCounter=1;iCounter<= 80000;iCounter++){
db.Asset_092.insert({
"Name":"Voting"+iCounter,
"Desc":"Story about a college student"+iCounter,
"Rank":iCounter,
"Language":["English","Hindi","Tamil"],
"AssetGrp":[{"GrpName":"16+",
"Desc":" Can be admitted in college 16+ years old"+iCounter}]
})
}
Default_id Index :
This is a unique Index created on the ID field of the collection. Please note that we cannot
drop this Index, as it’s the default.
db.Asset.getIndexes()
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
Compound Index :
MongoDB allows us to create a single structure to refer multiple fields.
db.Asset_092.getIndexes()
Multi-Key Index
MongoDB allows multi-key Indexes to support efficient queries against arrays. It creates an
Index key for each element in the array.
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
2. Aggregation
Aggregations operations process data records and return computed results. Aggregation
operations group values from multiple documents together, and can perform a variety of
operations on the grouped data to return a single result. In SQL count(*) and with group by is an
equivalent of mongodb aggregation
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
"url": "http://www.ganpatuniversity.ac.in",
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
"title": "NoSQL Overview",
"Description": "No sql database is very fast",
"by_user": "spp",
"url": "http://www.gnu.ac.in",
tags: ['mongodb', 'database', 'NoSQL'],
likes: 10
},
{
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: "sujal patel",
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 750
}
{
" title": "MongoDB Overvie",
"Description" : "MongoDB is no sql database",
by_user: "sujal",
"url": "http://www.ganpatuniversity.ac.in",
tags: ['mongodb', 'database', 'NoSQL'],
likes: 450
},
{
"title": "NoSQL Overview",
"Description": "No sql database is very fast",
"by_user": "spp",
"url": "http://www.gnu.ac.in",
tags: ['mongodb', 'database', 'NoSQL'],
likes: 210
},
{
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: "sujal patel",
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 1000
}
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
1.db.gnu_092.aggregate([{$group:{_id:"$by_user",num_tutorial:{$sum:"$likes"}}}])
2.db.gnu_092.aggregate([{$group:{_id:"$by_user",num_tutorial:{$avg:"$likes"}}}])
3.db.gnu_092.aggregate([{$group:{_id:"$by_user",num_tutorial:{$min:"$likes"}}}])
4.db.gnu_092.aggregate([{$group:{_id:"$by_user",num_tutorial:{$max:"$likes"}}}])
5.db.gnu_092.aggregate([{$group:{_id:"$by_user",num_tutorial:{$first:"$likes"}}}])
6.db.gnu_092.aggregate([{$group:{_id:"$by_user",num_tutorial:{$last:"$likes"}}}])
Exercise
Create collection name : purchase_orders_092(Last three digit of your enrollment no)
Insert : customer:"name ","product":"guitar", total:190.8
Customer – 4 different customer
Product – 5 different product (guitar, piano, toothbrush, pizza, drum)
Different total
Insert 12 documents
db.purchase_orders_092.insertMany([
{customer:"parth",product:"guitar",total:190.8},
{customer:"ramesh",product:"piano",total:261},
{customer:"parth",product:"pizza",total:256},
{customer:"hitiksha",product:"toothbrush",total:451},
{customer:"ramesh",product:"guitar",total:2012},
{customer:"parth",product:"pizza",total:125.2},
{customer:"krish",product:"toothbrush",total:150.5},
{customer:"hitiksha",product:"guitar",total:150.2},
{customer:"parth",product:"piano",total:125.5},
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
{customer:"krish",product:"pizza",total:250.5},
{customer:"reshma",product:"drum",total:200.0},
{customer:"parth",product:"drum",total:200.0}
])
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
4. Find total money each customer has spent on toothbrushes and pizza.
db.purchase_orders_092.aggregate([
{
$match: {
product: { $in: ["toothbrush", "pizza"] }
}
},
{
$group: {
_id: "$customer",
totalSpent: { $sum: "$total" }
}
}
])
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
6. Find how much has been spent on each product and sort it by price.
db.purchase_orders_092.aggregate([
{
$group: {
_id: "$product",
totalSpent: { $sum: "$total" }
}
},
{
$sort: { totalSpent: 1 }
}
])
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
3.Map-Reduce
db.bo_092.insert(
{
"title" : "MongoDB: The Definitive Guide",
"published" : "2013-05-23",
"authors": [
{ "parth" : "Kristina", "patel" : "Chodorow" }
],
"categories" : [ "Databases", "NoSQL", "Programming" ],
"publisher" : { "parth" : "O'Reilly" },
"price" : 32.99
})
db.bo_092.insert({
"title" : "MongoDB Applied Design Patterns",
"published" : "2013-03-19",
"authors": [
{ "subham" : "Rick", "rao" : "Copeland" }
],
"categories" : [ "Databases", "NoSQL", "Patterns", "Programming" ],
"publisher" : { "subham" : "O'Reilly" },
"price" : 32.99
})
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
db.bo_092.insert(
{
"title" : "MongoDB in Action",
"published" : "2011-12-16",
"authors": [
{ "sujal" : "Kyle", "patel" : "Banker" }
],
"categories" : [ "Databases", "NoSQL", "Programming" ],
"publisher" : { "rohan" : "Manning" },
"price" : 30.83
})
db.bo_092.insert(
{
"title" : "NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence",
"published" : "2012-08-18",
"authors": [
{ "panchal" : "Pramod J.", "akash" : "Sadalage" },
{ "panchal" : "Martin", "rohan" : "Fowler" }
],
"categories" : [ "Databases", "NoSQL" ],
"publisher" : { "akash" : "Addison Wesley" },
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
"price" : 26.36
})
db.bo_092.insert(
{
"title" : "50 Tips and Tricks for MongoDB Developers",
"published" : "2011-05-06",
"authors": [
{ "krishna" : "Kristina", "patel" : "Chodorow" }
],
"categories" : [ "Databases", "NoSQL", "Programming" ],
"publisher" : { "krishna" : "O'Reilly" },
"price" : 25.08
})
db.bo_092.insert(
{
"title" : "MongoDB in Action, 2nd Edition",
"published" : "2014-12-01",
"authors": [
{ "sujal" : "Kyle", "patel" : "Banker" },
{ "parth" : "Peter", "makvan" : "Bakkum" },
{ "tirth" : "Tim", "talvaniya" : "Hawkins" }
],
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
db.bo_092.insert(
{
"title" : "Node.js, MongoDB, and AngularJS Web Development",
"published" : "2014-04-04",
"authors": [
{ "krish" : "Brad", "talvaniya" : "Dayley" }
],
"categories" : [ "Databases", "NoSQL", "Programming", "Web" ],
"publisher" : { "krish" : "Addison Wesley" },
"price" : 34.35
})
db.runCommand( {
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
mapReduce: "books",
map: function() {
},
count = 0;
count += counters[index];
return count;
},
out: { inline: 1 }
})
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
Exercise
Create a collection called cities having 3 fields city (Ahmedabad,Mehsana,Baroda),
temperature, country. Insert 10 records into cities collection create map and reduce
function to find maximum temperature for each city
db.createCollection("cities_092")
db.cities_092.insertMany([
{city:"Ahmedabad",temperature:30,country:"India"},
{city:"Mehsana",temperature:28,country:"India"},
{city:"Baroda",temperature:32,country:"India"},
{city:"Ahmedabad",temperature:30,country:"India"},
{city:"Mehsana",temperature:22,country:"India"},
{city:"Baroda",temperature:28,country:"India"},
{city:"Ahmedabad",temperature:40,country:"India"},
{city:"Mehsana",temperature:23,country:"India"},
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
{city:"Baroda",temperature:32,country:"India"},
{city:"Ahmedabad",temperature:42,country:"India"}
])
db.runCommand({
mapReduce: "cities_092",
map: function () {
Batch : 4CE-A2
2CEIT405-NoSQL DATABASE SYSTEM 22012011092_SUJAL PATEL
emit(this.city, this.temperature);
},
reduce: function (city, values) {
return Math.max.apply(null, values);
},
out: { inline: 1 }
})
Batch : 4CE-A2