如何归还所有项目员工?

时间:2017-08-02 13:33:26

标签: mongodb mongodb-query aggregation-framework

我的数据库中有以下格式集合(项目)的数据:

@pytest.mark.run_in_isolation
def test_import_not_made():
    ....

我需要找到最忙碌的员工并列出他所有的项目。

{ "_id" : ObjectId("5981a80f223e491a58230e5d"), "id" : 2, "name" : "gbqplhlqxzwl", "managerId" : 65151, "startDate" : "03.11.1999", "finishDate" : "02.01.2003", "projectStatus" : "POSTPONED", "participants" : [ ], "estimatedBudget" : 6017891.811079914 } { "_id" : ObjectId("5981a80f223e491a58230e5e"), "id" : 3, "name" : "erfekfsdgryu", "managerId" : 83749, "startDate" : "07.07.2007", "finishDate" : "26.12.2027", "projectStatus" : "POSTPONED", "participants" : [ 19229, 81856, 79270, 5509, 70344, 39424 ], "estimatedBudget" : 3086213.8981674756 } { "_id" : ObjectId("5981a80f223e491a58230e5f"), "id" : 1, "name" : "jvbzobhppntd", "managerId" : 18925, "startDate" : "29.04.1999", "finishDate" : "13.10.2008", "projectStatus" : "OPEN", "participants" : [ 46100, 96968, 6676, 56121, 4716, 68901, 43990, 48587, 62547, 30292, 65153, 17551, 27083, 20261, 27097, 50036, 86585, 69890, 18790, 22592, 60774, 93709, 78471, 27157, 4328, 36501, 47296, 16831 ], "estimatedBudget" : 3581496.7068344904 } { "_id" : ObjectId("5981a80f223e491a58230e60"), "id" : 4, "name" : "cdspkkqwvwld", "managerId" : 62042, "startDate" : "13.03.1998", "finishDate" : "20.06.2007", "projectStatus" : "OPEN", "participants" : [ 53480, 60897, 23677, 22064, 60807, 66637, 84609, 28378, 87143, 27675, 79283, 94992, 20429, 48769, 91671, 41747, 21651, 91134, 41684, 57228, 51949, 18756, 45679, 87781, 67287, 6902, 27526 ], "estimatedBudget" : 2126283.953787842 } .... 数组包含参与项目的员工ID。

我使用以下查询来找到最忙碌的员工:

participants

这正常。但我不知道如何列出他的所有项目。

任何想法?

1 个答案:

答案 0 :(得分:0)

db.projects.aggregate(
    [
    {
        $unwind: '$participants'
    },
    {
        $addFields: {
            count: 1
        }
    },
    {
        $group: {
            _id : '$participants',
            participation_count : {'$sum':'$count'},
            projectId : {$push: '$id'}
        }
    },
    {
        $sort:{participation_count:-1}
    },
    {
        $limit:1
    }
    ],
    {
        allowDiskUse:true
    }
)