Chirayu Chamoli Chirayu Chamoli - 1 year ago 160
R Question

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

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

con=mongo(collection = "test", url = "mongodb://")

Though it is connecting, but doesnt show any data. Also i dont 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

Thanks 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 = "", 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 im able to see the db and the collection within i still get this error. Any thoughts on whats happening.

Answer Source

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"


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

Therefore, when you initialise con with

con=mongo(collection = "test", url = "mongodb://")

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

mongo <- mongo.create()
## 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))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download