如何使用 $and 运算符构造 rmongodb 查询 [英] How to construct rmongodb query using $and operator

查看:44
本文介绍了如何使用 $and 运算符构造 rmongodb 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 rmongodb 访问 R 中的 MongoDB 数据库.我尝试构造以下查询:

I want to use rmongodb to access MongoDB database in R. I tried to construct following query:

{'$and': [{_id: {'$gte': '2013-01-01'}}, {_id: {'$lte': '2013-01-10'}}]}

我尝试了三种不同的方法来创建 bson 对象,但都没有成功.

I tried three different methods to create bson object, having no luck for all.

方法一:

buf = mongo.bson.buffer.create()
mongo.bson.buffer.start.array(buf, '$and')
mongo.bson.buffer.append(buf, '_id', list('$gte'='2013-01-01'))
mongo.bson.buffer.append(buf, '_id', list('$lte'='2013-01-10'))
mongo.bson.buffer.finish.object(buf)
bson = mongo.bson.from.buffer(buf)

方法二:

    buf = mongo.bson.buffer.create()
mongo.bson.buffer.start.array(buf, '$and')
mongo.bson.buffer.start.object(buf, '_id')
mongo.bson.buffer.append(buf, '$gte', '2013-01-01')
mongo.bson.buffer.finish.object(buf)
mongo.bson.buffer.start.object(buf, '_id')
mongo.bson.buffer.append(buf, '$lte', '2013-01-10')
mongo.bson.buffer.finish.object(buf)
mongo.bson.buffer.finish.object(buf)
bson = mongo.bson.from.buffer(buf)

方法三:

mongo.bson.from.list(list('$and'=list('_id' = list('$lte'='2013-01-10'), '_id' = list('$gte'='2013-01-01'))))

它们都不起作用.全部返回空结果.

None of them works. All return empty result.

我搜索了一段时间,唯一提出的类似问题是rmongodb:在查询中使用 $or解决方案是使用 RMongo,而 R 2.50 不支持.

I searched for sometime, the only similar question asked is rmongodb: using $or in query The solution was to use RMongo instead, which is not available for R 2.50.

我被这个问题困了好几天.如果我找不到解决方案,我将不得不用 python 编写一个外部脚本并从 R 中调用它.

I am stuck in this problem for several days. if I cannot find a solution, I would have to write an external script with python and call it from R.

推荐答案

我不懂 R,但您实际上并不需要使用 $and 进行该查询.这有点简单,应该更容易转换为 R:

I don't know R, but you don't actually need to use $and for that query. This is a bit simpler and should be easier to translate to R:

{_id: {'$gte': '2013-01-01', '$lte': '2013-01-10'}}

这篇关于如何使用 $and 运算符构造 rmongodb 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