Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to and query database other than authentication database #46

Open
komalsrathi opened this issue Mar 21, 2017 · 7 comments
Open

Comments

@komalsrathi
Copy link

komalsrathi commented Mar 21, 2017

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
Copy link
Owner

tc commented Mar 22, 2017

Can you make another connection:

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

@korobovaj
Copy link

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
Copy link
Owner

tc commented Aug 6, 2019

Can you list the commands you tried?

I think it should work.

@korobovaj
Copy link

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
Copy link

Resolved, thank you for your help!

@tc
Copy link
Owner

tc commented Aug 7, 2019

Can you leave your solution so it can help others?

@korobovaj
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants