如何从R查询MongoDB ?

时间:2021-11-28 11:29:15

I want to get a MongoDB query from R.

我想从R获取MongoDB查询。

With the mongo shell, I would query with:

对于mongo shell,我的问题是:

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

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

然而,在R-Mongo中,我还没有找到如何描述这个查询。

Thanks

谢谢

3 个解决方案

#1


13  

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

如果您正在使用rmongodb(有一个类似的包称为Rmongo):

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

the BSON query object can also be built like so:

BSON查询对象也可以这样构建:

     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)

#2


8  

If you are using RMongo, the query would be:

如果您使用的是RMongo,查询将是:

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

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

dbGetQuery()的结果将是一个数据框架。

#3


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

我还为pymongo包(python的官方API)的R编写了light接口(https://github.com/RockScience/Rpymongo/blob/master/Rpymongo.r),它尽可能地模拟API http://api.mongodb.org/python/api/currentmongo/collection.html的官方页面上的函数和参数

#1


13  

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

如果您正在使用rmongodb(有一个类似的包称为Rmongo):

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

the BSON query object can also be built like so:

BSON查询对象也可以这样构建:

     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)

#2


8  

If you are using RMongo, the query would be:

如果您使用的是RMongo,查询将是:

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

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

dbGetQuery()的结果将是一个数据框架。

#3


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

我还为pymongo包(python的官方API)的R编写了light接口(https://github.com/RockScience/Rpymongo/blob/master/Rpymongo.r),它尽可能地模拟API http://api.mongodb.org/python/api/currentmongo/collection.html的官方页面上的函数和参数