Search code examples
rmongolite

Unable to see all collections from a MongoDB remote server using mongolite


This may be a trivial problem. I'm able to connect to remote MongoDB server. Im using mongolite for connecting to the db. My DB is mobileapps. I also don't know what to specify in 'collection'. I know I can specify any collection.

library(mongolite)
con=mongo(collection = "test", url = "mongodb://user:[email protected]:35965/mobileapps")

Though it is connecting, but doesn't show any data. Also I don't understand why does it show this for str(con): why is jeroen there.

Classes 'mongo', 'jeroen', 'environment' <environment: 0x0000000014a8ec00>

It is connecting but I am unable to see all the collections. How do I see all the collections in the db.

Also how do some basic statistics about the collection without querying like column names, types. I could only use con$count() to count the rows in the db. Something similar to db.getCollectionNames() from cmd prompt.

Update 1

I understand that I have to specify a particular collection while connecting from mongolite. But how do I connect using Rmongodb is still an issue.

mongo.create(host = "ds035965.mongolab.com", name = "MobileApp1", username = "user", password = "password ", db = "mobileapps")

This gives me an error:

Unable to connect to replset
Authentication failed.

Update 2

When I connect to my local host using rmongodb I get this error.

Error in as.environment(pos) : invalid 'pos' argument

Even though I'm able to see the db and the collection within, I still get this error. Any thoughts on what's happening?


Solution

  • mongolite requires you to connect to a specific collection of a database. When you initialise the mongo object, you see that the db and collection arguments default to "test"

    ?mongolite::mongo
    

    mongo(collection = "test", db = "test", url = "mongodb://localhost", verbose = TRUE)

    Therefore, when you initialise con with

    con=mongo(collection = "test", url = "mongodb://user:[email protected]:35965/mobileapps")
    

    Even though you haven't specified it to connect to a particular database, it is connecting to db = test, because that is the default. And, therefore, if you don't have a database called test, it won't contain any data. Which explains why you're not seeing any data.


    It's my understanding (and I'm happy to be corrected) that with mongolite it's not possible to look at all the collections in a database, as you have to specifically connect to one.

    If you want to see the collections, you can try the rmongodb package

    library(rmongodb)
    mongo <- mongo.create()
    mongo.get.databases(mongo)
    ## returns databases
    

    However, to see all the collections in the database you can't use the function

    mongo.get.database.collections(mongo, "test")
    

    as it return an empty string. This is a known issue

    A workaround is to use

    ## return all the collections in the database 'test'
    mongo.command(mongo = mongo, db = "test", command = list(listCollections=1))
    
    mongo.destroy(mongo)