-1
我如何在mongo中編寫下面的查詢?mongo中mysql max和min函數的等價物?
select max(priority) as max, min(priority) as min from queue group by user
如果您在PHP中提供解決方案,我將非常感激。
謝謝
我如何在mongo中編寫下面的查詢?mongo中mysql max和min函數的等價物?
select max(priority) as max, min(priority) as min from queue group by user
如果您在PHP中提供解決方案,我將非常感激。
謝謝
查詢像這樣與聚集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')
)
)
));