tc / RMongo

R client to interface with MongoDB
102 stars 34 forks source link

Switch to and query database other than authentication database #46

Open komalsrathi opened 7 years ago

komalsrathi commented 7 years ago

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

tc commented 7 years ago

Can you make another connection:


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

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

tc commented 5 years ago

Can you list the commands you tried?

I think it should work.

korobovaj commented 5 years ago

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 commented 5 years ago

Resolved, thank you for your help!

tc commented 5 years ago

Can you leave your solution so it can help others?

korobovaj commented 5 years ago

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.