[Question] Is there a way to filter/sort the data?
Closed this issue · 12 comments
Hi,
I tried to find the answer in rethinker, but it's not straight forward to me.
rethinkdb
is NoSQL database so it is by definition unstructured, but you might perform both filtering and sorting using e.g. dplyr package in R. For example assume that in collection
is your fetched query and there is column with time
included, then you can sort your data basing on time by collection %>% arrange(time)
I hope this helps.
I trying to fetch a subset of data from rethinkdb, instead of reading everything in everytime and then use R to do filtering. It's okay to deal with 500 docs, but how about 500K docs?
Can you provide an example how to search for a keyword in a field and only return those matched jsons? I saw examples using the data explorer, but I couldn't get rethinker syntax to work
Yes, you're right that reading all items from DB is very inefficient. Unfortunately, currently we're not supporting filtering or sorting in shiny.collections
, but it's definitely worth to add.
After a quick look at rethinker I found this piece of code:
r()$db("someDb")$table("someTable")$orderBy(list(index='awe'))$run(cn)->cr
which might be what you are looking for in terms of sorting. Notice that orderBy
is called before run
here.
Searching for a keyword... not sure yet, but you may also ask at rethinker issues. The answer would be interesting for me as well. Meanwhile I will try to experiment with the package and try to figure it out myself.
In my table I have a field called ts for time stamp. I created ts index via admin portal first and run the following
> r()$db("xxx")$table("xxx")$orderBy(list(index='ts'))$run(cn$raw_connection)
Error in fetchResponseRaw(x, token) :
Error: Expected type STRING but found OBJECT.
Did you ran into the same error?
I recreated an example from https://www.rethinkdb.com/docs/quickstart/ and this worked for me:
cn<-openConnection()
r()$db("test")$table("tv_shows")$orderBy('episodes')$run(cn)
so my suggestion would be:
r()$db("xxx")$table("xxx")$orderBy('ts')$run(cn$raw_connection)
LMK if that helps.
Yes your example works now :) now the only puzzle left is the filtering...
sorry, did you figured out how to do desc sorting?
So far we've got filtering. So you need to use lambda expression with x("...")
exchanged into x$bracket("...")
. Following an example from https://www.rethinkdb.com/docs/quickstart/ with tv_shows
this gives me correct output:
r()$db("test")$table("tv_shows")$filter(function(x) x$bracket("episodes")$gt(100))$run(cn)
Note that if you have more than one item to catch you need to use cursor and cursorNext(cursor)
.
@happyshows and here you go with desc sorting:
r()$db("test")$table("tv_shows")$orderBy(r()$desc('episodes'))$run(cn)
:)
@dokato man you're awesome
As this one is solved I'm closing in favor to #11
It would be super cool to have a filtering option in collection
function.
Thanks @happyshows for the suggestion!