12

I want to get a MongoDB query from R.

With the mongo shell, I would query with:

db.user.find({age:{$gt:21}})

However, In R-Mongo, I haven't found how to describe this query.

Thanks

sashkello
  • 17,306
  • 24
  • 81
  • 109
user189594
  • 191
  • 2
  • 6

3 Answers3

13

If you are using rmongodb (there is a similar package called Rmongo):

     r <- mongo.find(mongo, "test.user", list(age=list('$gt'=21L)))

the BSON query object can also be built like so:

     buf <- mongo.bson.buffer.create()
     mongo.bson.buffer.start.object(buf, "age")
     mongo.bson.buffer.append(buf, "$gt", 21L)
     mongo.bson.buffer.finish.object(buf)
     query <- mongo.bson.from.buffer(buf)
     r <- mongo.find("mongo", "test.user", query)
Gerald Lindsly
  • 256
  • 2
  • 3
8

If you are using RMongo, the query would be:

 dbGetQuery(mongo, "user","{'age':{'$gt': 21}}}")

The result of dbGetQuery() will be a Data Frame.

Stennie
  • 63,885
  • 14
  • 149
  • 175
1

I have also written light interface to R of the pymongo package (the official API for python) https://github.com/RockScience/Rpymongo/blob/master/Rpymongo.r It mimics as close as possible the functions and arguments on the official page of the API http://api.mongodb.org/python/current/api/pymongo/collection.html

RockScience
  • 17,932
  • 26
  • 89
  • 125