1

我试图将我的 R 代码指向我的 mongodb。

我有一个config.yml

default:
    host:    "cluster0.qixym.mongodb.net"
    username: "****"
    password: "****"
    
production:
    host:     "cluster0.qixym.mongodb.net"
    username: "****"
    password: "****"

这是我的代码:

# MONGO DB TRAINING -----
# Version 1
# LIBRARIES ----
library(mongolite) # Resource: https://jeroen.github.io/mongolite/
library(jsonlite)
library(config)
library(tidyverse)
library(lubridate)

# 1.0 CONNECTION TO REMOTE MONGODB ----

# Setup config Package & database YAML
Sys.setenv(R_CONFIG_ACTIVE = "default")

config <- config::get(file = "config.yml")



mongo_connect <- function(collection, database, 
                          username = config$username, 
                          password = config$password, 
                          host = config$host){
  
  mongo(
    collection = collection, 
    url = str_glue("mongodb+srv://{username}:{password}@{host}/{database}/?tls=true"), 
    options = ssl_options(weak_cert_validation = T)
    
    )
  
}



# Connect to MongoDB Atlas Cloud Database
mongo_connect(collection = "mtcars", database = "rstats")

我收到以下错误:

** (函数中的错误(uri =“mongodb://127.0.0.1”,pem_file = NULL,pem_pwd = NULL,:解析 URI 失败:p(URI 中的数据库名称无效)**

我确实看过这个帖子,但找不到解决方案:https ://github.com/jeroen/mongolite/issues/219

4

1 回答 1

1

所以上面的代码有效!

问题是我的密码中有特殊字符!

https://docs.mongodb.com/manual/reference/connection-string/#components

于 2022-01-05T16:07:31.707 回答