0

I am trying to connect mongodb in R using Rmongo library.But query function giving me error.

> chn = mongoDbConnect("marketing_db", host = 'localhost', port = 27017)
> print(dbShowCollections(chn))
character(0)
> results = dbGetQuery(chn, marketing_data, {}, 0, 5)
Error in dbGetQuery(chn, marketing_data, { : 
  object 'marketing_data' not found
> results = dbGetQuery(chn, "marketing_data", {}, 0, 5)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘dbGetQuery’ for signature ‘"RMongo", "character", "NULL", "numeric", "numeric"’
> results = dbGetQuery(chn, "marketing_data", query = {}, skip = 0, limit = 5)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘dbGetQuery’ for signature ‘"RMongo", "character", "NULL", "numeric", "numeric"’

Where marketing_db is db and marketing_data is collection in my mongodb

Below is output from mongo console:

> show dbs
admin         0.000GB
config        0.000GB
local         0.000GB
marketing_db  0.001GB
myblogs       0.000GB
test          0.003GB
> use marketing_db
switched to db marketing_db
> show collections
marketing_data
> db.marketing_data.count()
7414
> db.marketing_data.find().pretty().limit(1)
{
        "_id" : ObjectId("5b1d65c6cc6e576297462cdc"),
        "custAge" : 55,
        "profession" : "admin.",
        "marital" : "single"
}

I am using R for windows 3.4.3 and MongoDB 3.6.5

Please help in resolving this error.

Mohit Sharma
  • 590
  • 3
  • 10
  • in first attempt you did not set `marketing_data` variable in your code. also signature is different: `dbGetQuery(rmongo.object, collection, query, skip=0, limit=1000)`. Query should be a string, e.g. `{}` – Bulat Jun 10 '18 at 21:31
  • Thanks Bulat, it worked but i have also another issue in this, why there is output as character(0) in dbshowCollections(chn) function. why it is not showing collection name? – Mohit Sharma Jun 11 '18 at 06:52

1 Answers1

1

Try passing query json as a string:

results = dbGetQuery(chn, "marketing_data", "{}", 0, 5)
Bulat
  • 6,869
  • 1
  • 29
  • 52