【问题标题】:Equivalent of mysql max and min functions in mongo?mongo中的mysql max和min函数等效吗?
【发布时间】:2014-06-24 04:23:44
【问题描述】:

如何在 mongo 中编写以下查询?

select max(priority) as max, min(priority) as min from queue group by user

如果您提供 PHP 解决方案,我将不胜感激。

谢谢

【问题讨论】:

    标签: php mysql mongodb mongodb-query aggregation-framework


    【解决方案1】:

    这样的查询是使用聚合框架和.aggregate() 方法执行的。他们使用带有$min$max 运算符的$group 管道阶段。

    db.collection.aggregate([
        { "$group": {
            "_id": "$user",
            "max": { "$max": "$priority" },
            "min": { "$min": "$priority" }
        }}
    ])
    

    或更多 PHP 语法:

    $collection->aggregate(array(
        array(
            '$group' => array (
                '_id' => '$user',
                'max' => array( '$max' => '$priority' ),
                'min' => array( '$min' => '$priority' )
            )
        )
    ));
    

    另请参阅文档中的SQL to Aggregation Mapping Chart

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-07
      • 2022-11-02
      • 2023-03-26
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      相关资源
      最近更新 更多