2. IMPLEMENTATION OF PRODUCT AND ADVANCED CRUD OPERATIONS USING MONGODB

{
CRUD OPERATIONS USING MONGODB
                                                                    ProductID: 103, ProductName: "Wireless Mouse", Brand:
1. Database creation :-                                           "Logitech", Category: "Accessories", Price: 1200, Stock: 80,
                                                                  Rating: 4.4,
use ProductCatalog
                                                                          Supplier: { Name: "Computer Hub", City: "Madurai" },
Output:
                                                                          Specifications: { Connectivity: "Bluetooth", Battery: "AA" },
switched to db ProductCatalog
                                                                          Tags: ["Mouse", "Wireless"], Available: true
                                                                      },
2. Checking Currently Selected Database :-
                                                                      {
db
                                                                    ProductID: 104, ProductName: "Keyboard", Brand: "HP",
Output:                                                           Category: "Accessories", Price: 1800, Stock: 315, Rating: 4.2,
ProductCatalog                                                            Supplier: { Name: "Computer Hub", City: "Madurai" },
                                                                          Specifications: { Type: "Mechanical" },
3. Collection Creation :-                                                 Tags: ["Keyboard"], Available: true
db.createCollection("Products")                                       },
Output:                                                               {
{ "ok" : 1 }                                                        ProductID: 105, ProductName: "Monitor", Brand: "LG",
                                                                  Category: "Electronics", Price: 15000, Stock: 20, Rating: 4.5,

4. Checking Created Collections :-                                        Supplier: { Name: "Vision Tech", City: "Salem" },

show collections                                                          Specifications: { Size: "24 Inch", Resolution: "4K" },

Output:                                                                   Tags: ["Display", "Monitor"], Available: false

Products                                                              }
                                                                  ])

5. Inserting Product Documents :-                                 Output:

