在查询中使用 $or 数组 [英] Using $or array in query

查看:52
本文介绍了在查询中使用 $or 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 R 驱动程序 rmongodb 查询 MongoDB.以下查询适用于 cmd 行(结果:204,915):

I'm trying to query a MongoDB via the R driver rmongodb. The following query works on the cmd line (result: 204,915):

db.col1.count( 
    {
        $or: [
            {'status.time':{$gt: ISODate('2013-09-10 00:00:00')}},
            {'editings.time':{$gt: ISODate('2013-09-10 00:00:00')}}
        ]
    } );

将其翻译成 R,我尝试过:

Translating this into R, I tried:

d<-strptime('2013-09-10', format='%Y-%m-%d')
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.array(buf, "$or")
mongo.bson.buffer.start.object(buf, 'status.time')
mongo.bson.buffer.append(buf, "$gt", d)
mongo.bson.buffer.finish.object(buf)
mongo.bson.buffer.start.object(buf, 'editings.time')
mongo.bson.buffer.append(buf, "$gt", d)        
mongo.bson.buffer.finish.object(buf)

这是查询在 R 中打印的内容:

This is what the query prints in R:

>mongo.bson.from.buffer(buf)
    $or : 4      
        status.time : 3      
            $gt : 9      79497984

        editings.time : 3    
            $gt : 9      79497984

使用...执行查询

mongo.count(mongo, db1.col1, query=mongo.bson.from.buffer(buf))

...给我-1".我尝试了 BSON 的几种变体,结果都一样.但是,仅使用其中一个条件(没有 $ 或数组)是有效的.有没有人看到我的错误?

...gives me "-1". I tried several variants of the BSON, all with the same result. Using only one of the conditions (without the $or array) works, however. Does anyone see my mistake?

顺便说一句:我知道线程 rmongodb: using $or in query 但是,使用替代驱动程序 RMongo 的建议答案不满足我的代码的其他要求.

BTW: I'm aware of the thread rmongodb: using $or in query, however, the suggested answer to use the alternative driver RMongo does not satisfy other requirements of my code.

推荐答案

您创建 mongo bson 数组的方式是错误的.您缺少零件

your way of creating an mongo bson array is wrong. You are missing the parts

mongo.bson.buffer.start.object(buf, "0")
...
mongo.bson.buffer.finish.object(buf)
mongo.bson.buffer.start.object(buf, "1")
...
mongo.bson.buffer.finish.object(buf)

有关工作示例,请查看有关以下内容的最新评论:https://github.com/mongosoup/rmongodb/issues/17

For a working example please check the latest comment on: https://github.com/mongosoup/rmongodb/issues/17

我希望这现在有效.我正在研究一个更简单的解决方案!

I hope this works for now. I am working on an easier solution!

这篇关于在查询中使用 $or 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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