对查询中的嵌套时间戳进行排序
sort nested timestamps in a query
我需要使用 Incidents
列表和他的嵌套 events
按他的 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'))
,事件是按预期由 createdAt
订购的。但嵌套的 events
仍按 ASC 顺序排列。
如何进行查询以获取按时间戳排序的嵌套事件?
应该这样做:
r.table('Incident').orderBy(r.desc('createdAt')).merge(function(row) {
return {events: row('events').orderBy(r.desc('timestamp'))};
})
我想这就是您要找的。只是对 .map(...)
方法施展了一点魔法。
r.db("test").table("Whosebug").orderBy(r.desc('startedAt')).map(function(d){
return {
"startedAt":d("startedAt"),
"title": d("title"),
"id": d("id"),
"events": d("events").orderBy(r.desc("timestamp"))
}
})
我需要使用 Incidents
列表和他的嵌套 events
按他的 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'))
,事件是按预期由 createdAt
订购的。但嵌套的 events
仍按 ASC 顺序排列。
如何进行查询以获取按时间戳排序的嵌套事件?
应该这样做:
r.table('Incident').orderBy(r.desc('createdAt')).merge(function(row) {
return {events: row('events').orderBy(r.desc('timestamp'))};
})
我想这就是您要找的。只是对 .map(...)
方法施展了一点魔法。
r.db("test").table("Whosebug").orderBy(r.desc('startedAt')).map(function(d){
return {
"startedAt":d("startedAt"),
"title": d("title"),
"id": d("id"),
"events": d("events").orderBy(r.desc("timestamp"))
}
})