我是MongoDB的新手,正在我的Laravel项目中使用jenssegers库。我有一张名为'table1'的表格,它有'_id'、'c1'、'c2'、'c3'和'created_at'这些列。我需要将下面的SQL查询转换成MongoDB。
我需要选择所有列,包括'_id'。当按照c2列分组并再次按照创建时间列排序结果行时,只需要包含最大created_at值的行。
select *, count(c3) as total from
(select * from table1
where c1 in ('1', '32', '6', ...)
order by created_at desc) as temp
group by c2 order by created_at desc
我需要选择所有列,包括'_id'。当按照c2列分组并再次按照创建时间列排序结果行时,只需要包含最大created_at值的行。
我找到了这个答案,但我认为我的问题有点更复杂: https://dev59.com/9IDba4cB1Zd3GeqPFIBL#24143255
这可能对我的问题有用且相关: http://blog.chomperstomp.com/how-to-order-by-before-group-by-with-mysql/