1

I need to make a query with a list of Incidents and his nested events ordered DESC by his startedAt and timestamp dates. By default ReQL give the dates with a ASC order. I've got the folowing structure: { "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) }

If I run r.db('mydb').table('Incident').orderBy(r.desc('createdAt')), the Incident's are ordered by createdAt as espected. But the nested eventsare still ordered ASC.

How can I make a query in order to get the nested events with a DESC order by timestamp?

Alejandro Silva
  • 8,808
  • 1
  • 35
  • 29

2 Answers2

2

Something like this should do it:

r.table('Incident').orderBy(r.desc('createdAt')).merge(function(row) {
  return {events: row('events').orderBy(r.desc('timestamp'))};
})
mlucy
  • 5,249
  • 1
  • 17
  • 21
  • It works perfectly!! I must check more deep that merge method, somethimes do things in rethinkdb is hard because SQL mind-set, but in the long run I love it. Thanks a lot :) – Alejandro Silva Oct 26 '15 at 19:09
2

I think this is what you're looking for. Just took a little wizardy with the .map(...) method.

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"))
  }
})
dalanmiller
  • 3,467
  • 5
  • 31
  • 38