db.Products.insertMany([                                          {

 {                                                                    "acknowledged" : true,

  ProductID: 101, ProductName: "Laptop", Brand: "Dell",            "insertedIds" : [ ObjectId("..."), ObjectId("..."), ObjectId("..."),
Category: "Electronics", Price: 65000, Stock: 25, Rating: 4.7,    ObjectId("..."), ObjectId("...") ]

     Supplier: { Name: "Tech World", City: "Chennai" },           }

  Specifications: { RAM: "16GB", Storage: "512GB SSD",
Processor: "Intel i7" },                                          6. Display All products :-
     Tags: ["Laptop", "office", "Electronics"], Available: true   db.Products.find()
 },                                                               Output:
 {                                                                { "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
  ProductID: 102, ProductName: "Smartphone", Brand:               "Laptop", "Price" : 65000, "Brand" : "Dell", ... }
"Samsung", Category: "Electronics", Price: 35000, Stock: 40,      { "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
Rating: 4.6,                                                      "Smartphone", "Price" : 35000, "Brand" : "Samsung", ... }
     Supplier: { Name: "Mobile Zone", City: "Coimbatore" },       { "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :
  Specifications: { RAM: "8GB", Storage: "256GB", Processor:      "Wireless Mouse", "Price" : 1200, "Brand" : "Logitech", ... }
"Snapdragon" },                                                   { "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :
     Tags: ["Android", "5G", "Mobile"], Available: true           "Keyboard", "Price" : 1800, "Brand" : "HP", ... }

 },                                                               { "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
                                                                  "Monitor", "Price" : 15000, "Brand" : "LG", ... }
7. Display One product :-                                         9. READ Operations: Display Product Name and Price Only :-
db.Products.findOne()                                             db.Products.find({}, { _id: 0, ProductName: 1, Price: 1 })
Output:                                                           Output:
{                                                                 { "ProductName" : "Laptop", "Price" : 65000 }
    "_id" : ObjectId("..."),                                      { "ProductName" : "Smartphone", "Price" : 35000 }
    "ProductID" : 101,                                            { "ProductName" : "Wireless Mouse", "Price" : 1200 }
    "ProductName" : "Laptop",                                     { "ProductName" : "Keyboard", "Price" : 1800 }
    "Brand" : "Dell",                                             { "ProductName" : "Monitor", "Price" : 15000 }
    "Category" : "Electronics",                                   { "ProductName" : "Printer", "Price" : 12000 }
    "Price" : 65000,
    "Stock" : 25,                                                 10. Product Costing More than ₹20,000 :-
    "Rating" : 4.7,                                               db.Products.find({ Price: { $gt: 20000 } })
    "Supplier" : { "Name" : "Tech World", "City" : "Chennai" },   Output:
 "Specifications" : { "RAM" : "16GB", "Storage" : "512GB SSD",    { "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
"Processor" : "Intel i7" },                                       "Laptop", "Price" : 65000, ... }
    "Tags" : [ "Laptop", "office", "Electronics" ],               { "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
                                                                  "Smartphone", "Price" : 35000, ... }
    "Available" : true
}
                                                                  11. Product Between ₹5000 and ₹40000 :-
                                                                  db.Products.find({ Price: { $gte: 5000, $lte: 40000 } })
8. CREATE Operations - Insert One Product :-
                                                                  Output:
db.Products.insertOne({
                                                                  { "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
    ProductID: 106,                                               "Smartphone", "Price" : 35000, ... }
    ProductName: "Printer",                                       { "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
    Brand: "Canon",                                               "Monitor", "Price" : 15000, ... }

    Category: "Office",                                           { "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :
                                                                  "Printer", "Price" : 12000, ... }
    Price: 12000,
    Stock: 15,
                                                                  12. Electronics Products :-
    Rating: 4.3,
                                                                  db.Products.find({ Category: "Electronics" })
    Supplier: { Name: "Print Solutions", City: "Trichy" },
                                                                  Output:
    Specifications: { Type: "Laser" },
                                                                  { "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
    Tags: ["Printer"],                                            "Laptop", "Category" : "Electronics", ... }

 Available: true                                                  { "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
                                                                  "Smartphone", "Category" : "Electronics", ... }
})
                                                                  { "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
Output:                                                           "Monitor", "Category" : "Electronics", ... }
{
    "acknowledged" : true,                                        13. Available Products :-
    "insertedId" : ObjectId("...")                                db.Products.find({ Available: true })
}                                                                 Output:
{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :     18. Using AND Operator :-
"Laptop", "Available" : true, ... }
                                                                  db.Products.find({ $and: [ { Category: "Electronics" }, { Price: {
{ "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :     $gt: 30000 } } ] })
"Smartphone", "Available" : true, ... }
                                                                  Output:
{ "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :
"Wireless Mouse", "Available" : true, ... }                       { "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
                                                                  "Laptop", "Category" : "Electronics", "Price" : 65000, ... }
{ "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :
"Keyboard", "Available" : true, ... }                             { "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
                                                                  "Smartphone", "Category" : "Electronics", "Price" : 35000, ... }
{ "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :
"Printer", "Available" : true, ... }
                                                                  19. Using OR Operator :-

14. Product Having Rating Greater than 4.5 :-                     db.Products.find({ $or: [ { Brand: "Dell" }, { Brand: "LG" } ] })

db.Products.find({ Rating: { $gt: 4.5 } })                        Output:

Output:                                                           { "_id" : ObjectId("..."), "ProductID" : 101, "Brand" : "Dell", ... }

{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :     { "_id" : ObjectId("..."), "ProductID" : 105, "Brand" : "LG", ... }
"Laptop", "Rating" : 4.7, ... }
{ "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :     20. Using NOT Operator :-
"Smartphone", "Rating" : 4.6, ... }
                                                                  db.Products.find({ Price: { $not: { $gt: 20000 } } })
                                                                  Output:
15. Product with stock less than 30 :-
                                                                  { "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :
db.Products.find({ Stock: { $lt: 30 } })                          "Wireless Mouse", "Price" : 1200, ... }
Output:                                                           { "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :
{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :     "Keyboard", "Price" : 1800, ... }
"Laptop", "Stock" : 25, ... }                                     { "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
{ "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :     "Monitor", "Price" : 15000, ... }
"Monitor", "Stock" : 20, ... }                                    { "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :
{ "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :     "Printer", "Price" : 12000, ... }
"Printer", "Stock" : 15, ... }

                                                                  21. Sorting Price in Ascending Order :-
16. Product by Supplier City :-                                   db.Products.find().sort({ Price: 1 })
db.Products.find({ "Supplier.City": "Madurai" })                  Output:
Output:                                                           { "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :
{ "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :     "Wireless Mouse", "Price" : 1200, ... }
"Wireless Mouse", "Supplier" : { "Name" : "Computer Hub",         { "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :
"City" : "Madurai" }, ... }                                       "Keyboard", "Price" : 1800, ... }
{ "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :     { "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :
"Keyboard", "Supplier" : { "Name" : "Computer Hub", "City" :      "Printer", "Price" : 12000, ... }
"Madurai" }, ... }
                                                                  { "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
                                                                  "Monitor", "Price" : 15000, ... }
17. Product Having Tag "Laptop" :-                                { "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
db.Products.find({ Tags: "Laptop" })                              "Smartphone", "Price" : 35000, ... }

Output:                                                           { "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
                                                                  "Laptop", "Price" : 65000, ... }
{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
"Laptop", "Tags" : [ "Laptop", "office", "Electronics" ], ... }
                                                                  22. Sorting Price in Descending Order :-
db.Products.find().sort({ Price: -1 })                             db.Products.updateOne({ ProductID: 102 }, { $set: { Stock: 55 }
                                                                   })
Output:
                                                                   Output:
{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
"Laptop", "Price" : 65000, ... }                                   { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1
                                                                   }
{ "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :
"Smartphone", "Price" : 35000, ... }
{ "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :      27. Increase Price by ₹5000 :-
"Monitor", "Price" : 15000, ... }
                                                                   db.Products.updateMany({ Category: "Electronics" }, { $inc: {
{ "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :      Price: 5000 } })
"Printer", "Price" : 12000, ... }
                                                                   Output:
{ "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :
"Keyboard", "Price" : 1800, ... }                                  { "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3
                                                                   }
{ "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :
"Wireless Mouse", "Price" : 1200, ... }
                                                                   28. Rename Brand :-

23. Top three Expensive Products :-                                db.Products.updateMany(

db.Products.find().sort({ Price: -1 }).limit(3)                    { Brand: "HP" },

Output:                                                            { $set: { Brand: "HP India" } }

{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :      )
"Laptop", "Price" : 65000, ... }                                   Output:
{ "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :      { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1
"Smartphone", "Price" : 35000, ... }                               }
{ "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
"Monitor", "Price" : 15000, ... }
                                                                   29. Add New Tag :-
                                                                   db.Products.updateOne(
24. Skip First two Products :-
                                                                   { ProductID: 101 },
db.Products.find().skip(2)
                                                                   { $push: { Tags: "Gaming" } }
Output:
                                                                   )
{ "_id" : ObjectId("..."), "ProductID" : 103, "ProductName" :
"Wireless Mouse", ... }                                            Output:
{ "_id" : ObjectId("..."), "ProductID" : 104, "ProductName" :      { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1
"Keyboard", ... }                                                  }
{ "_id" : ObjectId("..."), "ProductID" : 105, "ProductName" :
"Monitor", ... }
                                                                   30. Add multiple Tags :-
{ "_id" : ObjectId("..."), "ProductID" : 106, "ProductName" :
"Printer", ... }                                                   db.Products.updateOne(
                                                                   { ProductID: 102 },

25. UPDATE Operations: Update Single Product Price :-              { $push: { Tags: { $each: ["AMOLED", "Flagship"] } } }

db.Products.updateOne({ ProductID: 101 }, { $set: { Price: 70000   )
} })
                                                                   Output:
Output:
                                                                   { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1   }
}

                                                                   31. Remove a Tag :-
26. Update Stock :-
                                                                   db.Products.updateOne(
{ ProductID: 102 },                                                db.Products.aggregate([
{ $pull: { Tags: "5G" } }                                          { $group: { _id: "$Category", AveragePrice: { $avg: "$Price" } } }
)                                                                  ])
Output:                                                            Output:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1   { "_id" : "Electronics", "AveragePrice" : 57500 }
}
                                                                   { "_id" : "Accessories", "AveragePrice" : 1500 }


32. Rename Field :-
                                                                   38. Maximum Price Category Wise :-
db.Products.updateMany(
                                                                   db.Products.aggregate([
{},
                                                                   { $group: { _id: "$Category", MaximumPrice: { $max: "$Price" }
{ $rename: { Rating: "Customer Rating" } }                         }}
)                                                                  ])
Output:                                                            Output:
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6   { "_id" : "Electronics", "MaximumPrice" : 75000 }
}
                                                                   { "_id" : "Accessories", "MaximumPrice" : 1800 }


33. Delete One Product :-
                                                                   39. Minimum Price Category Wise :-
db.Products.deleteOne({ ProductID: 108 })
                                                                   db.Products.aggregate([
Output:
                                                                   { $group: { _id: "$Category", MinimumPrice: { $min: "$Price" } }
{ "acknowledged" : true, "deletedCount" : 0 }                      }
                                                                   ])
34. Delete Multiple Products :-                                    Output:
db.Products.deleteMany({ Available: false })                       { "_id" : "Electronics", "MinimumPrice" : 40000 }
Output:                                                            { "_id" : "Accessories", "MinimumPrice" : 1200 }
{ "acknowledged" : true, "deletedCount" : 1 }
                                                                   40. Number of Products in Each Category :-
35. Delete Products Having stock less than 20 :-                   db.Products.aggregate([
db.Products.deleteMany({ Stock: { $lt: 20 } })                     { $group: { _id: "$Category", NumberofProducts: { $sum: 1 } } }
Output:                                                            ])
{ "acknowledged" : true, "deletedCount" : 1 }                      Output:
                                                                   { "_id" : "Electronics", "NumberofProducts" : 2 }
36. Total Price Category Wise :-                                   { "_id" : "Accessories", "NumberofProducts" : 2 }
db.Products.aggregate([
{ $group: { _id: "$Category", TotalPrice: { $sum: "$Price" } } }   41. Average Rating Brand Wise :-
])                                                                 db.Products.aggregate([
Output:                                                            { $group: { _id: "$Brand", AverageRating: { $avg: "$Customer
                                                                   Rating" } } }
{ "_id" : "Electronics", "TotalPrice" : 115000 }
                                                                   ])
{ "_id" : "Accessories", "TotalPrice" : 3000 }
                                                                   Output:
                                                                   { "_id" : "Dell", "AverageRating" : 4.7 }
37. Average Price Category Wise :-
{ "_id" : "Samsung", "AverageRating" : 4.6 }                          db.Products.aggregate([
{ "_id" : "Logitech", "AverageRating" : 4.4 }                         { $group: { _id: "$Supplier.City", Products: { $push:
                                                                      "$ProductName" } } }
{ "_id" : "HP India", "AverageRating" : 4.2 }
                                                                      ])
42. Total Stock Brand Wise :-
                                                                      Output:
db.Products.aggregate([
                                                                      { "_id" : "Chennai", "Products" : [ "Laptop" ] }
{ $group: { _id: "$Brand", TotalStock: { $sum: "$Stock" } } }
                                                                      { "_id" : "Coimbatore", "Products" : [ "Smartphone" ] }
])
                                                                      { "_id" : "Madurai", "Products" : [ "Wireless Mouse", "Keyboard"
Output:                                                               ]}
{ "_id" : "Dell", "TotalStock" : 25 }
{ "_id" : "Samsung", "TotalStock" : 55 }                              46. First Product in Each Category :-
{ "_id" : "Logitech", "TotalStock" : 80 }                             db.Products.aggregate([
{ "_id" : "HP India", "TotalStock" : 315 }                            { $group: { _id: "$Category", FirstProduct: { $first:
                                                                      "$ProductName" } } }

43. Product with Price Greater than ₹20,000 and Sort :-               ])

db.Products.aggregate([                                               Output:

{ $match: { Price: { $gt: 20000 } } },                                { "_id" : "Electronics", "FirstProduct" : "Laptop" }

{ $sort: { Price: -1 } }                                              { "_id" : "Accessories", "FirstProduct" : "Wireless Mouse" }

])
Output:                                                               47. Last Product in each Category :-

{ "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :         db.Products.aggregate([
"Laptop", "Brand" : "Dell", "Price" : 75000, "Customer Rating":       { $group: { _id: "$Category", LastProduct: { $last:
4.7, ... }                                                            "$ProductName" } } }
{ "_id" : ObjectId("..."), "ProductID" : 102, "ProductName" :         ])
"Smartphone", "Brand" : "Samsung", "Price" : 40000, "Customer
Rating": 4.6, ... }                                                   Output:
                                                                      { "_id" : "Electronics", "LastProduct" : "Smartphone" }
44. Project Required Fields :-                                        { "_id" : "Accessories", "LastProduct" : "Keyboard" }
db.Products.aggregate([
{ $project: { _id: 0, ProductName: 1, Brand: 1, Price: 1, Category:   48. Highest Rated Product :-
1}}
                                                                      db.Products.aggregate([
])
                                                                      { $sort: { "Customer Rating": -1 } },
Output:
                                                                      { $limit: 1 }
{ "ProductName" : "Laptop", "Brand" : "Dell", "Category" :
"Electronics", "Price" : 75000 }                                      ])

{ "ProductName" : "Smartphone", "Brand" : "Samsung",                  Output:
"Category" : "Electronics", "Price" : 40000 }                         { "_id" : ObjectId("..."), "ProductID" : 101, "ProductName" :
{ "ProductName" : "Wireless Mouse", "Brand" : "Logitech",             "Laptop", "Customer Rating" : 4.7, ... }
"Category" : "Accessories", "Price" : 1200 }
{ "ProductName" : "Keyboard", "Brand" : "HP India", "Category"        49. Unwind Tags :-
: "Accessories", "Price" : 1800 }
                                                                      db.Products.aggregate([
                                                                      { $unwind: "$Tags" }
45. Group Products by Supplier City :-
                                                                      ])
Output:
