DatabaseManagementSystems/Practical/Assignment-B2/Queries-B2.md

3.7 KiB
Executable File

Queries-B2

Group A

![NOTE] Use Employee database created in Assignment B-01 and perform following aggregation operation Refer Queries-B1

  1. Return Designation with Total Salary Above 200000:
db.Employee.aggregate([
    {
        $group: {
            _id: "$Designation",
            TotalSalary: { $sum: "$Salary" }
        }
    },
    {
        $match: {
            TotalSalary: { $gt: 200000 }
        }
    }
])

  1. Find Employee with Total Salary for Each City with Designation "DBA":
db.Employee.aggregate([
    {
        $match: { Designation: "DBA" }
    },
    {
        $group: {
            _id: { $arrayElemAt: ["$Address.PAddr", 0] },
            TotalSalary: { $sum: "$Salary" }
        }
    }
])

  1. Find Total Salary of Employee with Designation "DBA" for Each Company:
db.Employee.aggregate([
    {
        $match: { Designation: "DBA" }
    },
    {
        $group: {
            _id: "$Company_name",
            TotalSalary: { $sum: "$Salary" }
        }
    }
])

  1. Returns Names and _id in Upper Case and in Alphabetical Order:
db.Employee.aggregate([
    {
        $project: {
            _id: 1,
            Name: { $toUpper: { $concat: ["$Name.FName", " ", "$Name.LName"] } }
        }
    },
    {
        $sort: { Name: 1 }
    }
])

  1. Count All Records from Collection:
db.Employee.countDocuments()

  1. For Each Unique Designation, Find Avg Salary and Output Sorted by AvgSal:
db.Employee.aggregate([
    {
        $group: {
            _id: "$Designation",
            AvgSalary: { $avg: "$Salary" }
        }
    },
    {
        $sort: { AvgSalary: 1 }
    }
])

  1. Return Separate Value in the Expertise Array Where Name of Employee is "Swapnil":
db.Employee.aggregate([
    {
        $match: { "Name.FName": "Swapnil" }
    },
    {
        $unwind: "$Expertise"
    },
    {
        $project: { Expertise: 1 }
    }
])

  1. Return Separate Value in the Expertise Array and Return Sum of Each Element of Array:
db.Employee.aggregate([
    {
        $unwind: "$Expertise"
    },
    {
        $group: {
            _id: "$Expertise",
            TotalCount: { $sum: 1 }
        }
    }
])

  1. Return Array for Designation Whose Address is "Pune":
db.Employee.aggregate([
    {
        $match: { "Address.PAddr": { $regex: "Pune" } }
    },
    {
        $project: { Designation: 1 }
    }
])

  1. Return Max and Min Salary for Each Company:
db.Employee.aggregate([
    {
        $group: {
            _id: "$Company_name",
            MaxSalary: { $max: "$Salary" },
            MinSalary: { $min: "$Salary" }
        }
    }
])

Group B

![NOTE] Use Employee database created in Assignment B-01 and perform following aggregation operation Refer Queries-B1

  1. Create Single Field Indexes on Designation:
db.Employee.createIndex({ Designation: 1 })

  1. Create Compound Indexes on Name and Age:
db.Employee.createIndex({ "Name.FName": 1, Age: -1 })

  1. Create Multikey Indexes on Expertise Array:
db.Employee.createIndex({ Expertise: 1 })

  1. Return a List of All Indexes on Collection:
db.Employee.getIndexes()

  1. Rebuild Indexes:
db.Employee.reIndex()

  1. Drop Index on Remove Specific Index:
db.Employee.dropIndex("empIndex")

  1. Remove All Indexes Except for the _id Index from a Collection:
db.Employee.dropIndexes()