首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R- rmongodb $or查询构造

R- rmongodb $or查询构造
EN

Stack Overflow用户
提问于 2015-07-18 12:33:35
回答 1查看 323关注 0票数 1

背景

我将GTFS数据存储在本地mongodb数据库中。

calendar表看起来像

代码语言:javascript
复制
field      | type
service_id | varchar
monday     | int (0 or 1)
tuesday    | int (0 or 1)
...
sunday     | int (0 or 1)

任务

我希望使用service_id中的rmongodb包来选择任何工作日(星期一到星期五)= 1的所有r

在SQL中,这将类似于:SELECT service_id FROM calendar WHERE monday = 1 OR tuesday = 1 OR ... OR friday = 1

细节

当使用罗波蒙戈 GUI时,查询如下:

代码语言:javascript
复制
db.getCollection('calendar').find({"$or" : 
    [{'monday':1},
    {'tuesday':1},
    {'wednesday':1},
    {'thursday':1},
    {'friday':1}]
})

返回8个文档(见图)

因此,在r中,我试图构造相同的or查询,返回相同的结果,但我没有任何运气。

代码语言:javascript
复制
library(rmongodb)
library(jsonlite)
## connect to db
mongo <- mongo.create()
mongo.is.connected(mongo)
db <- "temp"

## days for which I want a service:
serviceDays <- c("monday","tuesday","wednesday","thursday","friday")

尝试0:

代码语言:javascript
复制
## create list as the 'query' condition
ls <- list("$or" = 
         list("monday" = 1L, 
              "tuesday" = 1L, 
              "wednesday" = 1L, 
              "thursday" = 1L, 
              "friday" = 1L))

services <- mongo.find.all(mongo, "temp.calendar", query=ls)
## returns error:
Error in mongo.find(mongo, ns, query = query, sort = sort, fields = fields,  : 
  find failed with unknown error.

尝试1:

代码语言:javascript
复制
## paste the string together
js <- paste0('{"', serviceDays, '":[',1L,']}', collapse=",")
js <- paste0('{"$or" :[', js, ']}')
## this string has been validated at jsonlint.com

bs <- mongo.bson.from.JSON(js)
## run query
services <- mongo.find.all(mongo, "temp.calendar", query=bs)
## result
> services
list()    ## empty list

## manually writing the JSON string doesn't work either
# js <- '{"$or" : [{"monday":[1]},{"tuesday":[1]},{"wednesday":[1]},{"thursday":[1]},{"friday":[1]}]}'

企图2:

代码语言:javascript
复制
## create the or condition using R code
l <- as.list(sapply(serviceDays, function(y) 1L))
bs <- mongo.bson.from.list(list("$or" = list(l)))
## run query
services <- mongo.find.all(mongo, "temp.calendar", query=bs)
## result
> length(services)
[1] 2    ## 2 documents returned

返回的两份文件都是给service_id的,整个星期一,星期二,星期三,星期四,星期五= 1。也就是说,它似乎使用了AND子句,而不是OR

企图3:

代码语言:javascript
复制
## deconstruct the JSON string (attempt 1)
js <- fromJSON(js, simplifyVector=FALSE)
bs <- mongo.bson.from.list(js)

## run query
services <- mongo.find.all(mongo, "temp.calendar", query=bs)
## result
> services
list()    ## empty list

我在R中的查询尝试有什么问题,它阻止了我在使用Robomongo时获得相同的8个文档?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-18 22:22:53

我的“尝试0”已经接近,但是我丢失了更多的list参数。

代码语言:javascript
复制
ls <- list("$or" = list(list("monday" = 1L), 
                    list("tuesday" = 1L), 
                    list("wednesday" = 1L), 
                    list("thursday"= 1L), 
                    list("friday" = 1L)))
## json string:
> toJSON(ls)
{"$or":[{"monday":[1]},{"tuesday":[1]},{"wednesday":[1]},{"thursday":[1]},{"friday":[1]}]} 
## run query:
services <- mongo.find.all(mongo, "temp.calendar", query=ls)

## result
length(services)
[1] 8
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31491209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档