R / Mongolite:如何$展开数据框?

时间:2018-09-04 14:55:52

标签: r aggregate pipeline mongolite

我正在使用mongolite。我的数据集中有一个数组,我想使用$ unwind解构。我做了以下事情:

pUsers <- pUsers$aggregate(pipeline = {"$unwind" : "$profile.hobbies"})

结果:

Error in "$unwind":"$profile.hobbies" : NA/NaN argument
In addition: Warning messages:
1: In inherits(x, "bson") : NAs introduced by coercion
2: In inherits(x, "bson") : NAs introduced by coercion

看到错误消息,我尝试使用以下代码排除具有NA值的数据:

pUsers <- pUsers$aggregate(pipeline = '[
        {"$match" : {"$profile.hobbies" : {"$exists" : true}}}, 
        {"$unwind" : "$profile.hobbies"}]')

结果:

Error: unknown top level operator: $profile.hobbies

有人可以解释我的错误吗?此外,如何正确展开数据框? 谢谢!

1 个答案:

答案 0 :(得分:0)

如前所述,R的语法与Mongo的查询调用不一致,特别是在花括号和冒号不同的情况下,R的语法与其他语言可以在这些位置支持这些符号不同。

因此,请确保将整个调用包装在带引号的字符串中,并带有所有必需的符号(例如方括号)。具体是以下内容:

pUsers <- pUsers$aggregate(pipeline = {"$unwind" : "$profile.hobbies"})

应修改为

pUsers <- pUsers$aggregate(pipeline = '[{"$unwind" : "$profile.hobbies"}]')