Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Created April 26, 2018 06:06
Show Gist options
  • Select an option

  • Save tmasjc/85740370291ddce2bc0bb97065799b82 to your computer and use it in GitHub Desktop.

Select an option

Save tmasjc/85740370291ddce2bc0bb97065799b82 to your computer and use it in GitHub Desktop.
Mongolite basic demo
library(mongolite)
library(dplyr)
# connect to database
con <- mongo(collection = "relet_price", db = "bizops", url ="mongodb://localhost", verbose = TRUE)
# some random id based on datetime
gen_id <- function(){
c <- gsub("(\\:|\\-|\\s)", "", as.character(Sys.time()))
strsplit(c, "") %>%
unlist() %>%
sample() %>%
paste(sep = "", collapse = "") %>%
substr(start = 1, stop = 4)
}
#
res <- list(
id = gen_id(),
status = "1",
date = Sys.Date(),
price = rnorm(1, mean = 2000, sd = 100)
)
# insert
con$insert(res)
# find by
con$find() %>% filter(id == "1568")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment