为什么我的代码无法连接到 Mongo 数据库?

Why is my code unable to connect to Mongo DB?

我正在尝试将我的 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

所以上面的代码有效!

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

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