Tim Ka
Tim Ka

Reputation: 142

Authentication Error with mongolite

To save data to mongodb (using the mongolite package from CRAN), I defined the following function:

  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)
  }

My data is stored in a matrix that looks something like this:

> 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

However, when I'm using the command to save some data, I get the following error:

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]> 

I generated a user and enabled authentication in the mongodb configuration file. From the mongo shell, I can log in just fine with these credentials. It does neither work for local databases, nor does it connect to a remote one provided by mongolab.

Upvotes: 3

Views: 979

Answers (1)

Gabriel Fair
Gabriel Fair

Reputation: 4264

I found that the following command allowed me to connect to mongodb using mongolite and the legacy SCRAM-SHA-1 authentication mode.

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('{}')

Upvotes: 3

Related Questions