2016-02-26 75 views
0

什么是使用水线查找只有一条记录的最佳解决方案?在给定日期搜索room2的最后插入价格的Sails查询?使用Sails.js查找符合查询条件的最后一行

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); 
}); 
+0

这两个日期现在都是相同的:{“<=”:“2016-02-25T23:00:00.000Z”},“end”:{“> =”:“2016-02 -25T23:00:00.000Z“}' –

+0

@ yury-tarabanko我已经正确地重新编辑了我在给定日期寻找价格的问题(2016-02-25) – Cris69

回答

1

如何使用降序排序createdAt并限制到一个记录?

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