0

如果这个问题已经得到解决,我们深表歉意。

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

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

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

4

1 回答 1

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的 s 被解析为数组,而命名list的 s 被解析为对象。

于 2015-06-27T08:39:24.150 回答