2016-02-26 77 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) 
    });