2016-12-14 84 views
-1

我有一个数据集,看起来与此类似:如何通过时间戳返回最新记录?

{"user":333,"product":943, "rating":2.025743791177902, "timestamp":1481675659} 
{"user":333,"product":3074,"rating":2.1070657532324493,"timestamp":1481675178} 
{"user":333,"product":3074,"rating":2.108323259636257, "timestamp":1481673546} 
{"user":333,"product":943, "rating":2.0211849667268353,"timestamp":1481675178} 
{"user":333,"product":943, "rating":2.041045323231024, "timestamp":1481673546} 
{"user":333,"product":119, "rating":2.1832303461543163,"timestamp":1481675659} 
{"user":333,"product":119, "rating":2.1937538029700203,"timestamp":1481673546} 
{"user":111,"product":123, ... 

我想查询所有记录的用户(例如333),但只返回最新的时间戳:

{"user":333,"product":119, "rating":2.1832303461543163,"timestamp":1481675659}  

是这可能与地图/减少指数?如果是这样,怎么样?

理想情况下,我也想按照评分值排序。

+0

Downvoter - 我不知道你对这个问题不喜欢什么,以及如何改进它,除非你在downvote时添加评论。 –

回答

1

如果你创建一个这样

function(doc) { 
    emit([doc.user, doc.timestamp], null); 
} 

一个地图功能的指数将在用户&时间顺序产生。

要返回最新(最近)日期和时间给定用户可查询以下参数指标:

startkey=[333,9999999999] 
endkey=[333,0] 
descending=true 
limit=1 

我们正在查询以相反的顺序指数,从该最大的时间戳但将结果集大小限制为1 - 最新的条目。