r - rmongodb $或查询构造
r - rmongodb $or query construction
背景
我有 GTFS 数据存储在本地 mongodb 数据库中。
calendar
table长得像
field | type
service_id | varchar
monday | int (0 or 1)
tuesday | int (0 or 1)
...
sunday | int (0 or 1)
任务
我想 select 任何工作日(周一到周五)= 1 的所有 service_id
,使用 r
中的 rmongodb
包。
在 SQL 中类似于:SELECT service_id FROM calendar WHERE monday = 1 OR tuesday = 1 OR ... OR friday = 1
详情
使用 Robomongo GUI 时,查询是:
db.getCollection('calendar').find({"$or" :
[{'monday':1},
{'tuesday':1},
{'wednesday':1},
{'thursday':1},
{'friday':1}]
})
其中 returns 8 个文档(见图)
因此,在 r
中,我试图构建相同的 or
查询,将 return 得到相同的结果,但我运气不好。
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:
## 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:
## 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:
## 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
两个文档 returned 是针对 service_id
s 的,其中所有星期一、星期二、星期三、星期四、星期五 = 1。即,它似乎使用了 AND
子句,而不是 OR
.
尝试 3:
## 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 GUI 时相同的 8 个文档?
我的 'attempt 0' 很接近,但我缺少更多 list
个参数。
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
背景
我有 GTFS 数据存储在本地 mongodb 数据库中。
calendar
table长得像
field | type
service_id | varchar
monday | int (0 or 1)
tuesday | int (0 or 1)
...
sunday | int (0 or 1)
任务
我想 select 任何工作日(周一到周五)= 1 的所有 service_id
,使用 r
中的 rmongodb
包。
在 SQL 中类似于:SELECT service_id FROM calendar WHERE monday = 1 OR tuesday = 1 OR ... OR friday = 1
详情
使用 Robomongo GUI 时,查询是:
db.getCollection('calendar').find({"$or" :
[{'monday':1},
{'tuesday':1},
{'wednesday':1},
{'thursday':1},
{'friday':1}]
})
其中 returns 8 个文档(见图)
因此,在 r
中,我试图构建相同的 or
查询,将 return 得到相同的结果,但我运气不好。
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:
## 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:
## 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:
## 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
两个文档 returned 是针对 service_id
s 的,其中所有星期一、星期二、星期三、星期四、星期五 = 1。即,它似乎使用了 AND
子句,而不是 OR
.
尝试 3:
## 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 GUI 时相同的 8 个文档?
我的 'attempt 0' 很接近,但我缺少更多 list
个参数。
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