0% found this document useful (0 votes)
6 views3 pages

B 1

The document details a MongoDB session where a user connects to a local MongoDB instance and performs various operations on a database named 'btlab3b1'. It includes inserting sales data, aggregating total revenue and quantity by category, and filtering results based on revenue thresholds. The results show that 'Electronics' generates the highest revenue and quantity among the categories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

B 1

The document details a MongoDB session where a user connects to a local MongoDB instance and performs various operations on a database named 'btlab3b1'. It includes inserting sales data, aggregating total revenue and quantity by category, and filtering results based on revenue thresholds. The results show that 'Electronics' generates the highest revenue and quantity among the categories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

PS C:\Users\phung> mongosh

Current Mongosh Log ID: 67ea8fbcd2523b1f09b71235


Connecting to: mongodb://127.0.0.1:27017/?
directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.4.2
Using MongoDB: 8.0.6
Using Mongosh: 2.4.2

For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/

------
The server generated these startup warnings when booting
2025-03-27T23:50:20.213+07:00: Access control is not enabled for the database.
Read and write access to data and configuration is unrestricted
------

test> use btlab3b1


switched to db btlab3b1
btlab3b1> db.sales.insertMany([
... { "_id": 1, "product": "Laptop", "category": "Electronics", "price": 1000,
"quantity": 2, "date": "2024-01-01" },
... { "_id": 2, "product": "Phone", "category": "Electronics", "price": 500,
"quantity": 5, "date": "2024-01-02" },
... { "_id": 3, "product": "Desk", "category": "Furniture", "price": 200,
"quantity": 3, "date": "2024-01-02" },
... { "_id": 4, "product": "Chair", "category": "Furniture", "price": 150,
"quantity": 4, "date": "2024-01-03" },
... { "_id": 5, "product": "Phone", "category": "Electronics", "price": 500,
"quantity": 3, "date": "2024-01-04" }
... ]);
{
acknowledged: true,
insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
}
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$category",
... totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } },
... totalQuantity: { $sum: "$quantity" }
... }
... }
... ]);
[
{ _id: 'Electronics', totalRevenue: 6000, totalQuantity: 10 },
{ _id: 'Furniture', totalRevenue: 1200, totalQuantity: 7 }
]
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$category",
... totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } }
... }
... },
... { $sort: { totalRevenue: -1 } },
... { $limit: 1 }
... ]);
[ { _id: 'Electronics', totalRevenue: 6000 } ]
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$category",
... totalQuantity: { $sum: "$quantity" }
... }
... },
... { $sort: { totalQuantity: -1 } },
... { $limit: 1 }
... ]);
[ { _id: 'Electronics', totalQuantity: 10 } ]
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$date",
... totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } }
... }
... }
... ]);
[
{ _id: '2024-01-03', totalRevenue: 600 },
{ _id: '2024-01-02', totalRevenue: 3100 },
{ _id: '2024-01-01', totalRevenue: 2000 },
{ _id: '2024-01-04', totalRevenue: 1500 }
]
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$date",
... totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } }
... }
... },
... { $match: { totalRevenue: { $gt: 2000 } } }
... ]);
[ { _id: '2024-01-02', totalRevenue: 3100 } ]
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$category",
... totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } },
... totalQuantity: { $sum: "$quantity" }
... }
... },
... { $sort: { totalRevenue: -1 } }
... ]);
...
[
{ _id: 'Electronics', totalRevenue: 6000, totalQuantity: 10 },
{ _id: 'Furniture', totalRevenue: 1200, totalQuantity: 7 }
]
btlab3b1> db.sales.aggregate([
... {
... $group: {
... _id: "$category",
... totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } },
... totalQuantity: { $sum: "$quantity" }
... }
... },
... { $sort: { totalRevenue: -1 } },
... {
... $project: {
... _id: 0,
... category: "$_id",
... totalRevenue: 1,
... totalQuantity: 1
... }
... }
... ]);
...
[
{ totalRevenue: 6000, totalQuantity: 10, category: 'Electronics' },
{ totalRevenue: 1200, totalQuantity: 7, category: 'Furniture' }
]

You might also like