What is used to write functions in mongoDB / mongolite?
I am researching mongolite / mongoDB right now and came across this:
https://cran.r-project.org/web/packages/mongolite/vignettes/intro.html
Inside I saw code like this:
tbl <- m$mapreduce(
map = "function(){emit({cut:this.cut, color:this.color}, 1)}",
reduce = "function(id, counts){return Array.sum(counts)}"
)
Can anyone tell me what these functions are written? I don't think they are R functions.
source to share
The R language allows you to create environments where you put functions, which then refer to the $ -operator as you retrieve items from the list. So it m$mapreduce
calls the R function and sends this text to the database engine: http://docs.mongodb.org/manual/reference/command/mapReduce/
If you install the package and execute help(pac=mongolite)
, you will see that the package has one public function , mongo
which allows any of these function calls. Then you can work with the examples on the help page and vignette.
(Note: you will get an error if you don't install the first version and customize the database executable.)
If you do it with mongolith, you get a list of objects in the environment defined when the mongo function is created:
ls(envir=environment(mongo))
This environment has a collection of objects that appear to contain what you might be interested in:
[14] "mongo_collection_aggregate"
[15] "mongo_collection_command"
[16] "mongo_collection_command_simple"
[17] "mongo_collection_count"
[18] "mongo_collection_create_index"
[19] "mongo_collection_distinct"
[20] "mongo_collection_drop"
[21] "mongo_collection_drop_index"
[22] "mongo_collection_find"
[23] "mongo_collection_find_indexes"
[24] "mongo_collection_insert_bson"
[25] "mongo_collection_insert_page"
[26] "mongo_collection_mapreduce"
[27] "mongo_collection_name"
[28] "mongo_collection_new"
[29] "mongo_collection_remove"
[30] "mongo_collection_rename"
[31] "mongo_collection_stats"
[32] "mongo_collection_update"
source to share
The mapreduce functions in the mongolite package are written in javascript. Please see the package documents on CRAN for confirmation (page 3) (link to external PDF) :
mapreduce(map, reduce, query = β{}β, sort = β{}β, limit = 0, out = NULL, scope = NULL)
" Runs a query to shrink the map. The map and shrink arguments are strings containing a JavaScript function. Specify a string to store the results in a collection instead of returning. "
source to share