How to use getall with orderby in RethinkDB

∥☆過路亽.° 提交于 2019-11-30 07:12:36

RethinkDB doesn't support efficient index intersection (the Github issue to add this is #809), but you could implement this query efficiently by adding a compound index for the 'id' and 'timestamp' indexes.

If your result set is small enough, though, the orderBy could just be done completely in-memory by dropping the 'index' optarg:

r.table("test").getAll(1, {index: "id"}).orderBy("timestamp")

To do this efficiently for large result sets, you would need an index. Assuming your 'id' and 'timestamp' indexes correspond directly to fields in your rows, adding the index would look like:

r.table("test").indexCreate("id_time",
                            function(row) {
                                return [row("id"), row("timestamp")];
                            })

To get all the rows with id=1 and sort by the timestamp, you would then run:

r.table("test").between([1], [2], {"index": "id_time"})
               .orderBy({"index": "id_time"})

In addition, going back to the original query you posted, you could query between two timestamps for id=1 by running:

r.table("test").between([1, <time 1>], [1, <time 2>], {"index": "id_time"})
               .orderBy({"index": "id_time"})
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!