使用Sails.js查找满足查询条件的最后一行

时间:2016-02-26 11:17:18

标签: sails.js waterline

使用Waterline - Sails查询只查找一条​​记录,在给定日期内搜索room2的最后插入价格,最佳解决方案是什么?

ID  PRICE   START       STOP        ROOM    createdAt           updatedAt
-------------------------------------------------------------------------
85  750.00  2016-01-01  2017-12-31  room1   2016-02-25 10:09:47 2016-02-25 10:09:47
86  590.00  2016-01-01  2017-12-31  room2   2016-02-25 10:09:55 2016-02-25 10:09:55
90  410.00  2016-02-25  2016-02-27  room2   2016-02-25 16:46:08 2016-02-25 16:46:08
91  310.00  2016-01-26  2016-04-28  room2   2016-02-26 09:35:26 2016-02-26 09:35:26

我做错了这些结果:

var where = {"start":{"<=":"2016-02-25T23:00:00.000Z"},"end":{">=":"2016-02-25T23:00:00.000Z"},"room":"room2"}

PriceHistory.find(where).max('createdAt').exec(function(err, prices){
        res.json(prices);
});

1 个答案:

答案 0 :(得分:2)

如何使用createdAt降序排序并限制为一条记录?

PriceHistory.find(where)
    .sort('createdAt DESC')
    .limit(1)
    .exec(function(err, prices){
        res.json(prices);   // should return one record (last one)
    });