Beispiel: Queries in MongoDB

Sei folgendes Beispiel gegeben:

In MongoDB können wir die folgenden Queries wie folgt lösen:

Anzahl Projekte pro Department:

db.Project.aggregate([
        {
            $group: {
                _id: "$department.name",
                projectCount: {$sum: 1}
            }
        },
]);

Durchschnittliche Dauer aller Projekte:

db.Project.aggregate([
        {
            $project: {
                durationInDays: {
                    $dateDiff: {
                        startDate: "$start_date",
                        endDate: "$end_date",
                        unit: "day"
                    },
                },
            }
        },
        {
            $group: {
                _id: null,
                avgProjectDurationDays : {
                    $avg: "$durationInDays"
                }
            }
        },
        {
            $project: {
                _id: 0
            }
        }
 
]);

Durchschnittliche Projektdauer pro Department:

db.Project.aggregate([
        {
            $project: {
                name: "$department.name",
                durationInDays: {
                    $dateDiff: {
                        startDate: "$start_date",
                        endDate: "$end_date",
                        unit: "day"
                    },
                },
            }
        },
        {
            $group: {
                _id: "$name",
                avgProjectDurationDays: {
                    $avg: "$durationInDays"
                }
            }
        }
]);

Durchschnittliche Satisfaction pro Department:

db.Department.aggregate([
        {
            $unwind: "$employees"
        },
        {
            $project: {
                name: "$name",
                satisfied: "$employees.satisfied"
            }
        },
        {
            $group: {
                _id: "$name",
                averageSatisfaction : {
                    $avg: "$satisfied"
                }
            }
        }
]);

Durchschnittliche Projektdauer des “Technology” Departments:

db.Project.aggregate([
        {
            $match: {
                "department.name": "Technology"
            }
        },
        {
            $project: {
                name: "$department.name",
                durationInDays: {
                    $dateDiff: {
                        startDate: "$start_date",
                        endDate: "$end_date",
                        unit: "day"
                    },
                },
            }
        },
        {
            $group: {
                _id: null,
                avgProjectDurationDays : {$avg: "$durationInDays"}
            }
        },
        {
            $project: {
                _id: 0
            }
        }
]);'

Satisfaction des “Technology” Departments:

db.Department.aggregate([
    { 
        $match: { 
            "name": "Technology" 
        } 
    },
    {
        $project: {
            _id: 0,
            name: 1,
            averageSatisfaction: {
                $divide: [
                    { $sum: "$employees.satisfied" },
                    { $size: "$employees" }
                ]
            }
        }
    }
]);