如何按时间戳返回最新记录?

时间:2016-12-14 06:41:56

标签: couchdb cloudant

我的数据集与此类似:

{"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}     

这是否可以使用map / reduce索引?如果是这样,怎么样?

理想情况下,我也希望按评级值排序。

1 个答案:

答案 0 :(得分:2)

如果您创建这样的Map函数

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

将在用户&时间顺序。

要返回给定用户的最新(最新)时间戳,您可以使用以下参数查询索引:

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

我们以相反的顺序查询索引,从该用户的最大时间戳开始,但将结果集大小限制为1 - 最新的条目。