2014-06-24 94 views

回答

2

查詢像這樣與聚集framwework和.aggregate()方法進行。他們使用$group流水線階段與$min$max運營商。

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文檔中