[英]Rmongodb using regex does not work as expected
I am facing an issue while querying mongodb from R(using rmongodb package) using regular expressions. 使用正则表达式从R(使用rmongodb包)查询mongodb时遇到问题。 Below is my code:
下面是我的代码:
buf <- mongo.bson.buffer.create()
regex <- mongo.regex.create("air filter*$", options="i")
mongo.bson.buffer.append.regex(buf, "keyword", regex)
query <- mongo.bson.from.buffer(buf)
res <- mongo.find.all(mongo,collection,query)
But I get empty result in 'res' from the DB. 但是我从数据库中得到空结果“ res”。 However, when I check the regex in the DB directly, it gives me valid results.
但是,当我直接在数据库中检查正则表达式时,它给了我有效的结果。 When I give the keyword name without regular expressions in the above query
(eg: "air filter")
,it works fine. 当我在上面的查询中给出不带正则表达式的关键字名称时
(eg: "air filter")
,它可以正常工作。
I have tried all possible combinations but in vain. 我尝试了所有可能的组合,但徒劳无功。
Following are the example docoments 以下是示例文档
[
{
"_id":ObjectId("55dcdc72473fdf86c0020d96"),
"_class":"",
"keyword":"air filter",
"synonyms":[
]
},
{
"_id":ObjectId("55dcdc72473fdf86c0020e0f"),
"_class":"",
"keyword":"cabin air filter",
"synonyms":[
]
},
{
"_id":ObjectId("55dcdc79473fdf86c002143b"),
"_class":"",
"keyword":"secondary air filter",
"synonyms":[
]
}
]
Note: I have tried json string but cannot use since I need to pass variable as input regex,ie I create the regex using paste0(component[1], "*$")
where component list contains the terms like air filter,etc. 注意:我尝试了json字符串,但是不能使用,因为我需要将变量作为输入正则表达式传递,即我使用
paste0(component[1], "*$")
创建正则表达式paste0(component[1], "*$")
其中组件列表包含诸如空气过滤器之类的术语。
Could you kindly provide some guidance? 您能提供一些指导吗?
Thanks! 谢谢!
Here is what I tried and it is working 这是我尝试过的并且正在工作
library(rmongodb)
mongo <- mongo.create(host = "localhost", db = "test")
components <- list("air filter", "engine oil")
for (component in components) {
jsonStr <- paste0('{"keyword" : {"$regex" : "', component, '$"}}')
mongo.bson.from.JSON(jsonStr)
res <- mongo.find.all(mongo, "test.coll", mongo.bson.from.JSON(jsonStr))
print(res)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.