Search code examples
rmongodbrmongodb

Unable to see collections in mongo DB when connected through R


I used "rmongodb" package to connect to mongo DB through R. The connection is successful.

> mongo.is.connected(mongo)
[1] TRUE
> 

If I check at the host where mongoDB is running.

> use reporting
switched to db reporting

> show collections
MongoIndexing
details
test
>

But from R

> mongo.get.database.collections(mongo , db="reporting")
character(0)
>

Solution

  • The below code will return an array of collection names iff the provided database has collections in it. Otherwise, it will return character(0)

    try this:

    mongo <- mongo.create(host="127.0.0.1:27017" , db="sample")
    mongo.get.database.collections(mongo , "sample")
    

    Output: two collections named roles and categories

    "sample.roles"          
    "sample.categories" 
    

    To get all databases:

    mongo <- mongo.create(host="127.0.0.1:27017")
    mongo.get.databases(mongo)
    

    To get all collections in a specific database say sample:

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

    You can check rmongod link for more info.