1

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:password@ds035965.mongolab.com: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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Chirayu Chamoli
  • 2,076
  • 1
  • 17
  • 32
  • `jeroen` is a **class** defined by the package author, Jeroen Ooms. See line 261 of the [source code](https://github.com/jeroenooms/mongolite/blob/master/R/object.R) – SymbolixAU May 22 '16 at 03:44
  • reading another [similar question](http://stackoverflow.com/q/12233874/5977215), it could be that you need to use an IP address, not a host name? – SymbolixAU May 23 '16 at 07:16
  • @Symbolix: That would mean i would not be able to connect with out any ip right. – Chirayu Chamoli May 23 '16 at 07:19
  • I'm not entirely certain, and haven't researched it in great detail, but appears that you need an ip for `rmongodb` – SymbolixAU May 23 '16 at 07:24
  • @Symbolix: Me too. Thanks for your help!\ – Chirayu Chamoli May 23 '16 at 07:28
  • You should note that `rmongodb` has been [removed from cran](https://cran.r-project.org/web/packages/rmongodb/index.html) and is [no longer supported/maintained](https://github.com/dselivanov/rmongodb/issues/96) – SymbolixAU Sep 13 '16 at 11:31
  • @SymbolixAU oh thats sad news. will look for other package then. – Chirayu Chamoli Sep 13 '16 at 11:46

1 Answers1

2

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:password@ds035965.mongolab.com: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)
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139