jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

Documentation: where do I find infos on handlers? #130

Closed Ploulack closed 6 years ago

Ploulack commented 6 years ago

Hello,

thank you for the work, I use it everyday :) I'd like to learn more about handlers, and couldn't find anything in the documentation. Could you share a pointer? Thank you.

jeroen commented 6 years ago

What kind of handlers? Which function?

Ploulack commented 6 years ago

the 'find' method as in: db$find('{}', handler = )

jeroen commented 6 years ago

Ah OK. It's a callback function, similar to the handler argument in jsonlite::stream_in. If specified, it will be called many times, each time with a subset of the results.

Ploulack commented 6 years ago

Thank you. Got it. So I can apply my handler function on each page which' size depends on my pagesize parameter. Correct? Talking about this, where could I find more info on the pagesize parameter (still the find method)? Is it per documents found? per actual json lines?

jeroen commented 6 years ago

In the mongolite $find method it is the number of documents per page. Just try this:

con <- mongo("mtcars", url = "mongodb://readwrite:test@ds043942.mongolab.com:43942/jeroen_test")
con$insert(mtcars)
handler <- function(x){
  cat("found", nrow(x), "documents!\n")
  print(x)
}
con$find("{}", handler = handler, pagesize = 5)
con$drop()
Ploulack commented 6 years ago

Great thank you! :) I've just made my first handler for find. Life feels so much modular suddenly :)

jeroen commented 6 years ago

Also have a look at the iterate method which is similar but more low-level (per document).