【问题标题】:Mongodb aggregate not returning correct dateMongodb聚合没有返回正确的日期
【发布时间】:2018-10-19 21:38:46
【问题描述】:

我正在尝试从 MongoDB(使用 PHP)返回结果,其中有以下输出: 用户名、探险次数、转录次数、最后转录日期

我使用的查询是

$collection->aggregate([
[
    '$match' => [
        'subject_projectId' => (int) $projectId
    ]
],
[
    '$sort' => [
        'classification_finished_at' => -1
    ]
],
[
    '$group' => [
        '_id' => '$user_name',
        'transcriptionCount' => [
            '$sum' => 1
        ],
        'expedition' => [
            '$addToSet' => '$subject_expeditionId'
        ],
        'last_date' => [
            '$first' => '$classification_finished_at'
        ]
    ]
],
[
    '$project' => [
        '_id' => 0,
        'user_name' => '$_id',
        'transcriptionCount' => 1,
        'expeditionCount' => [
            '$size' => '$expedition'
        ],
        'last_date' => 1
    ]
]]);

我得到了正确的回报,除了最后一个日期。它没有给我用户进行转录的最后日期,而是给了我其他日期。不知道给出的日期是什么,我只知道某些用户有更晚的日期。 想知道我做错了什么。

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    $min$maxlast_date 一起使用

    [
        '$group' => [
            '_id' => '$user_name',
            'transcriptionCount' => [
                '$sum' => 1
            ],
            'expedition' => [
                '$addToSet' => '$subject_expeditionId'
            ],
            'last_date' => [
                '$min' => '$classification_finished_at'
            ]
        ]
    ]
    

    【讨论】:

    • 谢谢。完美运行。
    猜你喜欢
    • 2013-12-07
    • 2011-09-27
    • 1970-01-01
    • 2020-04-11
    • 2023-03-17
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多