mongolite
mongolite copied to clipboard
Apply call-back function and return values when hander is not NULL
When I was looking for a way to get one big list from aggreagate()
, I saw #236. I think handler
should work in this way.
library(mongolite)
con <- mongo("mtcars", url =
"mongodb+srv://readwrite:[email protected]/test")
if(con$count() > 0) con$drop()
con$insert(mtcars)
#> List of 5
#> $ nInserted : num 32
#> $ nMatched : num 0
#> $ nRemoved : num 0
#> $ nUpserted : num 0
#> $ writeErrors: list()
# data.frame
con$aggregate('[{"$match": {}}, {"$project": {"mpg": 1, "cyl": 1}}]')[1:2,]
#> _id mpg cyl
#> 1 656a87c6ab879540600b9221 21 6
#> 2 656a87c6ab879540600b9222 21 6
# list
con$aggregate('[{"$match": {}}, {"$project": {"mpg": 1, "cyl": 1}}]', handler = as.list)[1:2]
#> [[1]]
#> [[1]]$`_id`
#> [1] "656a87c6ab879540600b9221"
#>
#> [[1]]$mpg
#> [1] 21
#>
#> [[1]]$cyl
#> [1] 6
#>
#>
#> [[2]]
#> [[2]]$`_id`
#> [1] "656a87c6ab879540600b9222"
#>
#> [[2]]$mpg
#> [1] 21
#>
#> [[2]]$cyl
#> [1] 6
Created on 2023-12-02 with reprex v2.0.2