我有一個SQL查詢的工作示例:SQL子查詢聚集比喻
SELECT a.user_id, a.latitude, a.longitude, a.accuracy, a.heading, a.speed, a.created_at
FROM `location` a
INNER JOIN (
SELECT user_id, MAX(`created_at`) created_at
FROM location
WHERE created_at > :date
GROUP BY `user_id`
) b ON a.`user_id` = b.`user_id` AND a.`created_at` = b.`created_at`
它爲每個用戶
我怎麼能執行MongoDB中此查詢選擇今天的最後一個位置?
我得到了它(子查詢)在PHP中的第一部分:
$mongodb->getCollection('location')->aggregate([
['$match' => ['created_at' => ['$gte' => $beginOfDay]]],
[
'$group' => [
'_id' => '$user_id',
'lastTime' => ['$max' => '$created_at']
]
]
]);
和我就不會要創建新的查詢......
也許在MongoDB中它很容易做的比在關係D B?
什麼是收集格式/形狀ans所需的輸出? – felipsmartins