为了将数据保存到mongodb (使用CRAN中的mongolite包),我定义了以下函数:
saveData <- function(data, collection) {
# Connect to the database
db <- mongo(collection = collection,
url = paste0(
"mongodb://",
options()$mongodb$username, ":",
options()$mongodb$password, "@",
options()$mongodb$host, "/",
databaseName))
# Insert the data into the mongo collection as a data.frame
data <- as.data.frame(t(data))
db$insert(data)
}我的数据存储在一个矩阵中,如下所示:
> mat.answers
var1 var2 var3 var4 var5
1 90 60 70 60 50
2 85 65 75 55 50
3 80 70 75 70 25
4 75 70 80 80 55
5 80 66 80 75 55但是,当我使用该命令保存一些数据时,我得到了以下错误:
saveData(df.answers,"SST")
Mongo Message: SCRAM: authenticating "dynamo" (step 1)
Error: Authentication failed.
Called from: mongo_collection_command_simple(col, "{\"ping\":1}")
Browse[1]> 我在mongodb配置文件中生成了一个用户并启用了身份验证。在mongo shell中,使用这些凭据就可以很好地登录。它既不能用于本地数据库,也不能连接到mongolab提供的远程数据库。
发布于 2019-01-06 06:24:59
我发现以下命令允许我使用mongolite和传统的SCRAM-SHA-1身份验证模式连接到mongodb。
library(mongolite)
mongoUrl <- "mongodb://a_username:a_password@localhost:27017/admin" #<-admin here is the mongodb database that stores the authentication info
# specify your collection
colname <- "a_collection"
# specify your database
dbname <- "a_database"
# create connection (con)
con <- mongo(collection = colname, url = mongoUrl, db=dbname)
# count how many records (fyi this is just a test)
con$count('{}')https://stackoverflow.com/questions/38050869
复制相似问题