Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query MongoDB from R?

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

like image 943
user189594 Avatar asked Sep 23 '11 09:09

user189594


People also ask

How do I query data in MongoDB?

To query data from MongoDB collection, you need to use MongoDB's find() method.

How do I run a query in MongoDB terminal?

To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.

Is MongoDB good for query?

MongoDB is the premier NoSQL document database for modern developers working on high-performance applications. With its JSON-like documents, MongoDB is notable for horizontal scaling and load balancing, which offers developers an excellent balance of customization and scalability.


1 Answers

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)
like image 119
Gerald Lindsly Avatar answered Sep 25 '22 09:09

Gerald Lindsly