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

2.4 KiB
Executable File

Queries-B3

Note

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

  1. Return the Total Salary of per Company
db.Employee.mapReduce(
    function() {
        emit(this.Company_name, this.Salary);
    },
    function(key, values) {
        return Array.sum(values);
    },
    { out: "total_salary_per_company" }
);

  1. Return the Total Salary of Company Name:"Oscorp"
db.Employee.mapReduce(
    function() {
        if (this.Company_name === "Oscorp") {
            emit(this.Company_name, this.Salary);
        }
    },
    function(key, values) {
        return Array.sum(values);
    },
    { out: "total_salary_oscorp" }
);

  1. Return the Avg Salary of Company whose address is “Pune".
db.Employee.mapReduce(
    function() {
        if (this.Address.some(addr => addr.PAddr.includes("Pune"))) {
            emit("Pune", this.Salary);
        }
    },
    function(key, values) {
        return Array.avg(values);
    },
    { out: "avg_salary_pune" }
);

  1. Return the Total Salary for each Designation of Wayne Industries.
db.Employee.mapReduce(
    function() {
        if (this.Company_name === "Wayne Industries") {
            emit(this.Designation, this.Salary);
        }
    },
    function(key, values) {
        return Array.sum(values);
    },
    { out: "total_salary_wayne" }
);

  1. Return total count for “State=Maharashtra”
db.Employee.mapReduce(
    function() {
        this.Address.forEach(function(addr) {
            if (addr.LAddr === "Maharashtra") {
                emit("Maharashtra", 1);
            }
        });
    },
    function(key, values) {
        return Array.sum(values);
    },
    { out: "count_state_maharashtra" }
);

  1. Return Count for State Telangana and Age greater than 40.
db.Employee.mapReduce(
    function() {
        if (this.Age > 40) {
            this.Address.forEach(function(addr) {
                if (addr.LAddr === "Telangana") {
                    emit("Telangana_Age_Above_40", 1);
                }
            });
        }
    },
    function(key, values) {
        return Array.sum(values);
    },
    { out: "count_telangana_age_above_40" }
);