Connect R with mongoDB

In this post we will see how to connect MongoDB with R. For this first we have to install package “rmongodb “.

Install and load rmongodb package:-
We can install “rmongodb” package by two ways. In first way we can go to Packages from R console and Install packages and choose “rmongodb”. (R Console–>Packages–>Install Package (s)–>rmongodb)

Install Package












We can also install it  by writing command on R console-

After it installed, you have to load it for use. To load the install package use below command-


Connect R to MongoDB

First we need to make a connection with MongoDB. If we run the below command without any parameter, it will connect MongoDB on localhost.

mongo <- mongo.create()
The same command can be used by taking host, username, password and database as parameter.
host <- “localhost:27017
username <- “”

password <- “”

db <- “test
Above command now looks like-
mongo <- mongo.create(host=host , db=db, username=username, password=password)

We can also pass the hardcode values-
mongo <- mongo.create(host=”localhost:27017” , db=”Test”, username=””, password=””)


When you type “mongo” in R console, it will give you all the values which are used to connect mongoDB. In my case it returns:

> mongo
[1] 0
<pointer: 0x02f89e58>
[1] “mongo”
[1] “localhost:27017″
[1] “”
[1] “”
[1] “”
[1] “Test”
[1] 0

Below are some basic commands:

To check whether we are connected with MongoDB or not we can execute the below command:



[1] TRUE

To get all databases:

> mongo.get.databases(mongo)

[1] “R”    “test”

To get all the collections in a specific database:
> db <- “test”
> mongo.get.database.collections(mongo, db)


[1] “test.user”               “test.Student”
[3] “test.Track”              “test.ExampleMapReduce”
[5] “test.map_reduce_example” “test.Orders”
[7] “test.Dropthis”           “test.Employee”
[9] “test.Testdelete”         “test.categories”
[11] “test.Book1″              “test.Book2″
[13] “test.Publisher”          “test.numbers”
[15] “test.DemoIndex”          “test.ttt”
[17] “test.Items”

To find something in specific collection:
> JobLocation<-,”test.Employee”,’{“JobLocation”:”Gurgaon”}’)
> JobLocation

_id : 7          53efb77ea6f48f77e8c8f3d4
Name : 3
FName : 2        Suresh
LName : 2        Chaudhary

TechnicalSkill : 4
0 : 2    SQL Server
1 : 2    MSBI
2 : 2    Informatica

Experience : 2   8yrs
JobLocation : 2          Gurgaon

Note: The above command will return BSON object. And we can not use BSON object directly in R. To convert it in R object we can write:
> JobLocation<-,”test.Employee”,’{“JobLocation”:”Gurgaon”}’)

{ $oid : “53efb77ea6f48f77e8c8f3d4″ }

[1] “Suresh”
[1] “Chaudhary”
[1] “SQL Server”  “MSBI”        “Informatica”
[1] “8yrs”
[1] “Gurgaon”

To get all the data, we should use find.all commad.

> JobLocation<- mongo.find.all(mongo,”test.Employee”,’{“JobLocation”:”Noida”}’)
> JobLocation
[1] “53efb77ea6f48f77e8c8f3d0″
[1] “Deepak”
[1] “Sharma”
[1] “SQL Server” “MSBI”       “mongoDB”
[1] “8yrs”
[1] “Noida”

[1] “53efb77ea6f48f77e8c8f3d2″
[1] “Abhishek”
[1] “Perl”    “C++”     “Testing”
[1] “8yrs”
[1] “Noida”

This entry was posted in Big Data on by .

About Deepak Sharma

I am a Microsoft BI Enthusiast and developer. I have been working on various BI tools like SSIS, SSRS, QlikView, Cognos and RDBMS like SQL Server and IBM Netezza as data warehouse appliance. I am currently working as a Team Leader in organization in Noida.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>