Created
April 26, 2018 06:06
-
-
Save tmasjc/85740370291ddce2bc0bb97065799b82 to your computer and use it in GitHub Desktop.
Mongolite basic demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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