对查询中的嵌套时间戳进行排序

时间:2015-10-26 16:39:25

标签: rethinkdb rethinkdb-javascript

我需要在Incidentsevents日期之前使用startedAt列表及其嵌套的timestamp有序DESC进行查询。默认情况下,ReQL为日期提供ASC订单。我有以下结构: { "id": "87e14db8-1e15-4718-baac-f1c785e985cb" , "title": "Connection Error" "startedAt": Mon Oct 26 2015 14:33:00 GMT+00:00 , "events": [{ "message": "Cannot connect to theserver.com", "timestamp": Mon Oct 26 2015 14:33:00 GMT+00:00 },{ "message": "Cannot connect to theserver.com," "timestamp": Mon Oct 26 2015 14:33:20 GMT+00:00 },{ "message": "Cannot connect to theserver.com", "timestamp": Mon Oct 26 2015 14:33:40 GMT+00:00 }] },{ "id": "87e14db8-1e15-4718-baac-f1c785e985cb" , "title": "Other Connection Error" "startedAt": Mon Oct 26 2015 14:34:20 GMT+00:00 , "events": [{ "message": "Connection rejected", "timestamp": Mon Oct 26 2015 14:34:20 GMT+00:00 },{ "message": "Connection rejected", "timestamp": Mon Oct 26 2015 14:34:41 GMT+00:00 }] },{ ... (several more) }

如果我运行r.db('mydb').table('Incident').orderBy(r.desc('createdAt')),事件按{G} createdAt排序。但嵌套的events仍然是ASC命令。

如何通过按时间戳获取带有DESC顺序的嵌套事件来进行查询?

2 个答案:

答案 0 :(得分:2)

这样的事情应该这样做:

r.table('Incident').orderBy(r.desc('createdAt')).merge(function(row) {
  return {events: row('events').orderBy(r.desc('timestamp'))};
})

答案 1 :(得分:2)

我认为这就是你要找的东西。用.map(...)方法做了一点巫术。

r.db("test").table("stackoverflow").orderBy(r.desc('startedAt')).map(function(d){
  return {
    "startedAt":d("startedAt"),
    "title": d("title"),
    "id": d("id"),
    "events": d("events").orderBy(r.desc("timestamp"))
  }
})