0% found this document useful (0 votes)
14 views12 pages

Mon God B Aggregate

The document shows operations performed on a MongoDB database containing product inventory data. Several products are inserted into a collection called 'item' including details like customer name, product name, quantity and price. Indexes are created and dropped on different fields. Aggregation operations are then performed to calculate statistics like the average price and total sales for each customer, maximum and minimum prices, total quantities sold for each product, etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Mon God B Aggregate

The document shows operations performed on a MongoDB database containing product inventory data. Several products are inserted into a collection called 'item' including details like customer name, product name, quantity and price. Indexes are created and dropped on different fields. Aggregation operations are then performed to calculate statistics like the average price and total sales for each customer, maximum and minimum prices, total quantities sold for each product, etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

test> use shruti_college

switched to db shruti_college

shruti_college> db.createCollection('item')
{ ok: 1 }

shruti_college>
db.item.insertOne({cname:'Shruti',pname:'Macbook Air
M2',qun:1,price:75000})
{
acknowledged: true,
insertedId: ObjectId("6538ca960f355325123d0a28")
}

shruti_college>
db.item.insertOne({cname:'Aniket',pname:'DELL
Gaming',qun:1,price:80000})
{
acknowledged: true,
insertedId: ObjectId("6538caba0f355325123d0a29")
}

shruti_college>
db.item.insertMany([{cname:'Aniket',pname:'Keyboard',qun:
1,price:1000},
{cname:'Aniket',pname:'Mouse',qun:1,price:500},
{cname:'Pratiksha',pname:'ASUS
Gaming',qun:1,price:83000},{cname:'Prajval',pname:'HP
Professional',qun:1,price:90000},
{cname:'Shruti',pname:'Headphones',qun:1,price:1200}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("6538cb740f355325123d0a2a"),
'1': ObjectId("6538cb740f355325123d0a2b"),
'2': ObjectId("6538cb740f355325123d0a2c"),
'3': ObjectId("6538cb740f355325123d0a2d"),
'4': ObjectId("6538cb740f355325123d0a2e")
}
}

shruti_college>
db.item.insertMany([{cname:'Prajval',pname:'Keyboard',qun
:4,price:1250},
{cname:'Prajval',pname:'Mouse',qun:4,price:800},
{cname:'Pratiksha',pname:'Bluetooth
Speaker',qun:2,price:8500},
{cname:'Pratiksha',pname:'Macbook Air
M1',qun:10,price:90000},
{cname:'Aniket',pname:'Headphones',qun:10,price:1200}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("6538cc790f355325123d0a2f"),
'1': ObjectId("6538cc790f355325123d0a30"),
'2': ObjectId("6538cc790f355325123d0a31"),
'3': ObjectId("6538cc790f355325123d0a32"),
'4': ObjectId("6538cc790f355325123d0a33")
}
}

shruti_college> db.item.find()
[
{
_id: ObjectId("6538ca960f355325123d0a28"),
cname: 'Shruti',
pname: 'Macbook Air M2',
qun: 1,
price: 75000
},
{
_id: ObjectId("6538caba0f355325123d0a29"),
cname: 'Aniket',
pname: 'DELL Gaming',
qun: 1,
price: 80000
},
{
_id: ObjectId("6538cb740f355325123d0a2a"),
cname: 'Aniket',
pname: 'Keyboard',
qun: 1,
price: 1000
},
{
_id: ObjectId("6538cb740f355325123d0a2b"),
cname: 'Aniket',
pname: 'Mouse',
qun: 1,
price: 500
},
{
_id: ObjectId("6538cb740f355325123d0a2c"),
cname: 'Pratiksha',
pname: 'ASUS Gaming',
qun: 1,
price: 83000
},
{
_id: ObjectId("6538cb740f355325123d0a2d"),
cname: 'Prajval',
pname: 'HP Professional',
qun: 1,
price: 90000
},
{
_id: ObjectId("6538cb740f355325123d0a2e"),
cname: 'Shruti',
pname: 'Headphones',
qun: 1,
price: 1200
},
{
_id: ObjectId("6538cc790f355325123d0a2f"),
cname: 'Prajval',
pname: 'Keyboard',
qun: 4,
price: 1250
},
{
_id: ObjectId("6538cc790f355325123d0a30"),
cname: 'Prajval',
pname: 'Mouse',
qun: 4,
price: 800
},
{
_id: ObjectId("6538cc790f355325123d0a31"),
cname: 'Pratiksha',
pname: 'Bluetooth Speaker',
qun: 2,
price: 8500
},
{
_id: ObjectId("6538cc790f355325123d0a32"),
cname: 'Pratiksha',
pname: 'Macbook Air M1',
qun: 10,
price: 90000
},
{
_id: ObjectId("6538cc790f355325123d0a33"),
cname: 'Aniket',
pname: 'Headphones',
qun: 10,
price: 1200
}
]
List all items whose price is greater than 45000

shruti_college> db.item.aggregate([{$match:{price:
{$gt:45000}}}])
[
{
_id: ObjectId("6538ca960f355325123d0a28"),
cname: 'Shruti',
pname: 'Macbook Air M2',
qun: 1,
price: 75000
},
{
_id: ObjectId("6538caba0f355325123d0a29"),
cname: 'Aniket',
pname: 'DELL Gaming',
qun: 1,
price: 80000
},
{
_id: ObjectId("6538cb740f355325123d0a2c"),
cname: 'Pratiksha',
pname: 'ASUS Gaming',
qun: 1,
price: 83000
},
{
_id: ObjectId("6538cb740f355325123d0a2d"),
cname: 'Prajval',
pname: 'HP Professional',
qun: 1,
price: 90000
},
{
_id: ObjectId("6538cc790f355325123d0a32"),
cname: 'Pratiksha',
pname: 'Macbook Air M1',
qun: 10,
price: 90000
}
]

Sort the details in ascending order

shruti_college> db.item.aggregate([{$sort:{qun:1}}])
[
{
_id: ObjectId("6538ca960f355325123d0a28"),
cname: 'Shruti',
pname: 'Macbook Air M2',
qun: 1,
price: 75000
},
{
_id: ObjectId("6538caba0f355325123d0a29"),
cname: 'Aniket',
pname: 'DELL Gaming',
qun: 1,
price: 80000
},
{
_id: ObjectId("6538cb740f355325123d0a2a"),
cname: 'Aniket',
pname: 'Keyboard',
qun: 1,
price: 1000
},
{
_id: ObjectId("6538cb740f355325123d0a2b"),
cname: 'Aniket',
pname: 'Mouse',
qun: 1,
price: 500
},
{
_id: ObjectId("6538cb740f355325123d0a2c"),
cname: 'Pratiksha',
pname: 'ASUS Gaming',
qun: 1,
price: 83000
},
{
_id: ObjectId("6538cb740f355325123d0a2d"),
cname: 'Prajval',
pname: 'HP Professional',
qun: 1,
price: 90000
},
{
_id: ObjectId("6538cb740f355325123d0a2e"),
cname: 'Shruti',
pname: 'Headphones',
qun: 1,
price: 1200
},
{
_id: ObjectId("6538cc790f355325123d0a31"),
cname: 'Pratiksha',
pname: 'Bluetooth Speaker',
qun: 2,
price: 8500
},
{
_id: ObjectId("6538cc790f355325123d0a2f"),
cname: 'Prajval',
pname: 'Keyboard',
qun: 4,
price: 1250
},
{
_id: ObjectId("6538cc790f355325123d0a30"),
cname: 'Prajval',
pname: 'Mouse',
qun: 4,
price: 800
},
{
_id: ObjectId("6538cc790f355325123d0a32"),
cname: 'Pratiksha',
pname: 'Macbook Air M1',
qun: 10,
price: 90000
},
{
_id: ObjectId("6538cc790f355325123d0a33"),
cname: 'Aniket',
pname: 'Headphones',
qun: 10,
price: 1200
}
]

Create and Drop indexes

shruti_college> db.item.createIndex({quantity:1})
quantity_1
shruti_college> db.item.getIndexes()
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { quantity: 1 }, name: 'quantity_1' }
]
shruti_college> db.item.createIndex({'customer':1})
customer_1
shruti_college> db.item.getIndexes()
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { quantity: 1 }, name: 'quantity_1' },
{ v: 2, key: { customer: 1 }, name: 'customer_1' }
]
shruti_college> db.item.dropIndex({'customer':1})
{ nIndexesWas: 3, ok: 1 }
shruti_college> db.item.getIndexes()
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { quantity: 1 }, name: 'quantity_1' }
]

Calculate the average price of products purchased by each


customer.

shruti_college> db.item.aggregate([ { $group: { _id:


"$cname", averagePrice: { $avg: "$price" } } }] )
[
{ _id: 'Aniket', averagePrice: 20675 },
{ _id: 'Pratiksha', averagePrice: 60500 },
{ _id: 'Shruti', averagePrice: 38100 },
{ _id: 'Prajval', averagePrice: 30683.333333333332 }
]

Calculate the total sales for each customer.

shruti_college> db.item.aggregate([ { $group: { _id:


"$cname", totalSales: { $sum: { $multiply: ["$qun",
"$price"] } } } }] )
[
{ _id: 'Aniket', totalSales: 93500 },
{ _id: 'Pratiksha', totalSales: 1000000 },
{ _id: 'Prajval', totalSales: 98200 },
{ _id: 'Shruti', totalSales: 76200 }
]

Find the maximum price among all products.

shruti_college> db.item.aggregate([ { $group: { _id:


null, maxPrice: { $max: "$price" } } }] )
[ { _id: null, maxPrice: 90000 } ]
Calculate the total quantity of each product sold.

shruti_college> db.item.aggregate([
... {
... $group: {
... _id: "$pname",
... totalQuantitySold: { $sum: "$qun" }
... }
... }
... ])
[
{ _id: 'Bluetooth Speaker', totalQuantitySold: 2 },
{ _id: 'HP Professional', totalQuantitySold: 1 },
{ _id: 'Keyboard', totalQuantitySold: 5 },
{ _id: 'ASUS Gaming', totalQuantitySold: 1 },
{ _id: 'Macbook Air M1', totalQuantitySold: 10 },
{ _id: 'Mouse', totalQuantitySold: 5 },
{ _id: 'Macbook Air M2', totalQuantitySold: 1 },
{ _id: 'DELL Gaming', totalQuantitySold: 1 },
{ _id: 'Headphones', totalQuantitySold: 11 }
]

Find the minimum price among all products.

shruti_college> db.item.aggregate([
... {
... $group: {
... _id: null,
... minPrice: { $min: "$price" }
... }
... }
... ])
[ { _id: null, minPrice: 500 } ]
shruti_college>
Count the number of unique customers.

shruti_college> db.item.aggregate([
... {
... $group: {
... _id: null,
... customerCount: { $addToSet: "$cname" }
... },
... },
... {
... $project: {
... customerCount: { $size: "$customerCount" }
... }
... }
... ])
[ { _id: null, customerCount: 4 } ]

Calculate the average quantity of products sold to each


customer.

shruti_college> db.item.aggregate([
... {
... $group: {
... _id: "$cname",
... averageQuantitySold: { $avg: "$qun" }
... }
... }
... ])
[
{ _id: 'Aniket', averageQuantitySold: 3.25 },
{ _id: 'Pratiksha', averageQuantitySold:
4.333333333333333 },
{ _id: 'Shruti', averageQuantitySold: 1 },
{ _id: 'Prajval', averageQuantitySold: 3 }
]
Calculate the total sales revenue for a specific
customer.

shruti_college> db.item.aggregate([
... {
... $match: { cname: "Shruti" }
... },
... {
... $group: {
... _id: null,
... totalSales: { $sum: { $multiply: ["$qun",
"$price"] } }
... }
... }
... ])
[ { _id: null, totalSales: 76200 } ]

Count the number of products sold by each customer.

shruti_college> db.item.aggregate([
... {
... $group: {
... _id: "$cname",
... productCount: { $sum: 1 }
... }
... }
... ])
[
{ _id: 'Aniket', productCount: 4 },
{ _id: 'Pratiksha', productCount: 3 },
{ _id: 'Prajval', productCount: 3 },
{ _id: 'Shruti', productCount: 2 }
]

You might also like