Using $or array in query

荒凉一梦 提交于 2019-12-25 09:36:20

问题


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')}}
        ]
    } );

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)

EDIT: 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

Executing the query using...

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

...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?

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.


回答1:


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)

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!




回答2:


To avoid having to compose the sequence of mongo.bson.buffer-statements I wrote a package (rmongodbHelper) that will translate a JSON or a list() to a BSON object which can then be used with rmongodb.

First let's setup the environment:

library(rmongodb)

# install rmongodbHelper package from GitHub

library(devtools)
devtools::install_github("joyofdata/rmongodbHelper")
library(rmongodbHelper)

# the MongoDB instance

ns <- "dbx.collx"
M <- mongo.create()
mongo.is.connected(M)
mongo.remove(M, ns, json_to_bson("{}"))

# inserting a number of dummy objects
# JSON keys currently are expected to be wrapped in double quotes!

objs <- c(
  '{"_id":"__int(0)", "dates":{}}',
  '{"_id":"__int(1)", "dates":{"a":"__time(2013-01-01)", "b":"__time(2013-01-01)"}}',
  '{"_id":"__int(2)", "dates":{"a":"__time(2013-01-01)", "b":"__time(2014-01-01)"}}',
  '{"_id":"__int(3)", "dates":{"a":"__time(2014-01-01)", "b":"__time(2013-01-01)"}}',
  '{"_id":"__int(4)", "dates":{"a":"__time(2014-01-01)", "b":"__time(2014-01-01)"}}'
)

for(obj in objs) {
  mongo.insert(M, ns, json_to_bson(obj))
}

Let's see via MongoDB shell if they were successfully inserted:

> use dbx
switched to db dbx
> db.collx.find().pretty()
{ "_id" : 0, "dates" : { } }
{
        "_id" : 1,
        "dates" : {
                "a" : ISODate("2013-01-01T00:00:00Z"),
                "b" : ISODate("2013-01-01T00:00:00Z")
        }
}
[...]
{
        "_id" : 4,
        "dates" : {
                "a" : ISODate("2014-01-01T00:00:00Z"),
                "b" : ISODate("2014-01-01T00:00:00Z")
        }
}

Now let's search for documents with a query:

# searching for those objects
# JSON keys currently are expected to be wrapped in double quotes!

json_qry <- 
'{
  "$or": [
    {"dates.a":{"$gt": "__time(2013-06-10)"}},
    {"dates.b":{"$gt": "__time(2013-06-10)"}}
  ]
}'

cur <- mongo.find(M, "dbx.collx", json_to_bson(json_qry))

while(mongo.cursor.next(cur)) {
    print(mongo.cursor.value(cur))
}

And this is what we get in the end:

_id : 16     2
dates : 3    
    a : 9    -211265536
    b : 9    1259963392

_id : 16     3
dates : 3    
    a : 9    1259963392
    b : 9    -211265536

_id : 16     4
dates : 3    
    a : 9    1259963392
    b : 9    1259963392

  • keys - also operators like $or - need to be put in double quotes.
  • "x":3 will lead to 3 being casted as double
  • "x":"__int(3)" will lead to 3 being casted as integer


来源:https://stackoverflow.com/questions/19952705/using-or-array-in-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!