Install-Package

Connect R with mongoDB

n 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)

We can also install it  by writing command on R console-
install.packages(“rmongodb”)

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

library(rmongodb)

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
attr(,”mongo”)
<pointer: 0x02f89e58>
attr(,”class”)
[1] “mongo”
attr(,”host”)
[1] “localhost:27017″
attr(,”name”)
[1] “”
attr(,”username”)
[1] “”
attr(,”password”)
[1] “”
attr(,”db”)
[1] “Test”
attr(,”timeout”)
[1] 0

Below are some basic commands:

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

> mongo.is.connected(mongo)

Result:

[1] TRUE

To get all databases:

> mongo.get.databases(mongo)
Result:

[1] “R”    “test”

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

Result:

[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<- mongo.find.one(mongo,”test.Employee”,’{“JobLocation”:”Gurgaon”}’)
> JobLocation
Result:

_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<- mongo.find.one(mongo,”test.Employee”,’{“JobLocation”:”Gurgaon”}’)
> mongo.bson.to.list(JobLocation)
Result:

$`_id`
{ $oid : “53efb77ea6f48f77e8c8f3d4″ }

$Name
$Name$FName
[1] “Suresh”
$Name$LName
[1] “Chaudhary”
$TechnicalSkill
[1] “SQL Server”  “MSBI”        “Informatica”
$Experience
[1] “8yrs”
$JobLocation
[1] “Gurgaon”

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

> JobLocation<- mongo.find.all(mongo,”test.Employee”,’{“JobLocation”:”Noida”}’)
> JobLocation
Result:
[[1]]
[[1]]$`_id`
[1] “53efb77ea6f48f77e8c8f3d0″
[[1]]$Name
[[1]]$Name$FName
[1] “Deepak”
[[1]]$Name$LName
[1] “Sharma”
[[1]]$TechnicalSkill
[1] “SQL Server” “MSBI”       “mongoDB”
[[1]]$Experience
[1] “8yrs”
[[1]]$JobLocation|
[1] “Noida”

[[2]]
[[2]]$`_id`
[1] “53efb77ea6f48f77e8c8f3d2″
[[2]]$Name
[[2]]$Name$FName
[1] “Abhishek”
[[2]]$TechnicalSkill
[1] “Perl”    “C++”     “Testing”
[[2]]$Experience
[1] “8yrs”
[[2]]$JobLocation
[1] “Noida”

Leave a Reply

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