RMongo icon indicating copy to clipboard operation
RMongo copied to clipboard

Switch to and query database other than authentication database

Open komalsrathi opened this issue 7 years ago • 7 comments

Hi,

I am using RMongo to connect to MongoDB.

# this is how I connect using the mongo driver:
mongo -u user -p pass --authenticationDatabase "admin" --port 27018
# after I get in the database, I switch to test and query it
use test

# Now in R, this works perfectly
mg1 <- mongoDbConnect(dbName = 'admin', host = 'localhost', port = '27018')
auth <- dbAuthenticate(rmongo.object = mg1, username = 'user', password = 'pass')

However, I want to switch to another database called test and query it. How can I do that?

Thanks, Komal

komalsrathi avatar Mar 21 '17 00:03 komalsrathi

Can you make another connection:

testConn <- mongoDbConnect(dbName = 'test', host = 'localhost', port = '27018')
auth <- dbAuthenticate(rmongo.object = testConn, username = 'user', password = 'pass')
``

tc avatar Mar 22 '17 18:03 tc

Hi all,

Is it possible in RMongo to query against a non-admin authentication database? I had this issue with mongolite as well, and tried using RMongo as a solution, and believe I am having the same issue. I have a database, collection, and documents already created (I don't want to insert anything myself)—is there a way for me to query that collection (which I did not create myself), without being an admin?

Thank you, Jenna

korobovaj avatar Aug 06 '19 16:08 korobovaj

Can you list the commands you tried?

I think it should work.

tc avatar Aug 06 '19 22:08 tc

I tried:

dbGetQuery(my_coll, collec, '{}'), where my_coll is the mongo connection using mongolite and collec is the collection name. The goal of this was to do sort of an ls using RMongo. The error was: Error in (function (classes, fdef, table) : unable to find an inherited method for function 'dbGetQuery' for signature '"mongo", "character", character"

I assumed this had something to do with the fact that I was using a connection made in mongolite rather than in RMongo, so I tried making a connection in RMongo, and did this:

mon <- mongoDbConnect("DBname", "host name", port) dbShowCollections(mon) and the error was: Error in .jcall(RMongo.object@javaMongo, "[S", "dbShowCollections") : com.mongodb.CommandFailureException: {"serverUsed": , "ok" : 0.0 , "errmsg" : "not authorized on collection to execute command {listCollections: \"collectionName\"}", "code" : 13, "codeName" : "Unauthorized"}

korobovaj avatar Aug 07 '19 19:08 korobovaj

Resolved, thank you for your help!

korobovaj avatar Aug 07 '19 21:08 korobovaj

Can you leave your solution so it can help others?

tc avatar Aug 07 '19 21:08 tc

Not sure how to resolve in RMongo, but in mongolite, the mongo() must have a database and a collection as parameters, whereas the URL that mongo() takes in uses the authentication database. All different things, and all need to be included. RMongo connects to databases slightly differently, so I don't know how to work around that. Sorry this was unhelpful for RMongo.

korobovaj avatar Aug 07 '19 22:08 korobovaj