rmongodb查询组合AND,OR和字符串匹配

时间:2015-06-26 14:35:25

标签: rmongodb

道歉,如果已经解决了这个问题。

在mongodb中,我有一个名为“main”的数据库集合。我可以从mongodb shell成功运行以下查询:

        db.main.find( {
    $or : [
        { $and : [ { "Var1Path": /.*20072.*/ }, { "Var2Path": /.*30033.*/ } ] },
        { $and : [ { "Var1Path": /.*30033.*/ }, { "Var2Path": /.*20072.*/ } ] },
    ]
} )

我试图用rmongodb在R中运行相同的查询。该查询结合了AND和OR。斜杠充当字符串匹配(例如,在名为Var1Path的字段中的任何位置找到字符串'20072')。这可以用Rmondodb在R中运行吗?如果是这样,应该怎么写?

1 个答案:

答案 0 :(得分:0)

您是否尝试编写一些代码/读取rmongodb晕影?
你需要这样的东西:

and_1 = list("$and" = list( list("Var1Path" = list("$regex" = "20072" )), 
                            list("Var2Path" = list("$regex" = "30033" )) ))
and_2 = list("$and" = list( list("Var1Path" = list("$regex" = "30033" )), 
                            list("Var2Path" = list("$regex" = "20072" )) ))
mongo.find(mongo = mongo, ns = 'db_name.collection_name', 
           query = list("$or" = list(and_1, and_2)))

所有rmongodb用户应记住的一件事:mognodb对象与R对象之间的映射非常直接 - 未命名list被解析为数组并命名为list被解析为对象。

相关问题