0% found this document useful (0 votes)
5 views4 pages

DBMS Practical 11

Uploaded by

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

DBMS Practical 11

Uploaded by

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

Zeal College of Engineering and Research

Subject: Database Management System Lab

Name: Janhavi Rahul Raikar


Roll No: T213011
Div: C
Batch: C1
Group B: Practical No. 11

PROBLEM STATEMENT:
Title of the Assignment: MongoDB – Aggregation and Indexing: Design and
Develop MongoDB Queries using aggregation and indexing with suitable example
using MongoDB.

CODE:

> use ASSI_11


switched to db ASSI_11
> db.createCollection("products")
{ "ok" : 1 }
> db.products.insertMany([{ item: "Laptop", cust_name: "Amit", cost: 1200 },{ item:
"Smartphone", cust_name: "Koustubh", cost: 800 },{ item: "Tablet", cust_name: "Madhav", cost:
600 },{ item: "Headphones", cust_name: "Amit", cost: 150 },{ item: "Smartwatch", cust_name:
"Koustubh", cost: 250 },{ item: "Monitor", cust_name: "Madhav", cost: 300 },{ item: "Keyboard",
cust_name: "Amit", cost: 100 },{ item: "Mouse", cust_name: "Koustubh", cost: 50 },{ item: "Fan",
cust_name: "Madhav", cost: 300 },{ item: "Oven", cust_name: "Amit", cost: 300 }])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("6716a1c7be373a10092f133e"),
ObjectId("6716a1c7be373a10092f133f"),
ObjectId("6716a1c7be373a10092f1340")
,
ObjectId("6716a1c7be373a10092f1341")
,
ObjectId("6716a1c7be373a10092f1342")
,
ObjectId("6716a1c7be373a10092f1343")
,
ObjectId("6716a1c7be373a10092f1344")
,
ObjectId("6716a1c7be373a10092f1345")
,
ObjectId("6716a1c7be373a10092f1346")
,
ObjectId("6716a1c7be373a10092f1347")
]
}
> db.products.find().pretty()
{
"_id" : ObjectId("6716a1c7be373a10092f133e"),
"item" : "Laptop",
"cust_name" : "Amit",
"cost" : 1200
}
{
"_id" : ObjectId("6716a1c7be373a10092f133f"),
"item" : "Smartphone",
"cust_name" : "Koustubh",
"cost" : 800
}
{
"_id" : ObjectId("6716a1c7be373a10092f1340"),
"item" : "Tablet",
"cust_name" : "Madhav",
"cost" : 600
}
{
"_id" : ObjectId("6716a1c7be373a10092f1341"),
"item" : "Headphones",
"cust_name" : "Amit",
"cost" : 150
}
{
"_id" : ObjectId("6716a1c7be373a10092f1342"),
"item" : "Smartwatch",
"cust_name" : "Koustubh",
"cost" : 250
}
{
"_id" : ObjectId("6716a1c7be373a10092f1343"),
"item" : "Monitor",
"cust_name" : "Madhav",
"cost" : 300
}
{
"_id" : ObjectId("6716a1c7be373a10092f1344"),
"item" : "Keyboard",
"cust_name" : "Amit",
"cost" : 100
}
{
"_id" : ObjectId("6716a1c7be373a10092f1345"),
"item" : "Mouse",
"cust_name" : "Koustubh",
"cost" : 50
}
{
"_id" : ObjectId("6716a1c7be373a10092f1346"),
"item" : "Fan",
"cust_name" : "Madhav",
"cost" : 300
}
{
"_id" : ObjectId("6716a1c7be373a10092f1347"),
"item" : "Oven",
"cust_name" : "Amit",
"cost" : 300
}
> db.products.aggregate([{$group: {_id: "$cust_name", totalAmount:{$sum: "$cost"}}}])
{ "_id" : "Madhav", "totalAmount" : 1200 }
{ "_id" : "Koustubh", "totalAmount" : 1100 }
{ "_id" : "Amit", "totalAmount" : 1750 }
> db.products.aggregate([{$group: {_id: "$cust_name", averageAmount: { $avg: "$cost" }}}])
{ "_id" : "Madhav", "averageAmount" : 400 }
{ "_id" : "Koustubh", "averageAmount" : 366.6666666666667 }
{ "_id" : "Amit", "averageAmount" : 437.5 }
> db.products.aggregate([{$group: {_id: "$item",totalSales: { $sum: "$cost" }}}])
{ "_id" : "Oven", "totalSales" : 300 }
{ "_id" : "Fan", "totalSales" : 300 }
{ "_id" : "Mouse", "totalSales" : 50 }
{ "_id" : "Monitor", "totalSales" : 300 }
{ "_id" : "Smartwatch", "totalSales" : 250 }
{ "_id" : "Headphones", "totalSales" : 150 }
{ "_id" : "Tablet", "totalSales" : 600 }
{ "_id" : "Smartphone", "totalSales" : 800 }
{ "_id" : "Keyboard", "totalSales" : 100 }
{ "_id" : "Laptop", "totalSales" : 1200 }
> db.products.aggregate([{$group: {_id: "$item",totalCount: { $sum: 1 } }}])
{ "_id" : "Oven", "totalCount" : 1 }
{ "_id" : "Fan", "totalCount" : 1 }
{ "_id" : "Mouse", "totalCount" : 1 }
{ "_id" : "Monitor", "totalCount" : 1 }
{ "_id" : "Smartwatch", "totalCount" : 1 }
{ "_id" : "Headphones", "totalCount" : 1 }
{ "_id" : "Tablet", "totalCount" : 1 }
{ "_id" : "Smartphone", "totalCount" : 1 }
{ "_id" : "Keyboard", "totalCount" : 1 }
{ "_id" : "Laptop", "totalCount" : 1 }
> db.products.aggregate([{$group: {_id: "$cust_name",maxBilling: { $max: "$cost" },minBilling:
{ $min: "$cost" }}}])
{ "_id" : "Madhav", "maxBilling" : 600, "minBilling" : 300 }
{ "_id" : "Koustubh", "maxBilling" : 800, "minBilling" : 50 }
{ "_id" : "Amit", "maxBilling" : 1200, "minBilling" : 100 }
> db.products.aggregate([{$sort: { cust_name: 1, _id: 1 }},{$group: {_id:
"$cust_name",firstBillingAmount: { $first: "$cost" },lastBillingAmount: { $last: "$cost"
}}}])
{ "_id" : "Madhav", "firstBillingAmount" : 600, "lastBillingAmount" : 300 }
{ "_id" : "Koustubh", "firstBillingAmount" : 800, "lastBillingAmount" : 50 }
{ "_id" : "Amit", "firstBillingAmount" : 1200, "lastBillingAmount" : 300 }
>

You might also like