获取mongo中特定字段的最后一个元素

时间:2017-09-13 17:39:42

标签: mongodb mongoose aggregate

我有这组数据:

mktemp

我正在尝试编写一个查询,该查询根据时间字段对文档进行分组,并仅返回每个组中的最新文档。结果也应该按时间字段降序排列。 在oracle中,它将是下面查询的内容

git add

最后我想要这样的东西:

    {
        "Url": "http://someUrl/1",
        "Type": "A",
        "Time": ISODate("2017-09-13T08:51:54.066Z:)
    }
    {
        "Url": "http://someUrl/2",
        "Type": "F",
        "Time": ISODate("2017-09-10T08:51:54.066Z:)
    }
    {
        "Url": "http://someUrl/3",
        "Type": "A",
        "Time": ISODate("2017-09-02T08:51:54.066Z:)
    }
    {
        "Url": "http://someUrl/1",
        "Type": "B",
        "Time": ISODate("2017-09-12T08:51:54.066Z:)
    }
    {
        "Url": "http://someUrl/2",
        "Type": "C",
        "Time": ISODate("2017-09-13T09:51:54.066Z:)
    }

我试过了:

select a.*
from table a
where a.time>=(select b.time from table b where a.url=b.url)
order by a.time desc

但它会返回奇怪的有序结果'我可能在这里遗漏了一些东西,因为它是这个主题的解决方案:Return the last "true" value for each group

由于

0 个答案:

没有答案