Mongolitedt

Mongolitedt is an extension to mongolite. It binds two methods, finddt() and aggregatedt() to the mongo object that's created through mongo <- mongo().

These functions return data.table objects from the find or aggregate query. Therefore, the query you send to mongodb has to be able to be coerced into a data.table. If not, it will throw an error.

Examples

You bind the two functions using bind_mongolitedt()

## create a mongo() connection object
mongo <- mongo(collection = "vignette",
               db = "mongolitedt",
               url = "mongodb://localhost")

## looking at the mongo object we see all the methods currently attached.
mongo
# <Mongo collection> 'test' 
#  $aggregate(pipeline = "{}", handler = NULL, pagesize = 1000) 
#  $count(query = "{}") 
#  $distinct(key, query = "{}") 
#  $drop() 
#  $export(con = stdout(), bson = FALSE) 
#  $find(query = "{}", fields = "{\"_id\":0}", sort = "{}", skip = 0, limit = 0, handler = NULL, pagesize = 1000) 
#  $import(con, bson = FALSE) 
#  $index(add = NULL, remove = NULL) 
#  $info() 
#  $insert(data, pagesize = 1000) 
#  $iterate(query = "{}", fields = "{\"_id\":0}", sort = "{}", skip = 0, limit = 0) 
#  $mapreduce(map, reduce, query = "{}", sort = "{}", limit = 0, out = NULL, scope = NULL) 
#  $remove(query, multiple = FALSE) 
#  $rename(name, db = NULL) 
#  $update(query, update = "{\"$set\":{}}", upsert = FALSE, multiple = FALSE) 

We can now bind the two new functions and see them added to the mongo object

bind_mongolitedt(mongo)

## and look at the methods
mongo
# <Mongo collection> 'test' 
#  $aggregate(pipeline = "{}", handler = NULL, pagesize = 1000) 
#  $aggregatedt(pipeline = "{}", pagesize = 1000) 
#  $count(query = "{}") 
#  $distinct(key, query = "{}") 
#  $drop() 
#  $export(con = stdout(), bson = FALSE) 
#  $find(query = "{}", fields = "{\"_id\":0}", sort = "{}", skip = 0, limit = 0, handler = NULL, pagesize = 1000) 
#  $finddt(query = "{}", fields = "{\"_id\":0}", sort = "{}", skip = 0, limit = 0, pagesize = 1000) 
#  $import(con, bson = FALSE) 
#  $index(add = NULL, remove = NULL) 
#  $info() 
#  $insert(data, pagesize = 1000) 
#  $iterate(query = "{}", fields = "{\"_id\":0}", sort = "{}", skip = 0, limit = 0) 
#  $mapreduce(map, reduce, query = "{}", sort = "{}", limit = 0, out = NULL, scope = NULL) 
#  $remove(query, multiple = FALSE) 
#  $rename(name, db = NULL) 
#  $update(query, update = "{\"$set\":{}}", upsert = FALSE, multiple = FALSE) 

In the above example we see the two methods, finddt() and aggregatedt() have been attached to the mongo object.

You then use them as you would the regular find and aggregate methods. However, the returned objects will be data.tables.

data("mtcars")
mongo$insert(mtcars)
# Complete! Processed total of 32 rows.
# [1] TRUE
rm(mtcars)

df <- mongo$find()
str(df)
# 'data.frame': 32 obs. of  11 variables:
#  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#  $ cyl : int  6 6 4 6 8 6 8 4 4 6 ...
#  $ disp: num  160 160 108 258 360 ...
#  $ hp  : int  110 110 93 110 175 105 245 62 95 123 ...
#  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
#  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
#  $ qsec: num  16.5 17 18.6 19.4 17 ...
#  $ vs  : int  0 0 1 1 0 1 0 1 1 1 ...
#  $ am  : int  1 1 1 0 0 0 0 0 0 0 ...
#  $ gear: int  4 4 4 3 3 3 3 4 4 4 ...
#  $ carb: int  4 4 1 1 2 1 4 2 2 4 ...

dt <- mongo$finddt()
str(dt)
# Classes ‘data.table’ and 'data.frame':    32 obs. of  12 variables:
#  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#  $ cyl : int  6 6 4 6 8 6 8 4 4 6 ...
#  $ disp: num  160 160 108 258 360 ...
#  $ hp  : int  110 110 93 110 175 105 245 62 95 123 ...
#  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
#  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
#  $ qsec: num  16.5 17 18.6 19.4 17 ...
#  $ vs  : int  0 0 1 1 0 1 0 1 1 1 ...
#  $ am  : int  1 1 1 0 0 0 0 0 0 0 ...
#  $ gear: int  4 4 4 3 3 3 3 4 4 4 ...
#  $ carb: int  4 4 1 1 2 1 4 2 2 4 ...
#  $ _row: chr  "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#  - attr(*, ".internal.selfref")=<externalptr> 

mongo$find(query = '{ "mpg" : 21 }')
#  Imported 2 records. Simplifying into dataframe...
#               mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

mongo$finddt(query = '{ "mpg" : 21 }')
#  Imported 2 records.
#    mpg cyl disp  hp drat    wt  qsec vs am gear carb          _row
# 1:  21   6  160 110  3.9 2.620 16.46  0  1    4    4     Mazda RX4
# 2:  21   6  160 110  3.9 2.875 17.02  0  1    4    4 Mazda RX4 Wag

mongo$aggregate(pipeline = '[ { "$match" : { "mpg" : 21 }  } , 
                               { "$project" : { "_id" : 0, "_row" : 1, "disp" : 1 } }  ]')

#  Imported 2 records. Simplifying into dataframe...
#               disp
# Mazda RX4      160
# Mazda RX4 Wag  160


mongo$aggregatedt(pipeline = '[ { "$match" : { "mpg" : 21 }  } , 
                               { "$project" : { "_id" : 0, "_row" : 1, "disp" : 1 } }  ]')

#  Imported 2 records.
#    disp          _row
# 1:  160     Mazda RX4
# 2:  160 Mazda RX4 Wag

## clean up
rm(mongo); gc()


SymbolixAU/mongolitedt documentation built on May 9, 2019, 3:30 p.m.